Solved

Text to GIF

Posted on 2003-12-02
11
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 7

Expert Comment

by:maheshexp
ID: 9859329
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 35

Expert Comment

by:TimYates
ID: 9859481
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
ID: 9859492
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
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 9859504
 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
ID: 9859566
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
ID: 9859605
:-)  Glad I could help :-)
0
 

Author Comment

by:ssherlock
ID: 9865002
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
ID: 9865047
it depends on whether the PDF generator you use allows you to embed images...

Which one are you using?
0
 

Author Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Suggested Courses

627 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