Search This Blog

Tuesday, January 20, 2009

text-image-generator example

As a follow-up on my previous post, here is an example of how to use the text-image generation library. Consider the following code:

TextImage testImage = new TextImageImpl(400, 250, new Margin(0, -5));

// Declare or read the fonts you need
Font header = new Font("Sans-Serif", Font.BOLD, 15);
Font plain = new Font("Sans-Serif", Font.PLAIN, 12);

// Read in any images you need
InputStream is = Test.class.getResourceAsStream("/warning.jpg");
Image warning = ImageIO.read(is);
// Put close() in try-finally block with production code!
is.close();

// 1. specify font and write text with a newline
testImage.useFont(header).writeLine("Example usage text-image-generator.").newLine();
// 2. enabled the wrapping of text and write text which is automatically wrapped
testImage.useFont(plain).wrap(true).write("This is an example of how to use the text-image generation library. With this library you can dynamically generated textbased content as images (png or jpeg). Notice how this sentence is automatically wrapped. See the code at http://code.google.com/p/textimagegenerator/ for more examples.").newLine();

// 3. Disable text-wrapping again. Write underlined text.
testImage.wrap(false).useColor(Color.BLUE).useFontStyle(Style.UNDERLINED).write("This line of text is underlinedand blue.").useFontStyle(Style.PLAIN).newLine();
testImage.writeLine("You can also embed images in your generated image:");
// 4. embed the actual image. Here we use absolute positioning
testImage.write(warning, 175, 175);

// 5. Write the image as a png to a file
OutputStream os = new FileOutputStream(new File("example.png"));
testImage.createPng(os);
os.close();

The code above generates the following image:


The text-image generation code can be downloaded at http://code.google.com/p/textimagegenerator/. Here you will find more examples of how to use this library. Hope you enjoy.

25 comments:

Becoming Arcadian said...
This comment has been removed by the author.
Becoming Arcadian said...
This comment has been removed by the author.
TEXT TO PIC said...

http://www.ImageGenerator.org has many fun image generators to see what you can make with this script as a sample.

Arun said...

Hi Jamie craane,
I want to change the background color, is there any way to change the background color? Please advice

Arun said...

Hi
how to find the class file for this jar

Jamie Craane said...

Hi Arun,

You can use the performAction method on the TextImage interface and providing an instance of the TextImageCallback. With TextImageCallback you gain access to the Graphics2D class todo all kinds of advanced stuff.

See http://code.google.com/p/textimagegenerator/source/browse/trunk/src/test/java/nl/jamiecraane/imagegenerator/examples/BackgroundColor.java for an example of how to change the background color.

Hope this helps.

fashion update said...
This comment has been removed by the author.
Arun said...

Thanks Jamie craane, your suggestion was excellent and moreover it worked.
I appreciate your help.

fashion update said...

clearRect (int X, int Y, int Width, int Height). Will this method help for clearing the default color which appears for the Background? The problem I am facing now is, the colors got blend. The default color and as well as the color which I mentioned to appear, so I get a different color but not the mentioned one.

Jamie Craane said...

Hi,

Can you please post some code to clarify your problem so I can take a look.

Thanks.

Regards,
Jamie

Arun said...

Hi Jamie,
You have used "png" format to display the output, however I am doing the same example to display the output in "Jpg" format. The problem with the "Jpg" format is, it could not give me the image quality as Png gives. What would be the problem? Please advice. And I have made a modification, I am not using an English Text, I am using a foreign language. Will that be a problem?

Jamie Craane said...

Hi Arun,

Yes, I noticed this to with the example. This is due to the JPEG compression. I will release a new version soon (within the next week) which creates a JPEG with the highest possible quality which should solve your problem. (Although PNG is better suited for these kinds of images than JPEG compression).

Regards,
Jamie

Jamie Craane said...

Hi Arun,

I uploaded a new version (1.1) which fixes the JPEG compression and provide an alternative way to export the image.

See http://code.google.com/p/textimagegenerator/downloads/list.

A 2.0 version is planned which is distributed via Maven central.

Please let me know if this release fixes your issue.

Regards,
Jamie

Arun said...

Hi Jamie,
I imported the new jar file which you posted yesterday to my IDE(eclipse) and started executing the program, However I could not get the clear output. It remains the same.
Is that the correct way to implement or should I call any of the methods from the new classes like (ImageWriter,ImageWriterFactory..... ) Please advice.

Jamie Craane said...

Hi Arun,

Yes, should use the new ImageWriter class. I decoupled the exporter functionality from the actual TextImageImpl. I have updated the previous example which uses this class.

Here is an example:
ImageWriter imageWriter = ImageWriterFactory.getImageWriter(ImageType.JPEG);
imageWriter.writeImageToFile(textImage, new File("background-color.jpg"));

You can also use the writeImageToOutputStream method to write to an OutputStream.

Let me know if this helps.

Regards,
Jamie

Arun said...

Hi Jamie,
I am sorry to inform you, the code for Jpeg image quality did not work.

Jamie Craane said...

Hi Arun,

Can you post some sample code so I can have a look?

Also, which version of the JDK are you using?

Regards,
Jamie

Vedika said...

Hi Jamie craane,

I want to change the background color.i can't find the example in the following path.please advice.

http://code.google.com/p/textimagegenerator/source/browse/trunk/src/test/java/nl/jamiecraane/imagegenerator/examples/BackgroundColor.java

Jamie Craane said...

Hi Vedika,

Sorry for the late reaction, I am quite busy lately.

A new version is in the pipeline. I will post an update when it is ready although I am not sure when this will be exactly.

Regards,
Jamie

XIAN sia said...

Hi Jamie,

i tried your codes and it works for creating the text image out, but i am unable to import a different image into the text image as the Test.class.getResourceAsStream and ImageIO isn't found. Is there any other file/library needed too?

Jamie Craane said...

Hi Xian,

I am still busy with a new release but haven't got much time lately. I will investigate the issue.

Regards,
Jamie

Himanshu said...

Hi jamie,
I generate the image for help of above code but these method are deprecated in 1.1.1. new methods name .

Jamie Craane said...

Hi Himanshu,

When I move the project to Github I will continue development and create a stable release of the library.

I will post a message as soon as it is ready but I cannot give a timeline at the moment due to my busy schedule at the moment.

Regards,
Jamie

Vishwa said...

Hi Jamie,

I have tried changing the background color using the example http://code.google.com/p/textimagegenerator/source/browse/trunk/src/test/java/nl/jamiecraane/imagegenerator/examples/BackgroundColor.java

but the color is setting to some other colors, actually when i am trying to WHITE , but the bgcolor is fillling with some oher color.

please help me?

Jamie Craane said...

Hi Vishwa,

I plan to work on the library in the near future. Can you post some code so I can check what went wrong?

Thanks.

Regards,
Jamie