Solved

Text to GIF

Posted on 2003-12-02
11
546 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:ssherlock
  • 5
  • 3
  • 3
11 Comments
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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.createJPEGEncoder( out ) ;
      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( 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.saveBufferedImage( "/users/tyates/woo.jpg", bi );
  }
}

0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
Comment Utility
 public static void main(String[] args)
  {
    BufferedImage bi = ImageMaker.makeImage( Color.red,
                                                                  Color.white,
                                                                  "Made at " + new Date().toString(),
                                                                  new Font( "Courier", Font.BOLD, 20 ) ) ;
    ImageMaker.saveBufferedImage( "/users/tyates/woo.jpg", bi );
  }

will obviously do the time and date ;-)
0
 

Author Comment

by:ssherlock
Comment Utility
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
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
:-)  Glad I could help :-)
0
 

Author Comment

by:ssherlock
Comment Utility
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
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
it depends on whether the PDF generator you use allows you to embed images...

Which one are you using?
0
 

Author Comment

by:ssherlock
Comment Utility
iText (http://www.lowagie.com/iText/) for creating reports in PDF format on a J2EE system
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now