takePicture failed after autofocus for the second time

扶醉桌前 提交于 2019-12-04 09:23:30

I propose two solutions that worked for me. 1) Stop and Resume the camera correctly. I do it by calling these methods on onPause and onResume, also in the middle of the camera Preview, where I scan QR codes in my app:

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)Very tricky but worked like magic! Make sure that you call autofocus AFTER the preview surface has been created. To do this run Autofocus with a 200ms delay, to buy time for the surface to be created. Set this by pressing ctrl+clic over a "CameraPreview" object declaration, such as:

CameraPreview my_camera;

Look for the "public void surfaceChanged" method and make this changes:

//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!

There are plenty other fixes, but these two may save your day.

Yes, the code crashes when you autofocus on the camera twice. You can workaround this issue, by having a flag, which will check if the autofocus has started, and then skip the execution of the autofocus again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!