Solved

Text to GIF

Posted on 2003-12-02
11
548 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
pagenation logic how it is working in my code 1 31
Adding multiple JVM environments to RedHat 6 7 23
Selenium docs api java index 3 19
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

821 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