android.hardware.Camera入坑之旅

1、相机预览方向适配

可以参考谷歌官方适配方案

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

2、相机对焦时挂掉

相机在非活动时对焦失败,要在mCamera.startPreview()候才可以调用对焦的方法,保险做法是设置一个boolean值isPreviewActive判断是否活动。当mCamera.stopPreview()时isPreviewActive=false;当mCamera.startPreview()时isPreviewActive=true,对焦时先判断这个标志位。
08-08 16:54:59.357   723 25306 E Camera2Client: autoFocus: Camera 0: Call autoFocus when preview is inactive (state = 1).
08-08 16:54:59.358  5205  5205 W System.err: java.lang.RuntimeException: autoFocus failed
08-08 16:54:59.358  5205  5205 W System.err:     at android.hardware.Camera.native_autoFocus(Native Method)
08-08 16:54:59.358  5205  5205 W System.err:     at android.hardware.Camera.autoFocus(Camera.java:1402)

3、自动对焦方式

代码设置了Camera.Parameters.FOCUS_MODE_AUTO,自动对焦效果并不好(相机模糊后并不会自动再次对焦),可以使用

Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE或者Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO来进行自动连续对焦,使用自动连续对焦需要调用mCamera.cancelAutoFocus()停止手动对焦
 

4、快门声音

 public final void takePicture(ShutterCallback shutter, PictureCallback raw,
            PictureCallback postview, PictureCallback jpeg) 

调用takePicture拍照时,第一个参数为null则拍照无声音,否则有快门声

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值