Advertisement
sci4me

GifMaker

Jun 20th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. import javax.imageio.*;
  2. import javax.imageio.metadata.IIOMetadata;
  3. import javax.imageio.metadata.IIOMetadataNode;
  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.util.Iterator;
  8.  
  9. /**
  10.  * @author sci4me
  11.  */
  12. public final class GifMaker
  13. {
  14.     private final int width, height;
  15.     private final int imageType;
  16.     private BufferedImage currentFrame;
  17.  
  18.     private ImageWriter gifWriter;
  19.     private ImageWriteParam imageWriteParam;
  20.     private IIOMetadata imageMetaData;
  21.  
  22.     public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int width, final int height) throws IOException
  23.     {
  24.         this(out, timeBetweenFramesMS, BufferedImage.TYPE_INT_RGB, width, height);
  25.     }
  26.  
  27.     public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int imageType, final int width, final int height) throws IOException
  28.     {
  29.         this(out, timeBetweenFramesMS, imageType, true, width, height);
  30.     }
  31.  
  32.     public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int imageType, final boolean loopContinuously, final int width, final int height) throws IOException
  33.     {
  34.         this.width = width;
  35.         this.height = height;
  36.         this.imageType = imageType;
  37.         this.currentFrame = new BufferedImage(width, height, this.imageType);
  38.  
  39.         this.gifWriter = this.getWriter();
  40.         this.imageWriteParam = this.gifWriter.getDefaultWriteParam();
  41.         final ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(this.imageType);
  42.  
  43.         this.imageMetaData = this.gifWriter.getDefaultImageMetadata(imageTypeSpecifier, this.imageWriteParam);
  44.  
  45.         final String metaFormatName = this.imageMetaData.getNativeMetadataFormatName();
  46.  
  47.         final IIOMetadataNode root = (IIOMetadataNode) this.imageMetaData.getAsTree(metaFormatName);
  48.  
  49.         final IIOMetadataNode graphicsControlExtensionNode = getNode(root, "GraphicControlExtension");
  50.  
  51.         graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
  52.         graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
  53.         graphicsControlExtensionNode.setAttribute("transparentColorFlag", "FALSE");
  54.         graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(timeBetweenFramesMS / 10));
  55.         graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0");
  56.  
  57.         final IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
  58.         commentsNode.setAttribute("CommentExtension", "Created by MAH");
  59.  
  60.         final IIOMetadataNode appEntensionsNode = getNode(root, "ApplicationExtensions");
  61.  
  62.         final IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
  63.  
  64.         child.setAttribute("applicationID", "NETSCAPE");
  65.         child.setAttribute("authenticationCode", "2.0");
  66.  
  67.         final int loop = loopContinuously ? 0 : 1;
  68.  
  69.         child.setUserObject(new byte[]{0x1, (byte) (loop & 0xFF), (byte) ((loop >> 8) & 0xFF)});
  70.         appEntensionsNode.appendChild(child);
  71.  
  72.         this.imageMetaData.setFromTree(metaFormatName, root);
  73.         this.gifWriter.setOutput(ImageIO.createImageOutputStream(out));
  74.         this.gifWriter.prepareWriteSequence(null);
  75.     }
  76.  
  77.     public GifMaker fill(final int startX, final int startY, final int width, final int height, final int color)
  78.     {
  79.         for (int x = 0; x < width; x++)
  80.             for (int y = 0; y < height; y++)
  81.                 this.setPixel(startX + x, startY + y, color);
  82.  
  83.         return this;
  84.     }
  85.  
  86.     public GifMaker setPixel(final int x, final int y, final int color)
  87.     {
  88.         this.currentFrame.setRGB(x, y, color);
  89.         return this;
  90.     }
  91.  
  92.     public GifMaker nextFrame() throws IOException
  93.     {
  94.         this.gifWriter.writeToSequence(new IIOImage(this.currentFrame, null, this.imageMetaData), this.imageWriteParam);
  95.         this.currentFrame = new BufferedImage(this.width, this.height, this.imageType);
  96.         return this;
  97.     }
  98.  
  99.     public GifMaker write() throws IOException
  100.     {
  101.         this.gifWriter.endWriteSequence();
  102.         return this;
  103.     }
  104.  
  105.     private static ImageWriter getWriter() throws IIOException
  106.     {
  107.         final Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix("gif");
  108.         if (!iter.hasNext())
  109.         {
  110.             throw new IIOException("No GIF Image Writers Exist");
  111.         }
  112.         else
  113.         {
  114.             return iter.next();
  115.         }
  116.     }
  117.  
  118.     private static IIOMetadataNode getNode(final IIOMetadataNode rootNode, final String nodeName)
  119.     {
  120.         final int nNodes = rootNode.getLength();
  121.         for (int i = 0; i < nNodes; i++)
  122.         {
  123.             if (rootNode.item(i).getNodeName().compareToIgnoreCase(nodeName) == 0)
  124.             {
  125.                 return ((IIOMetadataNode) rootNode.item(i));
  126.             }
  127.         }
  128.         final IIOMetadataNode node = new IIOMetadataNode(nodeName);
  129.         rootNode.appendChild(node);
  130.         return (node);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement