2017-06-13 83 views
3

我需要在我的應用程序上使用Camera2 API。 (Api21 +) 我發現了下一個樣本: https://github.com/googlesamples/android-Camera2BasicAndroid Camera2焦點狀態陷入困境

我下載了它,並開始在我的手機上。當我按「圖片」按鈕時,其調用takePhoto方法。

private void takePicture() { 
    lockFocus(); 
} 

這是一臺狀態機。有時候這臺機器在STATE_WAITING_LOCK上停頓。 我的設備正在等待焦點,但沒有任何反應! (是的,我的設備支持自動對焦)

case STATE_WAITING_LOCK: { 
        Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); 
        if (afState == null) { 
         captureStillPicture(); 
        } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || 
          CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) { 
         // CONTROL_AE_STATE can be null on some devices 
         Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); 
         if (aeState == null || 
           aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) { 
          mState = STATE_PICTURE_TAKEN; 
          captureStillPicture(); 
         } else { 
          runPrecaptureSequence(); 
         } 
        } 
        break; 
       } 

這個問題有什麼好的解決方案? ,並將該軟件有時墜毀在這裏:

private void unlockFocus() { 
      try { 
       // Reset the auto-focus trigger 
       mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 
         CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); 
       setAutoFlash(mPreviewRequestBuilder); 
/*HERE*/ mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 
         mBackgroundHandler); 
       // After this, the camera will go back to the normal state of preview. 
       mState = STATE_PREVIEW; 
       mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, 
         mBackgroundHandler); 
      } catch (CameraAccessException e) { 
       e.printStackTrace(); 
      } 
     } 

爲什麼不能集中我的設備?

+0

我有同樣的問題。我檢查了afState的值,它始終保留在CONTROL_AF_STATE_PASSIVE_FOCUSED中,但無法達到CONTROL_AF_STATE_NOT_FOCUSED_LOCKED和CONTROL_AF_STATE_FOCUSED_LOCKED –

+0

@ketom是否找到任何解決方案? –

回答

0

看來你的程序有時會在CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED中出現問題,根據文檔「AF算法一直無法聚焦,鏡頭沒有移動」。 link

嘗試取消AF_TRIGGER並重新啓動它。事情是這樣的:

if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) { 
      getRidOfNotFocusedLock(); 
} 

和:

private void getRidOfNotFocusedLock(){ 
     try { 
      mPreviewRequestBuilder.set(
        CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL); 
      mCaptureSession.capture(
        captureRequestBuilder.build(), captureSessionCaptureCallback, backgroundHandler); 
      mPreviewRequestBuilder.set(
        CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); 

     } catch (CameraAccessException e) { 
      e.printStackTrace(); 
     } 
    } 
相關問題