Cameraview: Converting Frame.getData() to Bitmap

Created on 3 Nov 2018  路  2Comments  路  Source: natario1/CameraView

I need to convert the frames to bitmap for processing, but I'm getting null from BitmapFactory.decodeByteArray(frame.getData(), 0, frame.getData().length).

Do I have to set offset to make this working? Or what is the correct way of doing this?

frame processing question

Most helpful comment

I need to convert the frames to bitmap for processing, but I'm getting null from BitmapFactory.decodeByteArray(frame.getData(), 0, frame.getData().length).

Do I have to set offset to make this working? Or what is the correct way of doing this?

You need to do something like this

  ByteArrayOutputStream out = new ByteArrayOutputStream();
            YuvImage yuvImage = new YuvImage(frame.getData(), ImageFormat.NV21, frame.getSize().getWidth(), frame.getSize().getHeight(), null);
            yuvImage.compressToJpeg(new Rect(0, 0, frame.getSize().getWidth(), frame.getSize().getHeight()), 90, out);
            byte[] imageBytes = out.toByteArray();
            Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

All 2 comments

I need to convert the frames to bitmap for processing, but I'm getting null from BitmapFactory.decodeByteArray(frame.getData(), 0, frame.getData().length).

Do I have to set offset to make this working? Or what is the correct way of doing this?

You need to do something like this

  ByteArrayOutputStream out = new ByteArrayOutputStream();
            YuvImage yuvImage = new YuvImage(frame.getData(), ImageFormat.NV21, frame.getSize().getWidth(), frame.getSize().getHeight(), null);
            yuvImage.compressToJpeg(new Rect(0, 0, frame.getSize().getWidth(), frame.getSize().getHeight()), 90, out);
            byte[] imageBytes = out.toByteArray();
            Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

@hitec16 yes,you are right.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OLDdriver2 picture OLDdriver2  路  4Comments

Monster-king picture Monster-king  路  4Comments

therealshabi picture therealshabi  路  10Comments

HD-AD picture HD-AD  路  8Comments

gamalsebaee18 picture gamalsebaee18  路  6Comments