|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.gif4j.GifDecoder
This java class allows loading and decoding of GIF images into GifImage(-s) from input streams, files and URLs.
It can be useful if you are going to manipulate GIF image frames separately and/or extract
GIF image format specific information including image size, number of frames, frames' sizes, frames' positions, color model, transparency, embedded comments etc.
The following example demostrates the common way to read GIF image from the file and extract some GIF image meta information:
GifDecoder
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 input stream
GifImage gifImage = GifDecoder.decode(file);
// pring 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 also can by 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());
frameColorModel.getTransparency();
//get frame's representation as an Image
Image image = frame.getAsImage();
//get frame's representation as an BufferedImage
BufferedImage bufferedImage = frame.getAsBufferedImage();
}
return gifImage;
}
Afterwards you can easily encode decoded GifImage instance using GifEncoder.
The following example demostrates how you can add some comments to GIF images:
import com.gif4j.GifDecoder;
import com.gif4j.GifEncoder;
import com.gif4j.GifImage;
import java.io.File;
import java.io.IOException;
// ...
public void addCommentToGifImage(File file, String comment) throws IOException {
//load and decode gif image from the file
GifImage gifImage = GifDecoder.decode(file);
// add comment
gifImage.addComment(comment);
// save commented gif image to the same file
GifEncoder.encode(gifImage, file);
}
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: Toolkit.getImage(java.lang.String), Toolkit.createImage(byte[]) or javax.swing.ImageIcon.
The following specification "GRAPHICS INTERCHANGE FORMAT(sm) Version 89a" was referenced for the development of this java gif image decoder.
Some of the naming conventions and clarification of some aspects
of GIF file format and LZW compression were obtained from the following:
LZW.C (Copyright 1989 Mark R. Nelson)
LZW data compression/expansion demonstration program (May 12, 1997)
GifImage,
GifFrame| Method Summary | |
static GifImage |
decode(java.io.File input)
Returns a loaded and decoded GifImage which gets its gif image data from the specified File. |
static GifImage |
decode(java.io.InputStream inputStream)
Returns a loaded and decoded GifImage which gets its gif image data from the specified input stream. |
static GifImage |
decode(java.net.URL input)
Returns a loaded and decoded GifImage which gets its gif image data from the specified URL. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
public static final GifImage decode(java.io.File input)
throws java.io.IOException
GifImage which gets its gif image data from the specified File.
input - a File to read from
GifImage which gets its gif image data from the specified File
java.io.IOException - If an error occurs during reading or the specified File contains invalid gif image data
java.lang.NullPointerException - If a File is nullGifImage
public static final GifImage decode(java.io.InputStream inputStream)
throws java.io.IOException
GifImage which gets its gif image data from the specified input stream.
inputStream - the input stream to use in fetching the gif image data
GifImage which gets its gif image data from the specified input stream
java.io.IOException - If an error occurs during reading or the specified input stream contains invalid gif image data
java.lang.NullPointerException - If input stream is nullGifImage
public static final GifImage decode(java.net.URL input)
throws java.io.IOException
GifImage which gets its gif image data from the specified URL.
input - a URL to read from
GifImage which gets its gif image data from the specified URL
java.io.IOException - If an error occurs during reading or the specified URL contains invalid gif image data
java.lang.NullPointerException - If a URL is nullGifImage
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||