Advertisement
Leo40Bin

Untitled

Sep 10th, 2017
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. List<BufferedImage> boxFrames = makeTextboxAnimation(boxes); // create frames for GIF
  2. final byte[] data = new byte[1];
  3. Image image = null;
  4. try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  5.         ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) {
  6.     ImageWriter writer = ImageIO.getImageWritersByFormatName("gif").next();
  7.     ImageWriteParam param = writer.getDefaultWriteParam();
  8.     IIOMetadata meta = writer.getDefaultImageMetadata(
  9.             ImageTypeSpecifier.createFromBufferedImageType(boxFrames.get(0).getType()), param);
  10.     String metaFormatName = meta.getNativeMetadataFormatName();
  11.     IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(metaFormatName);
  12.     IIOMetadataNode graphicsControl = getNode(root, "GraphicControlExtension");
  13.     graphicsControl.setAttribute("disposalMethod", "none");
  14.     graphicsControl.setAttribute("userInputFlag", "FALSE");
  15.     graphicsControl.setAttribute("transparentColorFlag", "FALSE");
  16.     graphicsControl.setAttribute("delayTime", "10");
  17.     graphicsControl.setAttribute("transparentColorIndex", "0");
  18.     IIOMetadataNode comments = getNode(root, "CommentExtensions");
  19.     comments.setAttribute("CommentExtension", "Animated OneShot Textbox");
  20.     IIOMetadataNode application = getNode(root, "ApplicationExtensions");
  21.     IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
  22.     child.setAttribute("applicationID", "NETSCAPE");
  23.     child.setAttribute("authenticationCode", "2.0");
  24.     child.setUserObject(new byte[] { 0x1, (byte) (0 & 0xFF), (byte) ((0 >> 8) & 0xFF) });
  25.     application.appendChild(child);
  26.     meta.setFromTree(metaFormatName, root);
  27.     writer.setOutput(ios);
  28.     writer.prepareWriteSequence(null);
  29.     for (int i = 0; i < boxFrames.size(); i++) {
  30.         BufferedImage frame = boxFrames.get(i);
  31.         writer.writeToSequence(new IIOImage(frame, null, meta), param);
  32.     }
  33.     writer.endWriteSequence();
  34.     byte[] tmp = baos.toByteArray();
  35.     System.arraycopy(tmp, 0, data, 0, tmp.length);
  36.     image = Toolkit.getDefaultToolkit().createImage(data);
  37. } catch (IOException e1) {
  38.     e1.printStackTrace();
  39.     JOptionPane.showMessageDialog(this, "An exception occured while generating the animation:\n" + e1,
  40.             "Couldn't generate animation!", JOptionPane.ERROR_MESSAGE);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement