Package com.luna.common.img.gif
Class AnimatedGifEncoder
java.lang.Object
com.luna.common.img.gif.AnimatedGifEncoder
动态GIF动画生成器,可生成一个或多个帧的GIF。
Example: AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outputFileName); e.setDelay(1000); // 1 frame per sec e.addFrame(image1); e.addFrame(image2); e.finish();
来自:https://github.com/rtyley/animated-gif-lib-for-java
- Since:
- 5.3.8
- Version:
- 1.03 November 2003
- Author:
- Kevin Weiner, FM Software
-
Field Summary
Modifier and TypeFieldDescriptionprotected Color
protected boolean
protected int
protected byte[]
protected int
protected int
protected boolean
protected int
protected BufferedImage
protected byte[]
protected OutputStream
protected int
protected byte[]
protected int
protected int
protected boolean
protected boolean
protected int
protected Color
protected boolean
protected boolean[]
protected int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds next GIF frame.protected void
Analyzes image colors and creates color map.protected int
findClosest
(Color c) Returns index of palette color closest to cprotected int
Returns index of palette exactly matching to color c or -1 if there is no exact matching.boolean
finish()
Flushes any pending data and closes output file.protected void
Extracts image pixels into byte array "pixels"boolean
void
Sets the background color for the last added frame and any subsequent frames.void
setDelay
(int ms) 设置每一帧的间隔时间 Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).void
setDispose
(int code) Sets the GIF frame disposal code for the last added frame and any subsequent frames.void
setFrameRate
(float fps) Sets frame rate in frames per second.void
setQuality
(int quality) Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification).void
setRepeat
(int iter) Sets the number of times the set of GIF frames should be played.void
setSize
(int w, int h) Sets the GIF frame size.void
Sets the transparent color for the last added frame and any subsequent frames.void
setTransparent
(Color c, boolean exactMatch) Sets the transparent color for the last added frame and any subsequent frames.boolean
start
(OutputStream os) Initiates GIF file creation on the given stream.boolean
Initiates writing of a GIF file with the specified name.protected void
Writes Graphic Control Extensionprotected void
Writes Image Descriptorprotected void
writeLSD()
Writes Logical Screen Descriptorprotected void
Writes Netscape application extension to define repeat count.protected void
Writes color tableprotected void
Encodes and writes pixel dataprotected void
writeShort
(int value) Write 16-bit value to output stream, LSB firstprotected void
Writes string to output stream
-
Field Details
-
width
protected int width -
height
protected int height -
transparent
-
transparentExactMatch
protected boolean transparentExactMatch -
background
-
transIndex
protected int transIndex -
repeat
protected int repeat -
delay
protected int delay -
started
protected boolean started -
out
-
image
-
pixels
protected byte[] pixels -
indexedPixels
protected byte[] indexedPixels -
colorDepth
protected int colorDepth -
colorTab
protected byte[] colorTab -
usedEntry
protected boolean[] usedEntry -
palSize
protected int palSize -
dispose
protected int dispose -
closeStream
protected boolean closeStream -
firstFrame
protected boolean firstFrame -
sizeSet
protected boolean sizeSet -
sample
protected int sample
-
-
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
- 间隔时间,单位毫秒
-
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
- If exactMatch is set to true, transparent color index is search with exact match
-
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. IfsetSize
was 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:
- is ok
-
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() -
analyzePixels
protected void analyzePixels()Analyzes image colors and creates color map. -
findClosest
Returns index of palette color closest to c- Parameters:
c
- Color- Returns:
- index
-
findExact
Returns index of palette exactly matching to color c or -1 if there is no exact matching.- Parameters:
c
- Color- Returns:
- index
-
getImagePixels
protected void getImagePixels()Extracts image pixels into byte array "pixels" -
writeGraphicCtrlExt
Writes Graphic Control Extension- Throws:
IOException
- IO异常
-
writeImageDesc
Writes Image Descriptor- Throws:
IOException
- IO异常
-
writeLSD
Writes Logical Screen Descriptor- Throws:
IOException
- IO异常
-
writeNetscapeExt
Writes Netscape application extension to define repeat count.- Throws:
IOException
- IO异常
-
writePalette
Writes color table- Throws:
IOException
- IO异常
-
writePixels
Encodes and writes pixel data- Throws:
IOException
- IO异常
-
writeShort
Write 16-bit value to output stream, LSB first- Parameters:
value
- 16-bit value- Throws:
IOException
- IO异常
-
writeString
Writes string to output stream- Parameters:
s
- String- Throws:
IOException
- IO异常
-