johnrao07
johnrao07

Reputation: 6908

Encoding Images to Video using JCODEC not working

For instance I'm only encoding one image to get an output. Below is what I'hv done. Its not working, and no crashes!

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

Upvotes: -1

Views: 2144

Answers (2)

UOK last
UOK last

Reputation: 44

fun convertImagesToVideo() {
    try {
        val output = creteRootPath()
        val enc =
            SequenceEncoder.createWithFps(NIOUtils.writableChannel(output), Rational(1, 1))
        for (bitmap in arrayOfImagesFinalImages) {
            enc.encodeNativeFrame(fromBitmaps(bitmap))
        }
        enc.finish()
    } finally {
        NIOUtils.closeQuietly(out);
    }
}

fun fromBitmaps(src: Bitmap): Picture {
    val dst: Picture = Picture.create(src.width, src.height, RGB)
    fromBitmap(src, dst)
    return dst
}

fun fromBitmap(src: Bitmap, dst: Picture) {
    val dstData: ByteArray? = dst.getPlaneData(0)
    val packed = IntArray(src.width * src.height)
    src.getPixels(packed, 0, src.width, 0, 0, src.width, src.height)
    var i = 0
    var srcOff = 0
    var dstOff = 0
    while (i < src.height) {
        var j = 0
        while (j < src.width) {
            val rgb = packed[srcOff]
            dstData?.set(dstOff, (rgb shr 16 and 0xff).toByte())
            dstData?.set(dstOff + 1, (rgb shr 8 and 0xff).toByte())
            dstData?.set(dstOff + 2, (rgb and 0xff).toByte())
            j++
            srcOff++
            dstOff += 3
        }
        i++
    }
}

Upvotes: 0

Floge
Floge

Reputation: 1

I am probably a bit late,but since your code helped to accomplish my goal I post my solution. Your methods 'fromBitmap()' work like a charm.

I take screenshots in an AsyncTask while the activity is active. blog.nkdroidsolutions.com/android-screenshot-programmatically/ was quite helpful for that. The images are all saved in the same folder.

Afterwards I add the images into a video:

 @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();
        }
    }

Upvotes: 0

Related Questions