Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

144
LINES

< > BotCompany Repo | #1003673 // class GifSequenceWriter

JavaX fragment (include)

1  
// 
2  
//  GifSequenceWriter.java
3  
//  
4  
//  Created by Elliot Kroo on 2009-04-25.
5  
//  (modified here)
6  
//
7  
8  
import javax.imageio.*;
9  
import javax.imageio.metadata.*;
10  
import javax.imageio.stream.*;
11  
import java.awt.image.*;
12  
13  
sclass GifSequenceWriter implements AutoCloseable {
14  
  protected ImageWriter gifWriter;
15  
  protected ImageWriteParam imageWriteParam;
16  
  protected IIOMetadata imageMetaData;
17  
  ImageOutputStream stream;
18  
  
19  
  /**
20  
   * Creates a new GifSequenceWriter
21  
   * 
22  
   * @param outputStream the ImageOutputStream to be written to
23  
   * @param imageType one of the imageTypes specified in BufferedImage
24  
   * @param timeBetweenFramesMS the time between frames in miliseconds
25  
   * @param loopContinuously wether the gif should loop repeatedly
26  
   * @throws IIOException if no gif ImageWriters are found
27  
   *
28  
   * @author Elliot Kroo (elliot[at]kroo[dot]net)
29  
   */
30  
  public GifSequenceWriter(
31  
      ImageOutputStream outputStream,
32  
      int imageType,
33  
      int timeBetweenFramesMS,
34  
      boolean loopContinuously) throws IIOException, IOException {
35  
    // my method to create a writer
36  
    stream = outputStream;
37  
    gifWriter = getWriter(); 
38  
    imageWriteParam = gifWriter.getDefaultWriteParam();
39  
    ImageTypeSpecifier imageTypeSpecifier =
40  
      ImageTypeSpecifier.createFromBufferedImageType(imageType);
41  
42  
    imageMetaData =
43  
      gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
44  
      imageWriteParam);
45  
46  
    String metaFormatName = imageMetaData.getNativeMetadataFormatName();
47  
48  
    IIOMetadataNode root = (IIOMetadataNode)
49  
      imageMetaData.getAsTree(metaFormatName);
50  
51  
    IIOMetadataNode graphicsControlExtensionNode = getNode(
52  
      root,
53  
      "GraphicControlExtension");
54  
55  
    graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
56  
    graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
57  
    graphicsControlExtensionNode.setAttribute(
58  
      "transparentColorFlag",
59  
      "FALSE");
60  
    graphicsControlExtensionNode.setAttribute(
61  
      "delayTime",
62  
      Integer.toString(timeBetweenFramesMS / 10));
63  
    graphicsControlExtensionNode.setAttribute(
64  
      "transparentColorIndex",
65  
      "0");
66  
67  
    IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
68  
    commentsNode.setAttribute("CommentExtension", "Created by MAH");
69  
70  
    IIOMetadataNode appEntensionsNode = getNode(
71  
      root,
72  
      "ApplicationExtensions");
73  
74  
    IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
75  
76  
    child.setAttribute("applicationID", "NETSCAPE");
77  
    child.setAttribute("authenticationCode", "2.0");
78  
79  
    int loop = loopContinuously ? 0 : 1;
80  
81  
    child.setUserObject(new byte[]{ 0x1, (byte) (loop & 0xFF), (byte)
82  
      ((loop >> 8) & 0xFF)});
83  
    appEntensionsNode.appendChild(child);
84  
85  
    imageMetaData.setFromTree(metaFormatName, root);
86  
87  
    gifWriter.setOutput(outputStream);
88  
89  
    gifWriter.prepareWriteSequence(null);
90  
  }
91  
  
92  
  public void writeToSequence(RenderedImage img) throws IOException {
93  
    gifWriter.writeToSequence(
94  
      new IIOImage(
95  
        img,
96  
        null,
97  
        imageMetaData),
98  
      imageWriteParam);
99  
  }
100  
  
101  
  /**
102  
   * Now also closes the stream
103  
   */
104  
  public void close() throws IOException {
105  
    gifWriter.endWriteSequence();    
106  
    stream.close();
107  
  }
108  
109  
  /**
110  
   * Returns the first available GIF ImageWriter using 
111  
   * ImageIO.getImageWritersBySuffix("gif").
112  
   * 
113  
   * @return a GIF ImageWriter object
114  
   * @throws IIOException if no GIF image writers are returned
115  
   */
116  
  private static ImageWriter getWriter() throws IIOException {
117  
    ret assertNotNull("No GIF Image Writers Exist",
118  
      first(ImageIO.getImageWritersBySuffix("gif")));
119  
  }
120  
121  
  /**
122  
   * Returns an existing child node, or creates and returns a new child node (if 
123  
   * the requested node does not exist).
124  
   * 
125  
   * @param rootNode the <tt>IIOMetadataNode</tt> to search for the child node.
126  
   * @param nodeName the name of the child node.
127  
   * 
128  
   * @return the child node, if found or a new node created with the given name.
129  
   */
130  
  private static IIOMetadataNode getNode(
131  
      IIOMetadataNode rootNode,
132  
      String nodeName) {
133  
    int nNodes = rootNode.getLength();
134  
    for (int i = 0; i < nNodes; i++) {
135  
      if (rootNode.item(i).getNodeName().compareToIgnoreCase(nodeName)
136  
          == 0) {
137  
        return((IIOMetadataNode) rootNode.item(i));
138  
      }
139  
    }
140  
    IIOMetadataNode node = new IIOMetadataNode(nodeName);
141  
    rootNode.appendChild(node);
142  
    return(node);
143  
  }
144  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003673
Snippet name: class GifSequenceWriter
Eternal ID of this version: #1003673/4
Text MD5: bc937f9c3739b4d63910aa35451ab9d4
Author: stefan
Category: javax / utils
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-30 13:25:13
Source code size: 4566 bytes / 144 lines
Pitched / IR pitched: No / No
Views / Downloads: 666 / 1209
Version history: 3 change(s)
Referenced in: [show references]