Gif4J LIGHT Java GIF Encoder - Write GIF Images
Gif4J gif animation software logotype
Home Page for Gif4J Software java gif image processing products, solutions and services Products Page for java GIF image processing software Purchase java GIF image managing products Download Gif4J GIF image processing products Java imaging FAQs and Gif4J solutions support Contact Page Customer Area
Gif4J LIGHT java gif imaging class library logo

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);
    }

Gif4J java gif imaging lib (professional)
Gif4J PRO
Overview
 
Gif4J Java Color Quantizer
Overview
Quality Measurements & Benchmark Testing
Test Online
 
Gif4J PRO Tutorial
Loading GIF Images
Creating GIF Images
Creating GIF Frames
Transform GIF Images
Saving GIF Images
Watermarking
Morphing Filters
Text Rendering
 
Gif4J java gif imaging lib (light)
Gif4J LIGHT
Overview
 
Gif4J LIGHT Tutorial
Creating GIF Images
Creating GIF Frames
Saving GIF Images
 
Home |  Privacy Policy |  Legal Information 
© Gif4J Software 2004-2017