自定义相机那些坑之预览界面适配及原理

0.思维导图

自定义相机那些坑之预览界面适配及原理_第1张图片

1.问题的产生

当Activity的oritation设置为portrait时,前置/后置摄像头预览图像会逆时针旋转90度展示,如图:

2.问题产生的本质

相机图像数据来自图像感应器(物理器件)(Image Sensor),图像感应器再将数据输出到预览Window上时,图像感应器有一个读取图像的方向,并且不会改变。这个读取的方向通过CameraInfo的orientation可以获取。现在的图像感应器硬件的读取方向都是顺时针90度,即读取方向如下图:

由此就会产生预览方向逆时针旋转90度。具体的产生过程如图:

自定义相机那些坑之预览界面适配及原理_第2张图片

3.官方推荐的解决方案

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

推荐的解决方案中:
(1)CameraInfo是实例化的相机类,其中orientation是相机(图像感应器)相对于自然坐标系(即左上角坐标系)的旋转角度数.
(2)rotation是预览window的旋转的方向,当Activity的 screenOrientation="portrait"时,rotation=0,也就是没有旋转,当 screenOrientation="landscape"时,rotation=1。
(3)camera.setDisplayOrientation(int param)根据官方解释就是图片已经输出后旋转的度数。旋转值可以是 0, 90, 180, and 270. (a)当打开后置摄像头,Window竖屏显示时,预览图像旋转: result=(90-0+360)%360=90,顺时针旋转90度.
(b)当打开后置摄像头,Window横屏显示时,预览图像旋转: result=(90-90+360)%360=0,顺时针旋转0度.
(c)前置摄像头有一些特殊情况,在下面会谈到。
(d)param的参数官方解释 @param degrees the angle that the picture will be rotated clockwise.Valid values are 0, 90, 180, and 270.

4.一些存在的问题

1、对于前后摄像头,图像感应器输出的方向是一致的,并不像部分博客所说进行了镜像。
2、CameraInfo中的facing字段,无论是调用前置摄像头还是后置摄像头,facing均为0,即后置摄像头值。如下图各种机型返回值:

自定义相机那些坑之预览界面适配及原理_第3张图片

5.参考相关文档

腾讯Bugly: https://blog.csdn.net/Tencent_Bugly/article/details/53375311 http://hao.caibaojian.com/16047.html
Demo: https://github.com/xionglei12/camera-preview-demo.git

你可能感兴趣的:(相机专题)