继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android 开发之解决相机预览上下颠倒问题

单刀土豆
关注TA
已关注
手记 26
粉丝 8798
获赞 71
问题的来源

最近在做一个二维码扫描功能,收到一个 bug:在 Nexus 5x 上二维码扫描预览界面上下颠倒,第一感觉应该是相机传感器的方向问题,于是 Google 了一下,果不其然:

> Because of manufacturing reasons, we needed to mount the Nexus 5X main sensor in the less-common (reverse landscape) orientation - the wires from the sensor chip wouldn't have fit otherwise. Unfortunately, our old camera API (which is deprecated, but most apps still use it) isn't terribly user-friendly, and requires application developers to explicitly set the preview rotation. On most devices, though, it turns out the default rotation is correct for a forced-landscape app, so many apps never call the display orientation method.

大意就是:由于硬件设计问题,需要把相机的主传感器芯片方面颠倒着装(真想吐槽,不过毕竟自己也没有做通用适配,还是算了吧),而老的 Camera API 需要开发者自己进行方向调整适配,但是大部分手机的默认方向就是正常的,所以很多应用开发者并没有做通用的方式适配也不会有什么问题。

后来发现其实在 Nexus 5X 刚出来时,包括微信、支付宝等国民级应用也存在二维码扫描颠倒的问题,不过现在早已经解决了,所以真的像 Google 所说很多开发者根本没有调用 setDisplayOrientation 方法去做适配。所以 Google 呼吁:
>So if you do see apps with upside-down camera preview on Nexus 5X, please let the developers know about this - Android's developer relations folks are trying to reach out to apps as we find them, and LG is also helping out, but it might take a bit before everyone's up to speed on this issue.

解决办法

其实问题并不复杂,原因也在预料之中,解决起来也很简单,之所以扯了那么多主要是觉得 Google 官方回应还是比较有意思的。现在来看解决办法:

 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);
 }
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP