File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Export Images as GIF, JPG?

 
Tom McComb
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution might be right under my nose (in my feedbag?) but I want to create a GIF or JPG file from an Image object or Graphics object created in a java application (could be applet or otherwise).
Do I have to buy an add-on API, or is there something in freespace available to do this?
Thanks in advance for your help.
------------------
Tom McComb
 
Tom McComb
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I discovered how to do it with JPEG. Any thoughts on GIF exports?
Only because they seem to be smaller files.
Thanks again.
------------------
Tom McComb
 
Tim Collins
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
I know this doesn't help you at all, but the whole purpose of this site is to share information, right? ;-)
How do you export graphics object (like in a paint method) to a jpg file? I have been looking for a few days now trying to figure out just that thing...
Let me Know
Tim
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gif compressed files will be smaller if the image is less complicated (i.e. small color palette, not so many 'interesting' features - like logos, etc.) but jpeg should be used for 'natural' images such as photographs. This is sort of a rule of thumb, of course, and should be taken with a grain of salt. Anyway, due the the legal issues involved with the gif LZW compression format, I doubt you'll find an 'official' api for this. However, www.acme.com has a nice package that you can use. I hope this helps.
Sean
Sean
 
Tom McComb
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this on another posting on JavaRanch. The "com.sun.image.codec.jpeg" package came with my VisualCafe install. It runs pretty quickly.
Hope this helps. Also, I found the GIF exporter on www.acme.com, and will try it today.
Thanks for your help!
----------------------------
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class Text2Image {
public static void main(String args[]) {
Frame myFrame = null;
Graphics2D myGraphics = null;
try {
myFrame = new Frame();
myFrame.addNotify();
BufferedImage buffImage = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\waterfall.jpg"));
myGraphics = buffImage.createGraphics();
myGraphics.setFont(new Font("Serif", Font.ITALIC, 48));

myGraphics.setColor(Color.white);
myGraphics.fillRect( 2, 2, 396, 196);

myGraphics.setColor(Color.red);
myGraphics.fillRect( 10, 130, 380, 60);

myGraphics.setColor(Color.green);
myGraphics.fillRect( 10, 10, 380, 60);

myGraphics.setColor(Color.yellow);
myGraphics.fillRect( 10, 70, 380, 60);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(buffImage);
System.out.println("gets here");
bos.flush();
System.out.println("flushed");
bos.close();
System.out.println("closed");
buffImage.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("final");
if (myGraphics != null) {
myGraphics.dispose();
System.out.println("dispose");
}
if (myFrame != null) {
myFrame.removeNotify();
System.out.println("remove notify");
}
}
System.out.println("done!");
} //end of main method
} //end of Text2Image class
------------------
Tom McComb
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic