Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<BufferedImage> boxFrames = makeTextboxAnimation(boxes); // create frames for GIF
- final byte[] data = new byte[1];
- Image image = null;
- try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) {
- ImageWriter writer = ImageIO.getImageWritersByFormatName("gif").next();
- ImageWriteParam param = writer.getDefaultWriteParam();
- IIOMetadata meta = writer.getDefaultImageMetadata(
- ImageTypeSpecifier.createFromBufferedImageType(boxFrames.get(0).getType()), param);
- String metaFormatName = meta.getNativeMetadataFormatName();
- IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(metaFormatName);
- IIOMetadataNode graphicsControl = getNode(root, "GraphicControlExtension");
- graphicsControl.setAttribute("disposalMethod", "none");
- graphicsControl.setAttribute("userInputFlag", "FALSE");
- graphicsControl.setAttribute("transparentColorFlag", "FALSE");
- graphicsControl.setAttribute("delayTime", "10");
- graphicsControl.setAttribute("transparentColorIndex", "0");
- IIOMetadataNode comments = getNode(root, "CommentExtensions");
- comments.setAttribute("CommentExtension", "Animated OneShot Textbox");
- IIOMetadataNode application = getNode(root, "ApplicationExtensions");
- IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
- child.setAttribute("applicationID", "NETSCAPE");
- child.setAttribute("authenticationCode", "2.0");
- child.setUserObject(new byte[] { 0x1, (byte) (0 & 0xFF), (byte) ((0 >> 8) & 0xFF) });
- application.appendChild(child);
- meta.setFromTree(metaFormatName, root);
- writer.setOutput(ios);
- writer.prepareWriteSequence(null);
- for (int i = 0; i < boxFrames.size(); i++) {
- BufferedImage frame = boxFrames.get(i);
- writer.writeToSequence(new IIOImage(frame, null, meta), param);
- }
- writer.endWriteSequence();
- byte[] tmp = baos.toByteArray();
- System.arraycopy(tmp, 0, data, 0, tmp.length);
- image = Toolkit.getDefaultToolkit().createImage(data);
- } catch (IOException e1) {
- e1.printStackTrace();
- JOptionPane.showMessageDialog(this, "An exception occured while generating the animation:\n" + e1,
- "Couldn't generate animation!", JOptionPane.ERROR_MESSAGE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement