ssherlock
asked on
Text to GIF
Is there an easy (or any :) way to save text (such as system time and date) to a GIF file. I need a GIF for watermarking a PDF report.
http://chimpen.com/things/archives/001139.php
Probably too slow :-(
-------------------------- ---------- --------
import java.awt.* ;
import java.awt.image.* ;
import java.io.* ;
import com.sun.image.codec.jpeg.* ;
public class ImageMaker
{
public ImageMaker()
{
}
public static void saveBufferedImage( String filename, BufferedImage image )
{
try
{
File f = new File( filename ) ;
FileOutputStream out = new FileOutputStream( f ) ;
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode r( out ) ;
JPEGEncodeParam param = encoder.getDefaultJPEGEnco deParam( image ) ;
param.setQuality( 1.0f, false ) ;
encoder.setJPEGEncodeParam ( param ) ;
encoder.encode( image ) ;
out.close() ;
}
catch( IOException e )
{
e.printStackTrace() ;
}
}
public static BufferedImage makeImage( Color bgColor,
Color txtColor,
String text,
Font font )
{
BufferedImage bi = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB ) ;
Graphics g = bi.getGraphics() ;
g.setFont( font ) ;
FontMetrics fm = g.getFontMetrics() ;
int width = fm.stringWidth( text ) + 10 ;
int height = fm.getAscent() + 10 ;
bi = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ) ;
g = bi.getGraphics() ;
fm = g.getFontMetrics() ;
g.setFont( font );
g.setColor( bgColor );
g.fillRect( 0, 0, width, height ) ;
g.setColor( txtColor ) ;
g.drawString( text, 5, fm.getAscent() + 5 ) ;
return bi ;
}
public static void main(String[] args)
{
BufferedImage bi = ImageMaker.makeImage( Color.red, Color.white, "TimYates", new Font( "Courier", Font.BOLD, 20 ) ) ;
ImageMaker.saveBufferedIma ge( "/users/tyates/woo.jpg", bi );
}
}
--------------------------
import java.awt.* ;
import java.awt.image.* ;
import java.io.* ;
import com.sun.image.codec.jpeg.*
public class ImageMaker
{
public ImageMaker()
{
}
public static void saveBufferedImage( String filename, BufferedImage image )
{
try
{
File f = new File( filename ) ;
FileOutputStream out = new FileOutputStream( f ) ;
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode
JPEGEncodeParam param = encoder.getDefaultJPEGEnco
param.setQuality( 1.0f, false ) ;
encoder.setJPEGEncodeParam
encoder.encode( image ) ;
out.close() ;
}
catch( IOException e )
{
e.printStackTrace() ;
}
}
public static BufferedImage makeImage( Color bgColor,
Color txtColor,
String text,
Font font )
{
BufferedImage bi = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB
Graphics g = bi.getGraphics() ;
g.setFont( font ) ;
FontMetrics fm = g.getFontMetrics() ;
int width = fm.stringWidth( text ) + 10 ;
int height = fm.getAscent() + 10 ;
bi = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB
g = bi.getGraphics() ;
fm = g.getFontMetrics() ;
g.setFont( font );
g.setColor( bgColor );
g.fillRect( 0, 0, width, height ) ;
g.setColor( txtColor ) ;
g.drawString( text, 5, fm.getAscent() + 5 ) ;
return bi ;
}
public static void main(String[] args)
{
BufferedImage bi = ImageMaker.makeImage( Color.red, Color.white, "TimYates", new Font( "Courier", Font.BOLD, 20 ) ) ;
ImageMaker.saveBufferedIma
}
}
To make a GIF, you will need a GIF encoder, and use that (I use the JPEG encode which comes with java in the obscure com.sun.image.codec.jpeg package)
Tim
Tim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Tim, That was exactly what I was after (and the speed is acceptable as it's being written to a PDF anyway)
Cheers, Simon
Cheers, Simon
:-) Glad I could help :-)
ASKER
Is it possible to use said image in memory rather than save it to disk? The reason for this is that it will be created everytime a report is requested and then is no longer required. Because it is an enterprise system I would rather not be creating and deleting small files on the server.
Thanks
Thanks
it depends on whether the PDF generator you use allows you to embed images...
Which one are you using?
Which one are you using?
ASKER
iText (http://www.lowagie.com/iText/) for creating reports in PDF format on a J2EE system