Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text to an image in java?

I need to add some texts to an existing table image (png). Which means that I need to "write" on the image and I need the option to select the text location. How can I do it?

like image 802
whiteberryapps Avatar asked Jun 07 '12 09:06

whiteberryapps


People also ask

How do I insert text into an image?

On the Insert tab, in the Text group, click WordArt, click the style of text you want, and then type your text. Click the outside edge of the WordArt to select it, drag the text over your photo and then, if you want to, rotate the text to the angle that works best for your photo.


1 Answers

It's easy, just get the Graphics object from the image and draw your string onto the image. This example (and output image) is doing that:

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

like image 70
dacwe Avatar answered Oct 06 '22 10:10

dacwe