This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Text to Image to File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Text to Image to File" Watch "Text to Image to File" New topic
Author

Text to Image to File

Saidul Islam
Greenhorn

Joined: Dec 05, 2000
Posts: 18
Sorry guys if I am posting it in the wrong forum.
I am trying to convert a text value (String) to an image (.jpg). My program seems to be working somewhat. It's creating a blank (whiite background) .jpg file, without my string value "Saidul Islam". I can open the file in any image editor. it just doesn't have the string I am trying to print.
And the strange part is that the program is not even terminating.
Here is the output I get when I run it
<pre>
C:\test>java Text2Image
gets here
flushed
closed
final
dispose
remove notify
done!
</pre>
it's of course printing the last line but the program is not terminating. I have been working on this since yesterday. and my brain is not functioning anymore on this. So I ask for your help.
what am I doing wrong and how do I get this working.
Here is my program-
<pre>
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;

public class Text2Image {
public static void main(String args[]) {
Frame myFrame = null;
Graphics2D myGraphics = null;
try {
myFrame = new Frame();
myFrame.addNotify();
BufferedImage buffImage = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
BufferedImage buff2Image = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("text.jpg"));
Image offImage = myFrame.createImage(400, 200);
//myGraphics = buffImage.getGraphics();
myGraphics = buffImage.createGraphics();
myGraphics.setFont(new Font("Serif", Font.ITALIC, 48));
myGraphics.setColor(Color.red);
myGraphics.drawString("Saidul Islam", 10, 50);
myGraphics.drawImage(offImage, 0, 0, myFrame);
buffImage.createGraphics().drawImage(buffImage, 0, 0, myFrame);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(buffImage);

//JpegEncoder jpg = new JpegEncoder(offImage, 80, bos);
//jpg.Compress();

System.out.println("gets here");
bos.flush();
System.out.println("flushed");
bos.close();
System.out.println("closed");

buffImage.flush();
buff2Image.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
System.out.println("final");
if (myGraphics != null) {
myGraphics.dispose();
System.out.println("dispose");
}
if (myFrame != null) {
myFrame.removeNotify();
System.out.println("remove notify");
}
}
System.out.println("done!");
} //end of main method
} //end of Text2Image class
</pre>

Thanks for your help!


<b>JStar</b><BR>
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Saidul,

First, let me say... Really awesome program!

I think I have fixed the problems you were having...

this line was causing the image you had created so far to be overwritten with a blank image of the same size...

I assume that you were trying to assign the image to the frame after you had created it... I couldn't find a direct way to paint a Graphics object or a BufferedImage object, so I simply saved the image first and then displayed it... The code I added isn't the best... there is no double buffering being done to keep the image from being erased in screen redraws, but this could be easily fixed by making a special class to display the image... I just used a regular canvas...

As far as the reason behind why your program would not exit... I think it is because you must specifically call System.exit if you are using any GUI elements... I think Java launches some kind of GUI manager that creates another thread that keeps all the GUI elements alive... so you have to specifically call System.exit...

On to the code...



HTH,
-Nate
[This message has been edited by Nathan Pruett (edited May 31, 2001).]


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Saidul Islam
Greenhorn

Joined: Dec 05, 2000
Posts: 18
Thanks Nate. It worked fine. And yes it looks like I have to use System.exit(0) to break out of the program.
 
Consider Paul's rocket mass heater.
 
subject: Text to Image to File
 
Similar Threads
Getting Bitmap from Canvas and converting it into image file format
How to Export Images as GIF, JPG?
Help!Help!
Text to Image to file
Text to Image to File