Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text to GIF

Posted on 2003-12-02
11
Medium Priority
?
555 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Suggested Courses

688 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