2011-03-18 128 views
12

我想从BufferedImages集创建一个gif图像。我怎样才能做到这一点?纯Java中是否有这样的库(ImageMagick不是一个选项)?我找到了Gif4J库,但它不是免费的。如何在Java中创建动画gif?

+0

尝试此[GIF编码器( http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm)。 **相关问题:** - [使用imageio创建动画图片](http://stackoverflow.com/questions/777947/creating-animated-gif-with-imageio) – Emil 2011-03-18 08:59:39

+0

还有这个http:// elliot。 kroo.net/software/java/GifSequenceWriter/这似乎工作得很好。 – 2014-05-01 06:47:47

+0

[This](https://github.com/dragon66/icafe)链接和那里的wiki例子会做你想做的。您也可以控制帧速率。这是纯粹的Java。你甚至可能不需要使用imageio! - dragon66 14年9月19日19:47 – dragon66 2015-10-28 20:04:21

回答

4

我只是回答类似的问题here,但我认为我的解决方案可以提供帮助。

'ImageIcon'类允许您加载gif动画。我用'getResource()'加载图像。为此,我通常使用URL类来传递文件路径。名称URL可能表示该路径不需要在远程计算机上使用。

URL url = This.class.getResource(path); 
Icon myImgIcon = new ImageIcon(url); 
JLabel imageLbl = new JLabel(myImgIcon); 
component.add(imageLbl, BorderLayout.CENTER); 

path将是类文件夹内gif的路径。

参考文献: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

0

有使用由Lifelogger- Glide Docs提到的非常相同AnimatedGifEncoder类的图像处理库,类似于PicassoGlide

AnimatedGifEncoder e = new AnimatedGifEncoder(); 
e.start(outputFileName); 
e.setDelay(1000); // 1 frame per sec 
e.addFrame(image1); 
e.addFrame(image2); 
e.finish();