Class GifEncoder
java.lang.Object
org.openpatch.scratch.recorder.GifEncoder
Encodes a GIF file consisting of one or more frames.
Example:
GifEncoder e = new GifEncoder();
e.start(outputFileName);
e.setDelay(1000); // 1 frame per sec
e.addFrame(argbPixels, width, height);
e.finish();
Ported from AnimatedGifEncoder (Kevin Weiner, FM Software, November 2003), with the LZW encoder
adapted from Jef Poskanzer's Java port by way of J. M. G. Elliott, and the NeuQuant neural-net
quantizer by Anthony Dekker, 1994. Uses only java.base: frames arrive as ARGB int arrays
instead of BufferedImages.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddFrame(int[] argb, int width, int height) Adds next GIF frame as an ARGB pixel array, top row first (0xAARRGGBB).booleanfinish()Flushes any pending data and closes output file.voidsetDelay(int ms) Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).voidsetDispose(int code) Sets the GIF frame disposal code for the last added frame and any subsequent frames.voidsetFrameRate(float fps) Sets frame rate in frames per second.voidsetQuality(int quality) Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification).voidsetRepeat(int iter) Sets the number of times the set of GIF frames should be played.voidsetTransparent(int rgb) Sets the transparent color for the last added frame and any subsequent frames.booleanstart(OutputStream os) Initiates GIF file creation on the given stream.booleanInitiates writing of a GIF file with the specified name.
-
Constructor Details
-
GifEncoder
public GifEncoder()
-
-
Method Details
-
setDelay
public void setDelay(int ms) Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).- Parameters:
ms- int delay time in milliseconds
-
setDispose
public void setDispose(int code) Sets the GIF frame disposal code for the last added frame and any subsequent frames. Default is 0 if no transparent color has been set, otherwise 2.- Parameters:
code- int disposal code.
-
setRepeat
public void setRepeat(int iter) Sets the number of times the set of GIF frames should be played. Default is 1; 0 means play indefinitely. Must be invoked before the first image is added.- Parameters:
iter- int number of iterations.
-
setTransparent
public void setTransparent(int rgb) Sets the transparent color for the last added frame and any subsequent frames. Since all colors are subject to modification in the quantization process, the color in the final palette for each frame closest to the given color becomes the transparent color for that frame. May be set to -1 to indicate no transparent color.- Parameters:
rgb- int color (0xRRGGBB) to be treated as transparent on display, or -1 for none.
-
addFrame
public boolean addFrame(int[] argb, int width, int height) Adds next GIF frame as an ARGB pixel array, top row first (0xAARRGGBB). The alpha channel is ignored. The frame is not written immediately, but is actually deferred until the next frame is received so that timing data can be inserted. Invokingfinish()flushes all frames. The size of the first frame is used for all subsequent frames; frames of another size are scaled with nearest-neighbour sampling.- Parameters:
argb- int array containing the frame's pixels, top row first.width- int frame width.height- int frame height.- Returns:
- true if successful.
-
finish
public boolean finish()Flushes any pending data and closes output file. If writing to an OutputStream, the stream is not closed.- Returns:
- true if finished
-
setFrameRate
public void setFrameRate(float fps) Sets frame rate in frames per second. Equivalent tosetDelay(1000/fps).- Parameters:
fps- float frame rate (frames per second)
-
setQuality
public void setQuality(int quality) Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification). Lower values (minimum = 1) produce better colors, but slow processing significantly. 10 is the default, and produces good color mapping at reasonable speeds. Values greater than 20 do not yield significant improvements in speed.- Parameters:
quality- int greater than 0.
-
start
Initiates GIF file creation on the given stream. The stream is not closed automatically.- Parameters:
os- OutputStream on which GIF images are written.- Returns:
- false if initial write failed.
-
start
Initiates writing of a GIF file with the specified name.- Parameters:
file- String containing output file name.- Returns:
- false if open or initial write failed.
-