-1

例如,我只对一张图像进行编码以获得输出。以下是我所做的。它不工作,也没有崩溃!

@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;
        }
    }
}
4

1 回答 1

0

我可能有点晚了,但是由于您的代码有助于实现我的目标,所以我发布了我的解决方案。您的方法“fromBitmap()”就像一个魅力。

当活动处于活动状态时,我在 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();
        }
    }
于 2016-08-01T16:06:09.700 回答