Home » Android » android – Camerainfo.orientation gives wrong answer

android – Camerainfo.orientation gives wrong answer

Posted by: admin May 14, 2020 Leave a comment

Questions:

Android provides this code to make a camera preview appear face-up for all camera and screen orientations:

public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
     int degrees = 0;
     switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }

In the simplest case, when the screen is face-up in portrait (rotation is 0) with a back-facing camera, this reduces to result = info.orientation. According to the docs, info.orientation is

the angle that the camera image needs to be rotated
clockwise so it shows correctly on the display in its natural
orientation. It should be 0, 90, 180, or 270.

This means that info.orientation should be the amount the raw images from the camera are rotated counter-clockwise.

On a Samsung Galaxy S II’s back-facing camera, info.orientation is 90, as expected for back-facing cameras. However, the raw camera preview is rotated 90 degrees clockwise, not counter-clockwise. This means that after applying the above code, the preview appears upside-down (180 degrees).

Why is there a 180 degree discrepancy between the camera’s reported orientation and the actual orientation of its raw images?

This exact problem – camera images being 90 degrees clockwise when info.orientation reports 90 – has also been reported in this question and this one.

Also see this possibly related question.

How to&Answers: