Domanda

I am trying to convert images to video with jcodec. The function to get images is:

public void encodeNativeFrame(Picture pic) throws IOException

I can send to this funciton Bitmap or byte array of the same Bitmap coverted to YUV420.

My question is how to convert Bitmap to Picture or convert byte array (byte[]) to Picture.

È stato utile?

Soluzione

First, you will need to create a bitmap :

Bitmap bitmap = BitmapFactory.decodeByteArray(yourByteArray, 0, yourByteArray.length);

Then, get a canvas in which you will record =

Picture picture = new Picture();
Canvas canvas = picture.beginRecording(bitmap.getWidth(), bitmap.getHeight());

Then, draw the bitmap on your canvas

canvas.drawBitmap(bitmap, null, new RectF(0f, 0f, (float) bitmap.getWidth, (float) bitmap.getHeight), null);

Then, end the record

picture.endRecording();

Then, the picture will contain your Bitmap.

Here a method doing that :

public Picture fromByteArray(byte[] byteArray){
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    Picture picture = new Picture();
    Canvas canvas = picture.beginRecording(bitmap.getWidth(), bitmap.getHeight());
    canvas.drawBitmap(bitmap, null, new RectF(0f, 0f, (float) bitmap.getWidth, (float) bitmap.getHeight), null);
    picture.endRecording();
    return picture;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top