Mobile Vision API new detector frame get Bitmap Image
Asked Answered
S

1

7

I know similar Question has been asked Before:- Mobile Vision API - concatenate new detector object to continue frame processing

I am receiving the frame but when I call frame.getBitmap() it returns a null object. I want to use this bitmap in https://github.com/tzutalin/dlib-android-app (Android dlib Library) FaceDet function.

Sixteenth answered 27/4, 2017 at 3:33 Comment(1)
Do you have any solutions?Circumstantiality
O
1

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.

Oneiric answered 23/10, 2017 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.