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 inputStream) throws IOException
public static final GifImage decode(File input) throws IOException
public static final GifImage decode(URL input) throws 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.io.File;
import java.io.IOException;
import java.util.Iterator;
// ...
public GifImage loadGifImageAndExtractMetaInfo(File file) throws 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(commentsIterator.next()); // 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;
}
|