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则拍照无声音,否则有快门声