Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.*;
- import javax.imageio.metadata.IIOMetadata;
- import javax.imageio.metadata.IIOMetadataNode;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Iterator;
- /**
- * @author sci4me
- */
- public final class GifMaker
- {
- private final int width, height;
- private final int imageType;
- private BufferedImage currentFrame;
- private ImageWriter gifWriter;
- private ImageWriteParam imageWriteParam;
- private IIOMetadata imageMetaData;
- public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int width, final int height) throws IOException
- {
- this(out, timeBetweenFramesMS, BufferedImage.TYPE_INT_RGB, width, height);
- }
- public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int imageType, final int width, final int height) throws IOException
- {
- this(out, timeBetweenFramesMS, imageType, true, width, height);
- }
- public GifMaker(final OutputStream out, final int timeBetweenFramesMS, final int imageType, final boolean loopContinuously, final int width, final int height) throws IOException
- {
- this.width = width;
- this.height = height;
- this.imageType = imageType;
- this.currentFrame = new BufferedImage(width, height, this.imageType);
- this.gifWriter = this.getWriter();
- this.imageWriteParam = this.gifWriter.getDefaultWriteParam();
- final ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(this.imageType);
- this.imageMetaData = this.gifWriter.getDefaultImageMetadata(imageTypeSpecifier, this.imageWriteParam);
- final String metaFormatName = this.imageMetaData.getNativeMetadataFormatName();
- final IIOMetadataNode root = (IIOMetadataNode) this.imageMetaData.getAsTree(metaFormatName);
- final IIOMetadataNode graphicsControlExtensionNode = getNode(root, "GraphicControlExtension");
- graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
- graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
- graphicsControlExtensionNode.setAttribute("transparentColorFlag", "FALSE");
- graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(timeBetweenFramesMS / 10));
- graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0");
- final IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
- commentsNode.setAttribute("CommentExtension", "Created by MAH");
- final IIOMetadataNode appEntensionsNode = getNode(root, "ApplicationExtensions");
- final IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
- child.setAttribute("applicationID", "NETSCAPE");
- child.setAttribute("authenticationCode", "2.0");
- final int loop = loopContinuously ? 0 : 1;
- child.setUserObject(new byte[]{0x1, (byte) (loop & 0xFF), (byte) ((loop >> 8) & 0xFF)});
- appEntensionsNode.appendChild(child);
- this.imageMetaData.setFromTree(metaFormatName, root);
- this.gifWriter.setOutput(ImageIO.createImageOutputStream(out));
- this.gifWriter.prepareWriteSequence(null);
- }
- public GifMaker fill(final int startX, final int startY, final int width, final int height, final int color)
- {
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- this.setPixel(startX + x, startY + y, color);
- return this;
- }
- public GifMaker setPixel(final int x, final int y, final int color)
- {
- this.currentFrame.setRGB(x, y, color);
- return this;
- }
- public GifMaker nextFrame() throws IOException
- {
- this.gifWriter.writeToSequence(new IIOImage(this.currentFrame, null, this.imageMetaData), this.imageWriteParam);
- this.currentFrame = new BufferedImage(this.width, this.height, this.imageType);
- return this;
- }
- public GifMaker write() throws IOException
- {
- this.gifWriter.endWriteSequence();
- return this;
- }
- private static ImageWriter getWriter() throws IIOException
- {
- final Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix("gif");
- if (!iter.hasNext())
- {
- throw new IIOException("No GIF Image Writers Exist");
- }
- else
- {
- return iter.next();
- }
- }
- private static IIOMetadataNode getNode(final IIOMetadataNode rootNode, final String nodeName)
- {
- final int nNodes = rootNode.getLength();
- for (int i = 0; i < nNodes; i++)
- {
- if (rootNode.item(i).getNodeName().compareToIgnoreCase(nodeName) == 0)
- {
- return ((IIOMetadataNode) rootNode.item(i));
- }
- }
- final IIOMetadataNode node = new IIOMetadataNode(nodeName);
- rootNode.appendChild(node);
- return (node);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement