|
8 | 8 | // License. To view a copy of this license, visit |
9 | 9 | // http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative |
10 | 10 | // Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. |
11 | | -import javax.imageio.*; |
12 | | -import javax.imageio.metadata.*; |
13 | | -import javax.imageio.stream.*; |
14 | | -import java.awt.image.*; |
15 | | -import java.io.*; |
| 11 | +import java.awt.image.BufferedImage; |
| 12 | +import java.awt.image.RenderedImage; |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
16 | 16 | import java.util.Iterator; |
17 | 17 |
|
| 18 | +import javax.imageio.IIOException; |
| 19 | +import javax.imageio.IIOImage; |
| 20 | +import javax.imageio.ImageIO; |
| 21 | +import javax.imageio.ImageTypeSpecifier; |
| 22 | +import javax.imageio.ImageWriteParam; |
| 23 | +import javax.imageio.ImageWriter; |
| 24 | +import javax.imageio.metadata.IIOMetadata; |
| 25 | +import javax.imageio.metadata.IIOMetadataNode; |
| 26 | +import javax.imageio.stream.FileImageOutputStream; |
| 27 | +import javax.imageio.stream.ImageOutputStream; |
| 28 | + |
| 29 | +import com.spun.util.ObjectUtils; |
| 30 | + |
18 | 31 | public class GifSequenceWriter implements AutoCloseable |
19 | 32 | { |
20 | 33 | protected ImageWriter gifWriter; |
@@ -60,6 +73,25 @@ public GifSequenceWriter(ImageOutputStream outputStream, int imageType, int time |
60 | 73 | gifWriter.setOutput(outputStream); |
61 | 74 | gifWriter.prepareWriteSequence(null); |
62 | 75 | } |
| 76 | + static File writeAnimatedGif(File imageFile, ArrayList<BufferedImage> images, int timeBetweenFramesMS) |
| 77 | + { |
| 78 | + try (ImageOutputStream output = new FileImageOutputStream(imageFile)) |
| 79 | + { |
| 80 | + try (GifSequenceWriter writer = new GifSequenceWriter(output, images.get(0).getType(), timeBetweenFramesMS, |
| 81 | + true)) |
| 82 | + { |
| 83 | + for (BufferedImage image : images) |
| 84 | + { |
| 85 | + writer.writeToSequence(image); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + catch (Exception e) |
| 90 | + { |
| 91 | + throw ObjectUtils.throwAsError(e); |
| 92 | + } |
| 93 | + return imageFile; |
| 94 | + } |
63 | 95 | public void writeToSequence(RenderedImage img) throws IOException |
64 | 96 | { |
65 | 97 | gifWriter.writeToSequence(new IIOImage(img, null, imageMetaData), imageWriteParam); |
@@ -112,37 +144,4 @@ private static IIOMetadataNode getNode(IIOMetadataNode rootNode, String nodeName |
112 | 144 | rootNode.appendChild(node); |
113 | 145 | return (node); |
114 | 146 | } |
115 | | - /** |
116 | | - public GifSequenceWriter( |
117 | | - BufferedOutputStream outputStream, |
118 | | - int imageType, |
119 | | - int timeBetweenFramesMS, |
120 | | - boolean loopContinuously) { |
121 | | - */ |
122 | | - public static void main(String[] args) throws Exception |
123 | | - { |
124 | | - if (args.length > 1) |
125 | | - { |
126 | | - // grab the output image type from the first image in the sequence |
127 | | - BufferedImage firstImage = ImageIO.read(new File(args[0])); |
128 | | - // create a new BufferedOutputStream with the last argument |
129 | | - ImageOutputStream output = new FileImageOutputStream(new File(args[args.length - 1])); |
130 | | - // create a gif sequence with the type of the first image, 1 second |
131 | | - // between frames, which loops continuously |
132 | | - GifSequenceWriter writer = new GifSequenceWriter(output, firstImage.getType(), 1, false); |
133 | | - // write out the first image to our sequence... |
134 | | - writer.writeToSequence(firstImage); |
135 | | - for (int i = 1; i < args.length - 1; i++) |
136 | | - { |
137 | | - BufferedImage nextImage = ImageIO.read(new File(args[i])); |
138 | | - writer.writeToSequence(nextImage); |
139 | | - } |
140 | | - writer.close(); |
141 | | - output.close(); |
142 | | - } |
143 | | - else |
144 | | - { |
145 | | - System.out.println("Usage: java GifSequenceWriter [list of gif files] [output file]"); |
146 | | - } |
147 | | - } |
148 | 147 | } |
0 commit comments