Gif4J PRO for Java - Load GIF Images; Java GIF Decoder
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 PRO java gif imaging class library logo

Load & Decode a GIF Image

The GifDecoder java class lets programmers load and decode gif images from a gif file formatted input stream with a single method call. The next methods to load and decode gif images are available:

    public static final GifImage decode(InputStream inputStreamthrows IOException 
    public static final GifImage decode(File inputthrows IOException 
    public static final GifImage decode(URL inputthrows IOException 

Note: If you load GIF images for displaying purposes (for example to display as image icons within a swing application) then you should use the standard Java API: java.awt.Toolkit#getImage, java.awt.Toolkit#createImage or javax.swing.ImageIcon.

Java Example: Load a GIF Image and Extract GIF File Format Meta Info

import com.gif4j.GifDecoder;
import com.gif4j.GifFrame;
import com.gif4j.GifImage;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.util.Iterator;

// ...

      public GifImage loadGifImageAndExtractMetaInfo(File filethrows IOException {
        // load and decode gif image from the file
        GifImage gifImage = GifDecoder.decode(file);
        // print general GIF image info
        System.out.println("gif image format version: " + gifImage.getVersion());
        System.out.println("gif image logic screen width: " + gifImage.getScreenWidth());
        System.out.println("gif image logic screen height: " + gifImage.getScreenHeight());
        // check if one or more comments present
        if (gifImage.getNumberOfComments() 0) {
            // get iterator over gif image textual comments
            Iterator commentsIterator = gifImage.comments();
            while (commentsIterator.hasNext())
                System.out.println( print comments
        System.out.println("number of frames: " + gifImage.getNumberOfFrames());
        // below we iterate frames in loop
        // but it can also be done using Iterator instance: gifImage.frames()
        for (int i = 0; i < gifImage.getNumberOfFrames(); i++) {
            System.out.println("------frame(" (i + 1")---------");
            GifFrame frame = gifImage.getFrame(i);
            System.out.println("width: " + frame.getWidth());
            System.out.println("height: " + frame.getHeight());
            System.out.println("position: " + frame.getX() "," + frame.getY());
            System.out.println("disposal method: " + frame.getDisposalMethod());
            System.out.println("delay time: " + frame.getDelay());
            System.out.println("is interlaced: " + frame.isInterlaced());
            // get frame's color model
            IndexColorModel frameColorModel = frame.getColorModel();
            System.out.println("number of colors: " + frameColorModel.getMapSize());
            System.out.println("is transparent: " + frameColorModel.hasAlpha());
            System.out.println("transparent index: " + frameColorModel.getTransparentPixel());
            //get frame's representation as an Image
            Image image = frame.getAsImage();
            //get frame's representation as a BufferedImage
            BufferedImage bufferedImage = frame.getAsBufferedImage();
        return gifImage;

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