How to add text to image in java?

I need to add some texts to an existing table image (png). This means that I need to "write" on the image, and I need an option to select the location of the text. How should I do it? Thank you very much.

+57
java string text image graphics
Jun 07 2018-12-12T00:
source share
2 answers

Easy, just get the Graphics object from the image and draw your line on the image. This example (and the output image) does this:

 public static void main(String[] args) throws Exception { final BufferedImage image = ImageIO.read(new URL( "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")); Graphics g = image.getGraphics(); g.setFont(g.getFont().deriveFont(30f)); g.drawString("Hello World!", 100, 100); g.dispose(); ImageIO.write(image, "png", new File("test.png")); } 

Output ( test.png ):

output

+115
Jun 07 2018-12-12T00:
source share

The font has no color attribute, just change the color of the Graphics object.

0
Jun 06 '19 at 6:40
source share



All Articles