我尝试使用ImageMagick port for Android加速gif编码。我发现的关于Magick的一切都是终端命令,但没有教程或一些关于如何以编程方式进行的演示。这是我的编码代码,但它不会创建动画gif,只是预览,但是日志告诉它,它必须是动画的并且必须包含帧:
MagickConverter#createGif:
final List<MagickImage> frames = new ArrayList<>();
for (final String bitmapPath : bitmapPaths) {
final MagickImage image = new MagickImage(new ImageInfo(bitmapPath));
final QuantizeInfo quantizeInfo = new QuantizeInfo();
quantizeInfo.setNumberColors(256);
quantizeInfo.setTreeDepth(4); // 8 is max
image.quantizeImage(quantizeInfo);
frames.add(image);
}
Timber.v("Frames: %s", frames.toString()); // first test log
final MagickImage[] images = frames.toArray(new MagickImage[frames.size()]);
final MagickImage finalGif = new MagickImage(images);
finalGif.setDelay(FRAME_FREQUENCY / 10);
finalGif.setImageFormat("gif");
finalGif.setIterations(1000); // temp, want to make infinity loops
Timber.d("Is animated: %b", finalGif.isAnimatedImage());
Timber.d("Has frames: %b, number of frames: %d", finalGif.hasFrames(),
finalGif.getNumFrames());
final ImageInfo resultInfo = new ImageInfo(path);
Timber.d("Result path: %s", resultInfo.getFileName());
resultInfo.setMagick("gif");
//finalGif.writeImage(resultInfo); // it doesn't write to the file
final byte[] img = finalGif.imageToBlob(resultInfo);
Timber.d("Image bytes count: %d", img.length);
FileUtils.store(img, path);