使用JCODEC将图像编码为视频无法正常工作

时间:2016-01-08 08:03:03

标签: android jcodec

例如,我只编码一个图像来获得输出。以下是我所做的事情。它不起作用,也没有崩溃!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {

        File wallpaperDirectory = new File("/sdcard/Wallpaper/");
        // have the object build the directory structure, if needed.
        wallpaperDirectory.mkdirs();

        File file = new File(wallpaperDirectory, "output.mp4");
        SequenceEncoder encoder = new SequenceEncoder(file);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
        encoder.encodeNativeFrame(this.fromBitmap(bitmap));

        encoder.finish();
    } catch (IOException e) {
        e.printStackTrace();
    }

}


// convert from Bitmap to Picture (jcodec native structure)
public Picture fromBitmap(Bitmap src) {
    Picture dst = Picture.create(src.getWidth(), 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可能有点晚了,但是由于你的代码有助于实现我的目标,我发布了我的解决方案。你的方法&#39; fromBitmap()&#39;像魅力一样工作。

我在活动处于活动状态时在AsyncTask中截取屏幕截图。 blog.nkdroidsolutions.com/android-screenshot-programmatically/对此非常有帮助。图像全部保存在同一文件夹中。

之后我将图像添加到视频中:

 @Override
    protected void onPostExecute(Void aVoid) {
        try {
            sequenceEncoder = new SequenceEncoder(new File(WHERE_TO_SAVE,"NAME.mp4"));

            for (int i = 1; i<9; i++) {
                File file = new File(path, "/img0000" + Integer.toString(i) + ".jpg");
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                org.jcodec.common.model.Picture picture = fromBitmap(bitmap);
                sequenceEncoder.encodeNativeFrame(picture);
            }

            sequenceEncoder.finish();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }