java.lang.Object
org.miaixz.bus.extra.image.gif.AnimatedGifEncoder
Encodes a sequence of images into an animated GIF.
AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outputFileName); e.setDelay(1000); // 1 frame per sec e.addFrame(image1); e.addFrame(image2); e.finish();
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ColorThe background color, if specified.protected booleanFlag to close the stream when finished.protected intThe number of bit planes.protected byte[]The RGB color palette.protected intFrame delay in hundredths of a second.protected intThe disposal code (-1 means use default).protected booleanFlag for the first frame.protected intThe height of the image frames.protected BufferedImageThe current frame image.protected byte[]The frame's pixels indexed to the palette.protected OutputStreamThe output stream to write the GIF to.protected intThe color table size (in bits - 1).protected byte[]BGR byte array from the current frame.protected intThe repeat setting, where 0 means infinite loop.protected intThe default sample interval for the quantizer.protected booleanFlag indicating if the size has been set.protected booleanFlag indicating if the encoder is ready to output frames.protected intThe index of the transparent color in the color table.protected ColorThe transparent color, if specified.protected booleanDetermines if the transparent color match should be exact.protected boolean[]A boolean array indicating which palette entries are active.protected intThe width of the image frames. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds next GIF frame.protected voidAnalyzes image colors and creates color map.protected intfindClosest(Color c) Returns index of palette color closest to cprotected intReturns index of palette exactly matching to color c or -1 if there is no exact matching.booleanfinish()Flushes any pending data and closes output file.protected voidExtracts image pixels into byte array "pixels"booleanReturns whether the encoding process has started.voidSets the background color for the last added frame and any subsequent frames.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.voidsetSize(int w, int h) Sets the GIF frame size.voidSets the transparent color for the last added frame and any subsequent frames.voidsetTransparent(Color c, boolean exactMatch) 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.protected voidWrites Graphic Control Extensionprotected voidWrites Image Descriptorprotected voidwriteLSD()Writes Logical Screen Descriptorprotected voidWrites Netscape application extension to define repeat count.protected voidWrites color tableprotected voidEncodes and writes pixel dataprotected voidwriteShort(int value) Write 16-bit value to output stream, LSB firstprotected voidWrites string to output stream
-
Field Details
-
width
protected int widthThe width of the image frames. -
height
protected int heightThe height of the image frames. -
transparent
The transparent color, if specified. -
transparentExactMatch
protected boolean transparentExactMatchDetermines if the transparent color match should be exact. -
background
The background color, if specified. -
transIndex
protected int transIndexThe index of the transparent color in the color table. -
repeat
protected int repeatThe repeat setting, where 0 means infinite loop. -
delay
protected int delayFrame delay in hundredths of a second. -
started
protected boolean startedFlag indicating if the encoder is ready to output frames. -
out
The output stream to write the GIF to. -
image
The current frame image. -
pixels
protected byte[] pixelsBGR byte array from the current frame. -
indexedPixels
protected byte[] indexedPixelsThe frame's pixels indexed to the palette. -
colorDepth
protected int colorDepthThe number of bit planes. -
colorTab
protected byte[] colorTabThe RGB color palette. -
usedEntry
protected boolean[] usedEntryA boolean array indicating which palette entries are active. -
palSize
protected int palSizeThe color table size (in bits - 1). -
dispose
protected int disposeThe disposal code (-1 means use default). -
closeStream
protected boolean closeStreamFlag to close the stream when finished. -
firstFrame
protected boolean firstFrameFlag for the first frame. -
sizeSet
protected boolean sizeSetFlag indicating if the size has been set. If false, size is taken from the first frame. -
sample
protected int sampleThe default sample interval for the quantizer.
-
-
Constructor Details
-
AnimatedGifEncoder
public AnimatedGifEncoder()
-
-
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
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 null to indicate no transparent color.- Parameters:
c- Color to be treated as transparent on display.
-
setTransparent
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. If exactMatch is set to true, transparent color index is search with exact match, and not looking for the closest one. May be set to null to indicate no transparent color.- Parameters:
c- Color to be treated as transparent on display.exactMatch- whether to look for an exact match or the closest color.
-
setBackground
Sets the background 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 background color for that frame. May be set to null to indicate no background color which will default to black.- Parameters:
c- Color to be treated as background on display.
-
addFrame
Adds next GIF frame. 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. IfsetSizewas not invoked, the size of the first image is used for all subsequent frames.- Parameters:
im- BufferedImage containing frame to write.- 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 successful.
-
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.
-
setSize
public void setSize(int w, int h) Sets the GIF frame size. The default size is the size of the first frame added if this method is not invoked.- Parameters:
w- int frame width.h- int frame width.
-
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.
-
isStarted
public boolean isStarted()Returns whether the encoding process has started.- Returns:
- true if the encoding has started, false otherwise.
-
analyzePixels
protected void analyzePixels()Analyzes image colors and creates color map. -
findClosest
Returns index of palette color closest to c- Parameters:
c- The color to find the closest match for.- Returns:
- The index of the closest color in the palette.
-
findExact
Returns index of palette exactly matching to color c or -1 if there is no exact matching.- Parameters:
c- The color to find an exact match for.- Returns:
- The index of the exact color in the palette, or -1 if not found.
-
getImagePixels
protected void getImagePixels()Extracts image pixels into byte array "pixels" -
writeGraphicCtrlExt
Writes Graphic Control Extension- Throws:
IOException- if an I/O error occurs.
-
writeImageDesc
Writes Image Descriptor- Throws:
IOException- if an I/O error occurs.
-
writeLSD
Writes Logical Screen Descriptor- Throws:
IOException- if an I/O error occurs.
-
writeNetscapeExt
Writes Netscape application extension to define repeat count.- Throws:
IOException- if an I/O error occurs.
-
writePalette
Writes color table- Throws:
IOException- if an I/O error occurs.
-
writePixels
Encodes and writes pixel data- Throws:
IOException- if an I/O error occurs.
-
writeShort
Write 16-bit value to output stream, LSB first- Parameters:
value- The integer value to write.- Throws:
IOException- if an I/O error occurs.
-
writeString
Writes string to output stream- Parameters:
s- The string to write.- Throws:
IOException- if an I/O error occurs.
-