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

橙三吉。 提交于 2019-11-27 13:19:13

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

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

Change 5x to 5X will be ok.

Soac

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!