ImageIOでAnimatedGifファイルを生成する
Total: 4298
, Today: 1
, Yesterday: 5
Posted by aterai at
Last-modified:
概要
ImageIO
を使用してAnimatedGif
を生成し、これを画像ファイルとして書き出します。
Screenshot
Advertisement
サンプルコード
BufferedImage image = new BufferedImage(
WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("gif");
try {
File file = File.createTempFile("anime", ".gif");
file.deleteOnExit();
ImageWriter writer = it.hasNext() ? it.next() : null;
ImageOutputStream stream = ImageIO.createImageOutputStream(file);
if (Objects.isNull(writer)) {
throw new IOException();
}
writer.setOutput(stream);
writer.prepareWriteSequence(null);
IIOMetadataNode gce = new IIOMetadataNode("GraphicControlExtension");
gce.setAttribute("disposalMethod", "none");
gce.setAttribute("userInputFlag", "FALSE");
gce.setAttribute("transparentColorFlag", "FALSE");
gce.setAttribute("transparentColorIndex", "0");
gce.setAttribute("delayTime", Objects.toString(DELAY));
IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
ae.setAttribute("applicationID", "NETSCAPE");
ae.setAttribute("authenticationCode", "2.0");
// last two bytes is an unsigned short (little endian) that
// indicates the the number of times to loop.
// 0 means loop forever.
ae.setUserObject(new byte[] {0x1, 0x0, 0x0});
IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
aes.appendChild(ae);
// Create animated GIF using imageio | Oracle Community
// https://community.oracle.com/thread/1264385
ImageWriteParam iwp = writer.getDefaultWriteParam();
IIOMetadata metadata = writer.getDefaultImageMetadata(
new ImageTypeSpecifier(image), iwp);
String metaFormat = metadata.getNativeMetadataFormatName();
Node root = metadata.getAsTree(metaFormat);
root.appendChild(gce);
root.appendChild(aes);
metadata.setFromTree(metaFormat, root);
// make frame
for (int i = 0; i < list.size() * DELAY; i++) {
paintFrame(image, list);
Collections.rotate(list, 1);
writer.writeToSequence(new IIOImage(image, null, metadata), null);
metadata = null;
}
writer.endWriteSequence();
stream.close();
String path = file.getAbsolutePath();
label.setText(path);
label.setIcon(new ImageIcon(path));
} catch (IOException ex) {
ex.printStackTrace();
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、Create animated GIF using imageio | Oracle Communityを参考にしてdelayTime
が10
ミリ秒、フレーム数が80
のAnimatedGif
を生成し、これを画像ファイルとして書き出してからJLabel
に表示しています。