 |
Encode and Save a GIF Image
The GifEncoder java class
lets programmers encode and save images out to a file or output stream
using the GIF file format (GIF89a version) with a single method call.
The next methods to encode and save images as GIFs are available (please consult the Gif4J LIGHT Java API for more info):
public static final void encode(Image image, DataOutput dataOutput)
throws IOException;
public static final void encode(Image image, File output)
throws IOException;
public static final void encode(Image image, OutputStream outputStream)
throws IOException;
public static final void encode(GifImage gifImage, File output)
throws IOException;
public static final void encode(GifImage gifImage, File output,
boolean forceGlobalColorTableUsage)
throws IOException;
public static final void encode(GifImage gifImage, OutputStream outputStream)
throws IOException;
public static final void encode(GifImage gifImage, OutputStream outputStream,
boolean forceGlobalColorTableUsage)
throws IOException;
The first three methods can be used to encode BufferedImage(s) directly to non-animated (consists of one frame) gif image files.
If the specified BufferedImage contains more than 256 unique colors then it'll be automatically quantized using the Gif4J
Java Color Quantizer (light edition).
Parameter forceGlobalColorTableUsage forces Global Color Table use. If this parameter is set to true then
Local Color Tables from all frames will be union to one Global Color Table.
It's useful to optimize final image size (every Local Color Table takes up to 768 bytes).
Especially this option is recommended to set for encoding gif images with lots of internal frames.
Gif4J LIGHT Java Example: Save Image as a GIF to a file
import com.gif4j.light.GifEncoder;
import com.gif4j.light.GifFrame;
import com.gif4j.light.GifImage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
// ...
public void saveImageAsGif(Image image, String comment, File fileToSave)
throws IOException, InterruptedException {
// create new GifImage
GifImage gifImage = new GifImage();
// create new GifFrame
GifFrame gifFrame = new GifFrame(image);
// add GifFrame to GifImage
gifImage.addGifFrame(gifFrame);
// add our comment
gifImage.addComment(comment);
// save gifImage
GifEncoder.encode(gifImage, fileToSave);
}
Gif4J LIGHT Java Example: Save array of images as an animated GIF to a file
import com.gif4j.light.GifEncoder;
import com.gif4j.light.GifFrame;
import com.gif4j.light.GifImage;
import java.awt.*;
import java.io.File;
import java.io.IOException;
// ...
void saveImageArrayAsAnimatedGif(Image[] images, File fileToSave)
throws IOException, InterruptedException {
// create new GifImage instance
GifImage gifImage = new GifImage();
// set default delay between gif frames
gifImage.setDefaultDelay(200);
// set infinite looping (by default only 1 looping iteration is set)
gifImage.setLoopNumber(0);
// add comment to gif image
gifImage.addComment("Animated GIF image example");
// add images wrapped by GifFrame
for (int i = 0; i < images.length; i++){
GifFrame nextFrame = new GifFrame(images[i]);
// clear logic screen after every frame
nextFrame.setDisposalMethod(
GifFrame.DISPOSAL_METHOD_RESTORE_TO_BACKGROUND_COLOR);
gifImage.addGifFrame(nextFrame);
}
// save animated gif image
GifEncoder.encode(gifImage, fileToSave);
}
|  |