File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Putting text in an image file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Putting text in an image file" Watch "Putting text in an image file" New topic

Putting text in an image file

Ishan Bhatt

Joined: Oct 09, 2005
Posts: 14
How can we put some text in an image file using Java?
I have a String and I want to put this String in an image file (.gif, .bmp, .jpg or any other).
Any help would be appreciated.

Thank you.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You certainly can write text into an image in Java, and you can write the result into a file in some popular image formats.

However, if, as posting in the beginner forum suggests, you are new to Java, this might not be a good choice for your first Java program!

You can use javax.imageio.ImageIO class to read and write the image.

To write text into the image, you need to get it as a BufferedImage. Then get a Graphics2D from your BufferedImage. Then you can use Graphics2D.drawString() to write text. You may wish to set the Font and Color of the Graphics2D before writing the text. A FontMetrics object can be used to find out how big the text will be.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I agree that this may not be the best topic for a beginner to cut his teeth on, but here's Sun's tutorial:

And here's the Hello World program:

[ February 02, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
I agree. Here's the link:
subject: Putting text in an image file
It's not a secret anymore!