Solved

Text to GIF

Posted on 2003-12-02
11
549 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
ID: 9859329
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 9859347
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 9859361
0
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
maven module vs maven project 3 46
What's wrong with this code? 4 26
Is there a simpler dropbox system? 10 34
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

839 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