将保存在buffereimage中的animate -gif写入java.io.File Object



我正在阅读互联网url中的gif图像。

// URL of a sample animated gif, needs to be wrapped in try-catch block
URL imageUrl = new Url("http://4.bp.blogspot.com/-CTUfMbxRZWg/URi_3Sp-vKI/AAAAAAAAAa4/a2n_9dUd2Hg/s1600/Kei_Run.gif");
// reads the image from url and stores in BufferedImage object.
BufferedImage bImage = ImageIO.read(imageUrl);
// creates a new `java.io.File` object with image name
File imageFile = new File("download.gif");
// ImageIO writes BufferedImage into File Object
ImageIO.write(bImage, "gif", imageFile);

代码执行成功。但是,保存的图像不像源图像那样是动画的。

我已经查看了许多堆栈溢出问题/答案,但我无法通过这个。它们中的大多数都是通过BufferedImage逐帧来实现的,这改变了帧率。我不想改变源图像。我想下载它,因为它是相同的大小,相同的分辨率和相同的帧率。

请记住,我想避免使用streamsunofficial-libraries尽可能多的(如果它不能没有它们,我将使用它们)。

如果有一个替代ImageIO或我从url读取图像的方式,它得到的事情完成,请指出我在那个方向。

无需解码图像然后重新编码。

直接读取图像的字节,然后按原样将字节写入文件:

try (InputStream in = imageUrl.openStream()) {
    Files.copy(in, new File("download.gif").toPath());
}