Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ahwar/b6797f81671b2f8fb3f7cc5de3c9a5dc to your computer and use it in GitHub Desktop.
Save Ahwar/b6797f81671b2f8fb3f7cc5de3c9a5dc to your computer and use it in GitHub Desktop.
This Java code converts android's ImageProxy to Bitmap.
private Bitmap toBitmap(Image image) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer yBuffer = planes[0].getBuffer();
ByteBuffer uBuffer = planes[1].getBuffer();
ByteBuffer vBuffer = planes[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
byte[] nv21 = new byte[ySize + uSize + vSize];
//U and V are swapped
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, image.getWidth(), image.getHeight(), null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight()), 75, out);
byte[] imageBytes = out.toByteArray();
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
@gtzippy
Copy link

gtzippy commented Aug 2, 2022

I'm getting a distorted image as well. I am not trying to do any kind of analysis. I have a ton of code that works with bitmaps and I need to be able to use that code as is, but I am bringing in an ImageProxy object through React-Native and I just cannot figure out how to get it not to be distorted.

@amf-paulinho
Copy link

I am having the same problem. I created a plugin to Process Frames. In Android 12 I get distorted image, exact the same above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment