Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera preview is upside down

I have an application which uses the camera of an android device. The activity which takes the picture can not be rotated, it is displayed only in portrait. On most devices this code works fine:

int degrees = 0;
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 (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
             result = (cameraInfo.orientation + degrees) % 360;
             result = (360 - result) % 360;  // compensate the mirror
         } else {  // back-facing
             result = (cameraInfo.orientation - degrees + 360) % 360;
         } camera.setDisplayOrientation(result);

but on a device (DMTECH 725H, a 7" tablet, with only a front camera) the preview is displayed upside down. Any ideas how to fix it?

like image 293
user3382127 Avatar asked Nov 01 '22 03:11

user3382127


1 Answers

The reason of wrong camera rotation can be in unusual orientation of sensor, as mentioned in this support issue. And you also should use camera2 package instead of android.hardware.Camera.

like image 142
Konstantin Konopko Avatar answered Nov 12 '22 10:11

Konstantin Konopko