Module bus.extra

Class AnimatedGifEncoder

java.lang.Object
org.miaixz.bus.extra.image.gif.AnimatedGifEncoder

public class AnimatedGifEncoder extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    protected Color
    The background color, if specified.
    protected boolean
    Flag to close the stream when finished.
    protected int
    The number of bit planes.
    protected byte[]
    The RGB color palette.
    protected int
    Frame delay in hundredths of a second.
    protected int
    The disposal code (-1 means use default).
    protected boolean
    Flag for the first frame.
    protected int
    The height of the image frames.
    protected BufferedImage
    The current frame image.
    protected byte[]
    The frame's pixels indexed to the palette.
    protected OutputStream
    The output stream to write the GIF to.
    protected int
    The color table size (in bits - 1).
    protected byte[]
    BGR byte array from the current frame.
    protected int
    The repeat setting, where 0 means infinite loop.
    protected int
    The default sample interval for the quantizer.
    protected boolean
    Flag indicating if the size has been set.
    protected boolean
    Flag indicating if the encoder is ready to output frames.
    protected int
    The index of the transparent color in the color table.
    protected Color
    The transparent color, if specified.
    protected boolean
    Determines if the transparent color match should be exact.
    protected boolean[]
    A boolean array indicating which palette entries are active.
    protected int
    The width of the image frames.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Adds next GIF frame.
    protected void
    Analyzes image colors and creates color map.
    protected int
    Returns index of palette color closest to c
    protected int
    Returns index of palette exactly matching to color c or -1 if there is no exact matching.
    boolean
    Flushes any pending data and closes output file.
    protected void
    Extracts image pixels into byte array "pixels"
    boolean
    Returns whether the encoding process has started.
    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
    Initiates GIF file creation on the given stream.
    boolean
    start(String file)
    Initiates writing of a GIF file with the specified name.
    protected void
    Writes Graphic Control Extension
    protected void
    Writes Image Descriptor
    protected void
    Writes Logical Screen Descriptor
    protected void
    Writes Netscape application extension to define repeat count.
    protected void
    Writes color table
    protected void
    Encodes and writes pixel data
    protected void
    writeShort(int value)
    Write 16-bit value to output stream, LSB first
    protected void
    Writes string to output stream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • width

      protected int width
      The width of the image frames.
    • height

      protected int height
      The height of the image frames.
    • transparent

      protected Color transparent
      The transparent color, if specified.
    • transparentExactMatch

      protected boolean transparentExactMatch
      Determines if the transparent color match should be exact.
    • background

      protected Color background
      The background color, if specified.
    • transIndex

      protected int transIndex
      The index of the transparent color in the color table.
    • repeat

      protected int repeat
      The repeat setting, where 0 means infinite loop.
    • delay

      protected int delay
      Frame delay in hundredths of a second.
    • started

      protected boolean started
      Flag indicating if the encoder is ready to output frames.
    • out

      protected OutputStream out
      The output stream to write the GIF to.
    • image

      protected BufferedImage image
      The current frame image.
    • pixels

      protected byte[] pixels
      BGR byte array from the current frame.
    • indexedPixels

      protected byte[] indexedPixels
      The frame's pixels indexed to the palette.
    • colorDepth

      protected int colorDepth
      The number of bit planes.
    • colorTab

      protected byte[] colorTab
      The RGB color palette.
    • usedEntry

      protected boolean[] usedEntry
      A boolean array indicating which palette entries are active.
    • palSize

      protected int palSize
      The color table size (in bits - 1).
    • dispose

      protected int dispose
      The disposal code (-1 means use default).
    • closeStream

      protected boolean closeStream
      Flag to close the stream when finished.
    • firstFrame

      protected boolean firstFrame
      Flag for the first frame.
    • sizeSet

      protected boolean sizeSet
      Flag indicating if the size has been set. If false, size is taken from the first frame.
    • sample

      protected int sample
      The 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

      public void setTransparent(Color c)
      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

      public void setTransparent(Color c, boolean exactMatch)
      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

      public void setBackground(Color c)
      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

      public boolean addFrame(BufferedImage im)
      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. Invoking finish() flushes all frames. If setSize 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:
      true if successful.
    • setFrameRate

      public void setFrameRate(float fps)
      Sets frame rate in frames per second. Equivalent to setDelay(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

      public boolean start(OutputStream os)
      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

      public boolean start(String file)
      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

      protected int findClosest(Color c)
      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

      protected int findExact(Color c)
      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

      protected void writeGraphicCtrlExt() throws IOException
      Writes Graphic Control Extension
      Throws:
      IOException - if an I/O error occurs.
    • writeImageDesc

      protected void writeImageDesc() throws IOException
      Writes Image Descriptor
      Throws:
      IOException - if an I/O error occurs.
    • writeLSD

      protected void writeLSD() throws IOException
      Writes Logical Screen Descriptor
      Throws:
      IOException - if an I/O error occurs.
    • writeNetscapeExt

      protected void writeNetscapeExt() throws IOException
      Writes Netscape application extension to define repeat count.
      Throws:
      IOException - if an I/O error occurs.
    • writePalette

      protected void writePalette() throws IOException
      Writes color table
      Throws:
      IOException - if an I/O error occurs.
    • writePixels

      protected void writePixels() throws IOException
      Encodes and writes pixel data
      Throws:
      IOException - if an I/O error occurs.
    • writeShort

      protected void writeShort(int value) throws IOException
      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

      protected void writeString(String s) throws IOException
      Writes string to output stream
      Parameters:
      s - The string to write.
      Throws:
      IOException - if an I/O error occurs.