关于android摄像头预览的问题

爱喝油茶的包子 2016-05-11 11:03:38
如果一个程序用到摄像头预览的话,我在程序的manifest中设置android:screenOrientation="sensorLandscape" 后,程序会进行横屏的自动旋转,但是当屏幕旋转后,摄像头预览的图像并不能进行旋转,这个该怎么解决? 求大神呀!!!!!
...全文
187 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 yueqinglkong 的回复:
预览旋转 是要配置Camera参数吧 应该是这个 你试试看
谢谢 已经解决了 我使用了OrientationEventListener 接口来实现了
  • 打赏
  • 举报
回复
引用 4 楼 jklwan 的回复:
在setDisplayOrientation方法的注释中,官方其实已经给出了代码了

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);
      }
谢谢 已经解决了
jklwan 2016-05-11
  • 打赏
  • 举报
回复
摄像头预览需要对camera进行旋转。
jklwan 2016-05-11
  • 打赏
  • 举报
回复
在setDisplayOrientation方法的注释中,官方其实已经给出了代码了

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);
      }
蒲锦_up 2016-05-11
  • 打赏
  • 举报
回复
预览旋转 是要配置Camera参数吧

应该是这个 你试试看
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
摄像头预览需要对camera进行旋转。
什么意思 能具体一些吗?我设置了setDisplayOrientation

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧