java.lang.RuntimeException: autoFocus failed
在使用华为手机测试自定义相机时,一进入自定义相机界面就崩溃,异常log信息如上。其他测试机没这个问题。
代码如下:
private void doStartPreview() {
try {
if (mCamera != null) {
width = mSurfaceView.getMeasuredWidth();
height = mSurfaceView.getMeasuredHeight();
Log.e(TAG, "宽高:" + width + "," + height + ",mCamera:" + mCamera);
mParams = mCamera.getParameters();
if (screenProp == 0) {
screenProp = (float) height / (float) width;
Log.e(TAG, "screenProp:" + screenProp + "," + height + width);
}
previewSize = CameraParamUtil.getInstance().getPreviewSize(mParams
.getSupportedPreviewSizes(), 1000, screenProp);
pictureSize = CameraParamUtil.getInstance().getPictureSize(mParams
.getSupportedPictureSizes(), 1200, screenProp);
mParams.setPreviewSize(previewSize.width, previewSize.height);
mParams.setPictureSize(pictureSize.width, pictureSize.height);
Log.e(TAG, "previewSize:" + previewSize.width + previewSize.height + ",pictureSize:" + pictureSize.width + pictureSize.height);
if (CameraParamUtil.getInstance().isSupportedFocusMode(
mParams.getSupportedFocusModes(),
Camera.Parameters.FOCUS_MODE_AUTO)) {
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
if (CameraParamUtil.getInstance().isSupportedPictureFormats(mParams.getSupportedPictureFormats(),
ImageFormat.JPEG)) {
mParams.setPictureFormat(ImageFormat.JPEG);
mParams.setJpegQuality(100);
}
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);//自动对焦
mCamera.setParameters(mParams);
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.setDisplayOrientation(cameraAngle);//浏览角度
// mCamera.setPreviewCallback(this); //每一帧回调。这个要注释掉,否则会出现Camera is being used after Camera.release() was called异常
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
}
});
mCamera.startPreview();//开始预览
}
} catch (IOException e) {
e.printStackTrace();
}
}
还没有预览就进行对焦是不行的。把startPreview放在autoFocus前面。
修改如下:
// 自动对焦
// mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);//注释掉
mCamera.setParameters(mParams);
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.setDisplayOrientation(cameraAngle);//浏览角度
// mCamera.setPreviewCallback(this); //每一帧回调。这个要注释掉,否则会出现Camera is being used after Camera.release() was called异常
mCamera.startPreview();//开始预览
//自动对焦。要放在startPreview后面,否则可能报自动对焦失败异常,还没预览就进行对焦是不行的。
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
}
});
运行正常。
但是如果点击了拍照按钮,preview停止了活动,如果点击“确定”和“取消”按钮之外的区域,页面出现异常。当然这个也是华为手机出现的问题,其他测试机没有出现。
查看log:
Call autoFocus when preview is inactive (state = 1)
preview停止活动时调用了autoFocus方法。异常代码如下:
@Override
public boolean onTouchEvent(MotionEvent event) {
//对焦
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
}
});
return super.onTouchEvent(event);
}
点击屏幕时调用autoFocus方法,自动对焦。
那就在preview活动时再调用autoFocus。但是怎么判断preview是活动的呢?目前只有mCamera.stopPreview()、mCamera.startPreview()方法表明是否是活动的,没找到其他可以判断是否活动的方法。后来想了想,设置一个boolean值isPreviewActive判断是否活动。当mCamera.stopPreview()时isPreviewActive=false;当mCamera.startPreview()时isPreviewActive=true。
代码如下:
private boolean isPreviewActive=true;//preview是否是活动的。防止preview是inactive时去调用对焦产生异常。
@Override
public boolean onTouchEvent(MotionEvent event) {
if(isPreviewActive) {//preview活动时才能调用自动对焦功能
//对焦
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
}
});
}
return super.onTouchEvent(event);
}
问题解决。