autoFocus抛出exception

我已经尝试了一切,但我仍然无法解决这个问题。

我正在应用程序中实现相机function,一切正常,除了自动对焦。 当我调用autoFocus()时,它会抛出exception,我无法理解为什么。 我在Desire HD上运行代码。

码:

@Override protected void onStart() { super.onStart(); //grab seurface view and callback cameraView = (CameraSurfaceView) findViewById(R.id.cameraView); try{ camera = Camera.open(); cameraView.setCamera(camera); //release previous autofocus and assign new one camera.cancelAutoFocus(); camera.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { // TODO Auto-generated method stub }}); } catch (Exception e) { //had an issue accessing the camera prompt user //TODO create user prompt e.printStackTrace(); } } 

堆栈跟踪:

 01-11 16:09:38.456: W/System.err(26546): java.lang.RuntimeException: autoFocus failed 01-11 16:09:38.456: W/System.err(26546): at android.hardware.Camera.native_autoFocus(Native Method) 01-11 16:09:38.456: W/System.err(26546): at android.hardware.Camera.autoFocus(Camera.java:680) 01-11 16:09:38.456: W/System.err(26546): at com.myapp.MyActivity.onStart(BarcodeScannerActivity.java:57) 01-11 16:09:38.466: W/System.err(26546): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1201) 01-11 16:09:38.466: W/System.err(26546): at android.app.Activity.performStart(Activity.java:3955) 01-11 16:09:38.466: W/System.err(26546): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1845) 01-11 16:09:38.466: W/System.err(26546): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 01-11 16:09:38.466: W/System.err(26546): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 01-11 16:09:38.466: W/System.err(26546): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 01-11 16:09:38.466: W/System.err(26546): at android.os.Handler.dispatchMessage(Handler.java:99) 01-11 16:09:38.466: W/System.err(26546): at android.os.Looper.loop(Looper.java:150) 01-11 16:09:38.476: W/System.err(26546): at android.app.ActivityThread.main(ActivityThread.java:4385) 01-11 16:09:38.476: W/System.err(26546): at java.lang.reflect.Method.invokeNative(Native Method) 01-11 16:09:38.476: W/System.err(26546): at java.lang.reflect.Method.invoke(Method.java:507) 01-11 16:09:38.476: W/System.err(26546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 01-11 16:09:38.476: W/System.err(26546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 01-11 16:09:38.476: W/System.err(26546): at dalvik.system.NativeStart.main(Native Method) 

Solutions Collecting From Web of "autoFocus抛出exception"

使用SurfaceHolder.Callback – > surfaceCreated来了解何时可以启动AutoFocus。 如果未创建曲面所有者(持续一段时间),则自动对焦将失败。

您可能需要确保手机支持自动对焦。 检查这个很容易:

 Camera.Parameters p = mCamera.getParameters(); List focusModes = p.getSupportedFocusModes(); if(focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { //Phone supports autofocus! } else { //Phone does not support autofocus! } 

我提出了两个对我有用的解决方案。 1)正确停止并恢复相机。 我是通过在onPause和onResume上调用这些方法来实现的,也是在相机预览中,我在我的应用中扫描QR码:

 public void stopCamera(){ mCamera.cancelAutoFocus(); mCamera.setPreviewCallback(null); mCamera.stopPreview(); mPreviewing = false; } public void rethrottleCamera(){ updateViews(); //Updates my Layouts mPreviewing = true; mCamera.startPreview(); mCamera.setPreviewCallback(previewCb); mCamera.autoFocus(autoFocusCB); } 

2)非常棘手,但像魔术一样工作! 确保在创建预览曲面后调用自动对焦。 要执行此操作,请以200毫秒的延迟运行自动对焦,以便为要创建的曲面购买时间。 通过在“CameraPreview”对象声明上按ctrl + clic来设置此项,例如:

 CameraPreview my_camera; 

查找“public void surfaceChanged”方法并进行此更改:

 //Add a delay to AUTOFOCUS after mCamera.startpreview();!!: mCamera.startPreview(); final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { mCamera.autoFocus(autoFocusCallback); } }, 200); //< -200 millisecond delay //If you call autofocus right after startPreview, chances are, //that the previewSurface will have not been created yet, //and autofocus will fail: mCamera.startPreview(); //Bad idea! mCamera.autoFocus(autoFocusCallback); //Bad idea! 

还有很多其他修复,但这两个可能会节省你的一天。

我find了一个很好的解决方案
因此,让我们简单地捕获exception,并再次尝试在某些设备上调用自动对焦(即sony experia和其他一些设备)
什么是关于重试之间的延迟时间(1秒)
我不喜欢代码中的任何“魔法”数字,所以在某些情况下这可能太大或太小。 对我来说足够了)

 public void requestAutoFocus(Handler handler, int message) { if(camera != null && previewing) { autoFocusCallback.setHandler(handler, message); scheduleAutoFocus(); } } public void safeAutoFocus() { try { camera.autoFocus(autoFocusCallback); } catch (RuntimeException e) { // Horrible hack to deal with autofocus errors on Sony devices // See https://github.com/dm77/barcodescanner/issues/7 for example scheduleAutoFocus(); // wait 1 sec and then do check again } } private void scheduleAutoFocus() { mAutoFocusHandler.postDelayed(doAutoFocus, 1000); } private Runnable doAutoFocus = new Runnable() { public void run() { if(camera != null && previewing) { safeAutoFocus(); } } }; 

这是启动和停止方法

 public void startPreview() { if (camera != null && !previewing) { camera.startPreview(); camera.autoFocus(autoFocusCallback); previewing = true; } } public void stopPreview() { if(camera != null && previewing) { try { camera.cancelAutoFocus(); if(!useOneShotPreviewCallback) { camera.setPreviewCallback(null); } camera.stopPreview(); previewCallback.setHandler(null, 0); autoFocusCallback.setHandler(null, 0); previewing = false; } catch(Exception e) { Log.e(TAG, e.toString(), e); } } } 

参考: https : //github.com/dm77/barcodescanner/blob/master/core/src/main/java/me/dm7/barcodescanner/core/CameraPreview.java

确保在调用开始预览后调用autoFocus函数。 根据android文档

此方法仅在预览处于活动状态时(startPreview()和stopPreview()之前)有效。

如果您仍然遇到任何错误,请按相同的顺序尝试Rasmus和zwebie的解决方案。

有很多解决方案,但这是一个简单,便宜的选项,对我有用:

 try{ mCamera.autoFocus(autoFocusCB); //Or whatever part of code that crashes } catch(Exception e){ Log.v("joshtag","THIS PHONE DOES NOT SUPPORT AUTOFOCUS!!"); //a warning, popup, whatever } 

瞧! 陷阱已停用。