android.hardware.Camera入坑之旅
阿新 • • 發佈:2019-02-10
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則拍照無聲音,否則有快門聲