Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ImageProxy return wrong getRotationDegrees

I have normal portrait orientation in the emulator and int rotation has zero value. Everything is fine.


void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {

        int rotation = cameraView.getDisplay().getRotation(); // 0

        // Preview
        Preview preview = new Preview.Builder()
                .setTargetAspectRatio(AspectRatio.RATIO_4_3)
                .build();

        // Camera
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        // Create image capture
        imageCapture = new ImageCapture.Builder()
                .setTargetResolution(new Size(1200, 720))
                .setTargetRotation(rotation)
                .build();

        // ViewPort
        Rational aspectRatio = new Rational(cameraView.getWidth(), cameraView.getHeight());
        ViewPort viewPort = new ViewPort.Builder(aspectRatio, rotation).build();

        // Use case
        UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
                .addUseCase(preview)
                .addUseCase(imageCapture)
                .setViewPort(viewPort)
                .build();

    }

But after in imageCapture.takePicture callback

            public void onCaptureSuccess(@NonNull ImageProxy image) {

                imageRotationDegrees = image.getImageInfo().getRotationDegrees(); // 90

            }

imageRotationDegrees return 90! means that the image must be rotated to get the natural orientation, but it is not! Its value should be 0.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it normal?

>Solution :

Yes, it is normal for the imageRotationDegrees value to be different from the value of rotation in the bindPreview method.

The rotation value represents the rotation of the device’s screen, while the imageRotationDegrees value represents the orientation of the image as captured by the camera. These values can be different because the camera and the screen are not necessarily oriented in the same way.

For example, if the device is in portrait orientation with the camera facing the user, the rotation value will be 0, but the imageRotationDegrees value will be 90 because the camera is capturing the image rotated 90 degrees from the device’s portrait orientation.

To get the natural orientation of the image, you can rotate the image by the value of imageRotationDegrees. For example, if you are using the Android’s Bitmap class to represent the image, you can use the rotate method to rotate the image by the correct amount:

Bitmap rotatedImage = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading