2017-06-13 5 views
3

Ich muss Camera2 API auf meiner Anwendung verwenden. (Api21 +) Ich fand das nächste Beispiel: https://github.com/googlesamples/android-Camera2BasicAndroid Camera2 Fokuszustand stucked

Ich habe es heruntergeladen und begann auf meinem Handy. Wenn ich den "Picture" Button drücke, ruft das die takePhoto Methode auf.

private void takePicture() { 
    lockFocus(); 
} 

Es ist eine Zustandsmaschine. Manchmal steckte diese Maschine auf STATE_WAITING_LOCK. Mein Gerät wartet auf Fokus, aber nichts passiert! (Ja, mein Gerät unterstützt Autofokus)

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; 
       } 

Was ist die gute Lösung für dieses Problem? Und dieses Programm abgestürzt manchmal hier:

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(); 
      } 
     } 

Warum kann mein Gerät nicht konzentrieren?

+0

Ich habe das gleiche Problem. Ich habe den Wert von afState überprüft, er bleibt immer in CONTROL_AF_STATE_PASSIVE_FOCUSED, kann aber weder CONTROL_AF_STATE_NOT_FOCUSED_LOCKED noch CONTROL_AF_STATE_FOCUSED_LOCKED erreichen. –

+0

@ketom Haben Sie eine Lösung gefunden? –

Antwort

0

Es scheint, dass Ihr Programm manchmal in CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED hängt, wo laut Dokumentation "Der AF-Algorithmus konnte nicht fokussieren. Das Objektiv bewegt sich nicht." link

Versuchen Sie, den AF_TRIGGER abzubrechen und starten Sie ihn erneut. Etwas wie folgt aus:

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

Und:

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(); 
     } 
    }