当前位置:   article > 正文

android.hardware.Camera入坑之旅

android.hardware.camera

1、相机预览方向适配

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

  1. public static void setCameraDisplayOrientation(Activity activity,
  2. int cameraId, android.hardware.Camera camera) {
  3. android.hardware.Camera.CameraInfo info =
  4. new android.hardware.Camera.CameraInfo();
  5. android.hardware.Camera.getCameraInfo(cameraId, info);
  6. int rotation = activity.getWindowManager().getDefaultDisplay()
  7. .getRotation();
  8. int degrees = 0;
  9. switch (rotation) {
  10. case Surface.ROTATION_0: degrees = 0; break;
  11. case Surface.ROTATION_90: degrees = 90; break;
  12. case Surface.ROTATION_180: degrees = 180; break;
  13. case Surface.ROTATION_270: degrees = 270; break;
  14. }
  15. int result;
  16. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  17. result = (info.orientation + degrees) % 360;
  18. result = (360 - result) % 360; // compensate the mirror
  19. } else { // back-facing
  20. result = (info.orientation - degrees + 360) % 360;
  21. }
  22. camera.setDisplayOrientation(result);
  23. }

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、快门声音

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46147?site
推荐阅读
相关标签
  

闽ICP备14008679号