Android 从图像列表制作动画视频 [英] Android make animated video from list of images

查看:21
本文介绍了Android 从图像列表制作动画视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在两个图像之间应用过渡动画来从图像列表制作动画视频.我在 SO 上发现了许多类似的问题,

I want to make animated video from list of images by applying transition animation between two images. I found many similar questions on SO like,

Android 屏幕截图或从图像制作视频

Android-如何制作视频使用 sd 卡中的一组图像?

所有类似的 SO 问题都建议为此使用动画,但是我们如何将该动画图像存储到视频文件中?是否有任何 Android 库支持此功能来制作图像视频?

All similar SO questions suggest to used animation for that, but how can we store that animated images to video file? Is there any Android library support this facility to make video of images?

推荐答案

Android 不支持 AWT 的 BufferedBitmap 和 AWTUtil,即 Java SE.目前带有SequenceEncoder的方案已经集成到jcodec的Android版本中.您可以从包 org.jcodec.api.SequenceEncoder 中使用它.

Android do not support for AWT's BufferedBitmap nor AWTUtil, that is for Java SE. Currently the solution with SequenceEncoder has been integrated into jcodec's Android version. You can use it from package org.jcodec.api.SequenceEncoder.

这是使用 jcodec 从一系列位图生成 MP4 文件的解决方案:

Here is the solution for generating MP4 file from series of Bitmaps using jcodec:

try {
    File file = this.GetSDPathToFile("", "output.mp4");
    SequenceEncoder encoder = new SequenceEncoder(file);

    // only 5 frames in total
    for (int i = 1; i <= 5; i++) {
        // getting bitmap from drawable path
        int bitmapResId = this.getResources().getIdentifier("image" + i, "drawable", this.getPackageName());
        Bitmap bitmap = this.getBitmapFromResources(this.getResources(), bitmapResId);
        encoder.encodeNativeFrame(this.fromBitmap(bitmap));
    }
    encoder.finish();
} catch (IOException e) {
    e.printStackTrace();
}

// get full SD path
File GetSDPathToFile(String filePatho, String fileName) {
    File extBaseDir = Environment.getExternalStorageDirectory();
    if (filePatho == null || filePatho.length() == 0 || filePatho.charAt(0) != '/')
        filePatho = "/" + filePatho;
    makeDirectory(filePatho);
    File file = new File(extBaseDir.getAbsoluteFile() + filePatho);
    return new File(file.getAbsolutePath() + "/" + fileName);// file;
}

// convert from Bitmap to Picture (jcodec native structure)
public Picture fromBitmap(Bitmap src) {
    Picture dst = Picture.create((int)src.getWidth(), (int)src.getHeight(), ColorSpace.RGB);
    fromBitmap(src, dst);
    return dst;
}

public void fromBitmap(Bitmap src, Picture dst) {
    int[] dstData = dst.getPlaneData(0);
    int[] packed = new int[src.getWidth() * src.getHeight()];

    src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());

    for (int i = 0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) {
        for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) {
            int rgb = packed[srcOff];
            dstData[dstOff]     = (rgb >> 16) & 0xff;
            dstData[dstOff + 1] = (rgb >> 8) & 0xff;
            dstData[dstOff + 2] = rgb & 0xff;
        }
    }
}

如果您需要更改 fps,您可以自定义 SequenceEncoder.

In case you need to change the fps, you may customize the SequenceEncoder.

这篇关于Android 从图像列表制作动画视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆