Nexus 5x reverse landscape sensor fix in a android camera preview app

前端 未结 3 1567
轮回少年
轮回少年 2020-12-03 03:51

I am kind of newbie in Android development, so my apologies in advance if my question is trivial. In one part of my app I need a live preview of my rear camera, so I created

相关标签:
3条回答
  • 2020-12-03 04:16
    if (Build.MODEL.equals("Nexus 5X")){
         // rotate camera 180°
         mCamera.setDisplayOrientation(180);
    }
    

    Change 5x to 5X will be ok.

    0 讨论(0)
  • 2020-12-03 04:30

    The 5X camera is not "reverse"; it does report the correct camera orientation in its parameters, so no special workarounds are needed, just make sure you're setting the display orientation correctly:

     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);
    }
    

    From http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29

    0 讨论(0)
  • 2020-12-03 04:36

    Same here. Since camera2 doesn't support devices < API 21, and don't want to implement the two APIs, it would be nice do get a quick fix.

    I'm trying somthing like :

    if (Build.MODEL.equals("Nexus 5x")){
         // rotate camera 180°
         mCamera.setDisplayOrientation(180);
    }
    

    But can't rotate the display of the camera. Gonna check if there is something to do with the PreviewCallback

    Is it a bad thing to use the 2 apis ? As in this topic, it is not recommended : How to use Android's camera or camera2 API to support old and new API versions without deprecation notes?

    0 讨论(0)
提交回复
热议问题