|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.gif4j.light.GifEncoder
GifEncoder is a java class which takes as image(-s), encodes and saves it(them) out to a file or output stream
using the GIF file format (GIF89a version).
It can be used to save Image instances as non-animated GIF image files
or prepared from the images sequence GifImage instances as animated GIF image files.
If an encoding Image instance has more than 256 colors
then before writing it as a GIF to the specified output stream it's automatically quantized to 256 unique colors.
GifEncoder java class is thread safe and can be used without addittional synchronization.
The following 2 java examples demostrate common ways to use this java gif image encoder. Please peruse the API, tutorial and external examples for additional features.
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);
}
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 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);
}
Note: After encoding a GifImage instance and its' internal GifFrame(-s) are disposed
to free memory and can't be reused!
GifImage,
Image| Method Summary | |
static void |
encode(GifImage gifImage,
java.io.File output)
Encode and write out the data contained in the GifImage to a
File in the GIF output format ('89a' version). |
static void |
encode(GifImage gifImage,
java.io.File output,
boolean forceGlobalColorTableUsage)
Encode and write out the data contained in the GifImage to a
File in the GIF output format ('89a' version). |
static void |
encode(GifImage gifImage,
java.io.OutputStream outputStream)
Encode and write out the data contained in the GifImage to the specified
output stream in the GIF image file format ('89a' version). |
static void |
encode(GifImage gifImage,
java.io.OutputStream outputStream,
boolean forceGlobalColorTableUsage)
Encode and write out the data contained in the GifImage to the specified
output stream in the GIF file format ('89a' version). |
static void |
encode(java.awt.Image image,
java.io.DataOutput dataOutput)
Encode and write out the data contained in the Image to the
specified output in the GIF89a file format. |
static void |
encode(java.awt.Image image,
java.io.File output)
Encode and write out the data contained in the Image to a
File in the GIF output format ('89a' version). |
static void |
encode(java.awt.Image image,
java.io.OutputStream outputStream)
Encode and write out the data contained in the Image to the
output stream in the GIF89a file format. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
public static final void encode(GifImage gifImage,
java.io.OutputStream outputStream)
throws java.io.IOException
GifImage to the specified
output stream in the GIF image file format ('89a' version).
gifImage - GifImage to encode and write outoutputStream - the specified stream to output
java.io.IOException - If write operation fails
java.lang.NullPointerException - If gifImage is null
java.lang.NullPointerException - If output stream is null
public static final void encode(GifImage gifImage,
java.io.OutputStream outputStream,
boolean forceGlobalColorTableUsage)
throws java.io.IOException
GifImage to the specified
output stream in the GIF file format ('89a' version).
gifImage - GifImage to encode and write outoutputStream - the specified stream to outputforceGlobalColorTableUsage - force Global Color Table usage. If true 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).
java.io.IOException - If write operation fails
java.lang.NullPointerException - If GifImage is null
java.lang.NullPointerException - If output stream is null
public static final void encode(GifImage gifImage,
java.io.File output)
throws java.io.IOException
GifImage to a
File in the GIF output format ('89a' version). If
there is already a File present, its contents are
discarded.
gifImage - GifImage to encode and write outoutput - a File to be written to.
java.io.IOException - If write operation fails
java.lang.NullPointerException - If GifImage is null
java.lang.NullPointerException - If output is null
public static final void encode(GifImage gifImage,
java.io.File output,
boolean forceGlobalColorTableUsage)
throws java.io.IOException
GifImage to a
File in the GIF output format ('89a' version). If
there is already a File present, its contents are
discarded.
gifImage - GifImage to encode and write outoutput - a File to be written to.forceGlobalColorTableUsage - force Global Color Table usage. If true 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).
java.io.IOException - If write operation fails
java.lang.NullPointerException - If GifImage is null
java.lang.NullPointerException - If output is null
public static final void encode(java.awt.Image image,
java.io.OutputStream outputStream)
throws java.io.IOException
Image to the
output stream in the GIF89a file format. Running this method is equal
to running encode(com.gif4j.light.GifImage gifImage, java.io.OutputStream outputStream) where
GifImage instance contains the only one GifFrame instance wrapped the specified image.
image - image to encodeoutputStream - the specified stream to output
java.io.IOException - If write operation fails
java.lang.NullPointerException - If image is null
java.lang.NullPointerException - If output stream is null
public static final void encode(java.awt.Image image,
java.io.DataOutput dataOutput)
throws java.io.IOException
Image to the
specified output in the GIF89a file format.
image - image to encodedataOutput - the specified output to write
java.io.IOException - If write operation fails
java.lang.NullPointerException - If image is null
java.lang.NullPointerException - If output is null
public static final void encode(java.awt.Image image,
java.io.File output)
throws java.io.IOException
Image to a
File in the GIF output format ('89a' version). If
there is already a File present, its contents are
discarded.
image - image to encode and write outoutput - a File to be written to.
java.io.IOException - If write operation fails
java.lang.NullPointerException - If GifImage is null
java.lang.NullPointerException - If output is null
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||