Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2007, 02:39 AM
Member
 
Join Date: Jul 2007
Location: Mexico City
Posts: 2
elcapi is on a distinguished road
Writing text into an image and save it
Hi,

I need to insert/write text into an existing image and I would like to know if someone has some experience with this. The goal is to open an image then write some text on it and then save it. The saved image should be displayed with the text wrote before.

At this moment I can open the image:

Code:
FileSeekableStream stream = new FileSeekableStream(imageFilename); TIFFDecodeParam decodeParam = new TIFFDecodeParam(); decodeParam.setDecodePaletteAsShorts(true); ParameterBlock params = new ParameterBlock(); params.add(stream); RenderedOp image1 = JAI.create("tiff", params); BufferedImage img = image1.getAsBufferedImage();
With the BufferedImage object, I can display the image with a graphics object and display some text too.

Code:
public void paint(Graphics g) { Dimension size = getSize(); g.drawImage(img, 0, 0, size.width, size.height, 0, 0, img.getWidth(null), img.getHeight(null), null); Font font = new Font("Serif", Font.PLAIN, 18); g.setFont(font); g.setColor(Color.black); g.setFont(font); g.drawString("Some Text...", 10,10); }
The idea is to do something similar without displaying the image and without using a graphics object or, someone know if is possible to save the objects displayed from the graphics object into an image?

I have been looking for some class in JAI that let me do that, but I haven't found the way to do it.

Any ideas would be appreciated
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-11-2007, 08:44 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Check createGraphics() method of BufferedImage class:

Quote:
Creates a Graphics2D, which can be used to draw into this BufferedImage.
BufferedImage (Java 2 Platform SE v1.4.2)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-11-2007, 08:09 PM
Member
 
Join Date: Jul 2007
Location: Mexico City
Posts: 2
elcapi is on a distinguished road
Hi,

thanks a lot for the tip.

Now I can insert text with the Graphics2D object and save it with the ImageIO class

Code:
Graphics2D g2 = img.createGraphics(); g2.setColor(Color.black); g2.setFont(font); g2.drawString(text, 80,1440); File outputfile = new File(f"imagewithtext.png"); ImageIO.write(img, "png", outputfile);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to save image to disk after using pixelgrabber shishirg Advanced Java 3 04-17-2008 04:58 PM
How can I save the content of textfield in a text file? fred Java Applets 4 08-09-2007 02:30 PM
how to save image as jpg? katie New To Java 2 08-06-2007 04:32 AM


All times are GMT +3. The time now is 08:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org