如何从 Android 中的 JPEG 创建动画 GIF(开发)
我正在寻找一种在原生 Android 应用程序中创建动画 GIF 的简单方法.源文件应为 JPEG(来自相机或其他格式),输出应在设备上保存为 GIF.
I am looking for a simple way to create an animated GIF in a native Android application. The source files should be JPEG (from camera or what ever) and the output should be saved as GIF on the device.
我不想知道如何播放动画或动画 GIF 文件.
要清楚:我想知道如何将单个图像逐帧放入电影"中,然后将其另存为 .gif 文件.
To be clear: I want to know how to put single images frame by frame into a "movie" and then save it as a .gif file.
例如这个应用可以做我想做的事.
e.g. This App can do want I want to do.
查看此解决方案.
https://github.com/nbadal/android-gif-encoder
这是这篇文章的 Android 版本.
It's an Android version of this post.
http://www.jappit.com/blog/2008/12/04/j2me-animated-gif-encoder/
要使用这个类,这里有一个示例帮助器方法来生成 GIF 字节数组.请注意,getBitmapArray() 函数是一种一次性返回图像适配器中所有位图文件的方法.所以输入是一个适配器中的所有位图文件,输出是一个可以写入文件的字节数组.
To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.
public byte[] generateGIF() {
ArrayList<Bitmap> bitmaps = adapter.getBitmapArray();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
for (Bitmap bitmap : bitmaps) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
要使用此功能,请执行以下操作,然后您可以将文件保存到 SD 卡中.
To use this function, do the following then you can save the file into SDcard.
FileOutputStream outStream = null;
try{
outStream = new FileOutputStream("/sdcard/generate_gif/test.gif");
outStream.write(generateGIF());
outStream.close();
}catch(Exception e){
e.printStackTrace();
}