According to Mobile Vision API documentation, Frame
object has method getBitmap()
but it's clearly stated that
getBitmap()
Returns the bitmap which was specified in creating this frame, or null if no bitmap was used to create this frame.
If you really want to get Bitmap object, you will have to create it yourself. One options is by getGrayscaleImageData()
method on Frame
object.
If there are some bytes in returned ByteBuffer
, you can convert it to Bitmap
.
Firstly you must create YuvImage
using byte array from your getGrayscaleImageData()
result. It's a mandatory step because byte array have image in YUV/YCbCr color space, encoded in NV21 format. So first line will look like this:
YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, width, height, null);
width
and height
can be extracted from frame by getMedatada().getHeight()
/ getMedatada().getWidth()
methods.
Then you can use ByteArrayOutputStream
to quickly compress your YuvImage
object.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, byteArrayOutputStream);
From there you can convert it to byte array again to finally use it in BitmapFactory
.
byte[] jpegArray = byteArrayOutputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
I know that it's alot of code comparing to simple getBitmap()
method usage, but it will do the job if you really need bitmap in that kind of situation.