save Animated GIF - Java 2D Graphics

Java examples for 2D Graphics:GIF

Description

save Animated GIF

Demo Code


import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.OutputStream;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;
import org.w3c.dom.Node;

public class Main{
    public static void saveAnimatedGIF(final OutputStream out,
            final List<GifFrame> frames, final int loopCount)
            throws Exception {
        final ImageWriter iw = ImageIO.getImageWritersByFormatName("gif")
                .next();//from w  ww . ja  v a  2  s  . com

        final ImageOutputStream ios = ImageIO.createImageOutputStream(out);
        iw.setOutput(ios);
        iw.prepareWriteSequence(null);

        int p = 0;
        for (final GifFrame frame : frames) {
            final ImageWriteParam iwp = iw.getDefaultWriteParam();
            final IIOMetadata metadata = iw.getDefaultImageMetadata(
                    new ImageTypeSpecifier(frame.img), iwp);
            GifUtil.configureGIFFrame(metadata,
                    String.valueOf(frame.delay / 10L), p++,
                    frame.disposalMethod, loopCount);
            final IIOImage ii = new IIOImage(frame.img, null, metadata);
            iw.writeToSequence(ii, null);
        }

        iw.endWriteSequence();
        ios.close();
    }
    private static void configureGIFFrame(final IIOMetadata meta,
            final String delayTime, final int imageIndex,
            final String disposalMethod, final int loopCount) {
        final String metaFormat = meta.getNativeMetadataFormatName();

        if (!"javax_imageio_gif_image_1.0".equals(metaFormat)) {
            throw new IllegalArgumentException(
                    "Unfamiliar gif metadata format: " + metaFormat);
        }

        final Node root = meta.getAsTree(metaFormat);

        Node child = root.getFirstChild();
        while (child != null) {
            if ("GraphicControlExtension".equals(child.getNodeName())) {
                break;
            }
            child = child.getNextSibling();
        }

        final IIOMetadataNode gce = (IIOMetadataNode) child;
        gce.setAttribute("userDelay", "FALSE");
        gce.setAttribute("delayTime", delayTime);
        gce.setAttribute("disposalMethod", disposalMethod);

        if (imageIndex == 0) {
            final IIOMetadataNode aes = new IIOMetadataNode(
                    "ApplicationExtensions");
            final IIOMetadataNode ae = new IIOMetadataNode(
                    "ApplicationExtension");
            ae.setAttribute("applicationID", "NETSCAPE");
            ae.setAttribute("authenticationCode", "2.0");
            final byte[] uo = new byte[] { 0x1, (byte) (loopCount & 0xFF),
                    (byte) (loopCount >> 8 & 0xFF) };
            ae.setUserObject(uo);
            aes.appendChild(ae);
            root.appendChild(aes);
        }

        try {
            meta.setFromTree(metaFormat, root);
        } catch (final IIOInvalidTreeException e) {
            throw new Error(e);
        }
    }
}

Related Tutorials