-
-
Save Ahwar/b6797f81671b2f8fb3f7cc5de3c9a5dc to your computer and use it in GitHub Desktop.
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); | |
} |
Thanks, Its working for me
Very happy to know that
Getting distorted image like below
This is my code
public Bitmap imageProxyToBitmap(ImageProxy imageProxy) {
ImageProxy.PlaneProxy[] planeProxies = imageProxy.getPlanes();
ByteBuffer yBuffer = planeProxies[0].getBuffer();
ByteBuffer uBuffer = planeProxies[1].getBuffer();
ByteBuffer vBuffer = planeProxies[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, imageProxy.getWidth(), imageProxy.getHeight(), null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight()), 100, out);
byte[] imageBytes = out.toByteArray();
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
private void bindImageAnalysis(@NonNull ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder().setTargetAspectRatio(AspectRatio.RATIO_16_9).build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_16_9)
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888)
.build();
imageAnalysis.setAnalyzer(ActivityCompat.getMainExecutor(this), imageProxy -> {
try {
Bitmap capturedImage = CameraXUtil.imageProxyToBitmap(imageProxy);
String imageAnalysisFile = Common.saveImageToCache(capturedImage, getApplicationContext(), "test.jpg");
Log.d("imageAnalysis", imageAnalysisFile);
} catch (Exception e) {
}
});
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
cameraProvider.bindToLifecycle(this, cameraSelector, imageAnalysis, preview);
}
the same thing happened to me in my image analysis use case, I changed the resolution of the image in the image analysis use case.
try changing the aspect ratio or resolution of an image or don't change the aspect ratio and image resolution at all.
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.
I am having the same problem. I created a plugin to Process Frames. In Android 12 I get distorted image, exact the same above.
Thanks, Its working for me