How can I create animated gif in Java?

℡╲_俬逩灬. 提交于 2019-11-27 17:42:24

问题


I would like to create a gif image from the set of BufferedImages. How can I do this? Is there such library in pure Java (ImageMagick is not an option)? I've found Gif4J library but it's not royality-free.


回答1:


I just answer a similar question here, but I think that my solution can help.

'ImageIcon' class allows you to load gif animations. I load the image with 'getResource()'. For doing this I normally us URL class to pass the file path. The path does not need to be necessary in a remote machine as the name URL may suggest.

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

path will be the path of the gif inside of the class folder.

References: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource




回答2:


There is an image processing library, akin to Picasso which uses the very same AnimatedGifEncoder class mentioned by Lifelogger- Glide Docs, Glide

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


来源:https://stackoverflow.com/questions/5349750/how-can-i-create-animated-gif-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!