How can I save Frame graphics as jpeg image?
Created May 14, 2012
The basic idea is to print the GUI to an offscreen image, then encode that image as a JPEG.
import sun.awt.image.codec.JPEGImageEncoderImpl;
import java.io.ByteArrayOutputStream;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
...
Frame f = ...; // create your GUI
BufferedImage awtImage = new BufferedImage(f.getWidth(),f.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = awtImage.getGraphics();
f.printAll(g);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(out);
j.encode(awtImage);
out.close();
}
catch (Exception e) {
e.printStackTrace();
}