• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting text in an image file

 
Ishan Bhatt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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.
Ishan
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that this may not be the best topic for a beginner to cut his teeth on, but here's Sun's tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html

And here's the Hello World program:

[ February 02, 2006: Message edited by: Jeff Albertson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic