File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How To Add Text to a JPEG image? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How To Add Text to a JPEG image?" Watch "How To Add Text to a JPEG image?" New topic
Author

How To Add Text to a JPEG image?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Is there a good source code example that demonstrates how to embed text into an exisiting JPEG image.
I want to put some copyright information into several hundred JPEG files, but can't find a good reference.
I bought the Java2D API reference, but although it's excellent, this text doesn't appear to have a lot of simple examples to get you understanding the basic stuff first.
I look forward to anyone's reply.
Thanks.
-- Mike
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The Java2D API contains a factory for creating some kind of JPEG decoder and encoder objects.
1) Get an implementation of the JPEGImageDecoder interface with JPEGCodec.createJPEGDecoder(InputStream)
2) Call decodeAsBufferedImage() to get a java.awt.BufferedImage of the image.
3) Use the BufferedImage's getGraphics() or createGraphics() method to get a Graphics object to draw with.
4) Draw the text with the methods provided by the Graphics class, i.e. drawString(String, int, int) or drawString(String, float, float).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Thanks.
You wouldn't have a quick example program would you? <s>
-- Mike
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Nope, sorry
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
Since I've found no complete examples on the web or here, is the issue that everybody knows how to write text on top a JPEG image or nobody does it?
-- Mike
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Actually... I just had this code lying around... it's a water marking gui done in Swing, using the new ImageIO API...

Here it is, all 720 lines of it... Enjoy!



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Add Text to a JPEG image?