// Lock the focus as the first step for a still image capture. private void LockFocus() { try { var availableAutoFocusModes = (int[])characteristics.Get(CameraCharacteristics.ControlAfAvailableModes); // Set autofocus if supported if (availableAutoFocusModes.Any(afMode => afMode != (int)ControlAFMode.Off)) { previewRequestBuilder.Set(CaptureRequest.ControlAfTrigger, (int)ControlAFTrigger.Start); state = MediaCaptorState.WaitingLock; // Tell cameraCaptureCallback to wait for the lock. captureSession.Capture(previewRequestBuilder.Build(), cameraCaptureCallback, backgroundHandler); } else { // If autofocus is not enabled, just capture the image CaptureStillPicture(); } } catch (CameraAccessException e) { e.PrintStackTrace(); } }
private void ProcessImageCapture(CaptureResult result) { switch (state) { case MediaCaptorState.WaitingLock: { var afState = (int?)result.Get(CaptureResult.ControlAfState); if (afState == null) { CaptureStillPicture(); } else if ((((int)ControlAFState.FocusedLocked) == afState.Value) || (((int)ControlAFState.NotFocusedLocked) == afState.Value)) { // ControlAeState can be null on some devices var aeState = (int?)result.Get(CaptureResult.ControlAeState); if (aeState == null || aeState.Value == ((int)ControlAEState.Converged)) { state = MediaCaptorState.PictureTaken; CaptureStillPicture(); } else { RunPrecaptureSequence(); } } break; } case MediaCaptorState.WaitingPrecapture: { // ControlAeState can be null on some devices var aeState = (int?)result.Get(CaptureResult.ControlAeState); if (aeState == null || aeState.Value == ((int)ControlAEState.Precapture) || aeState.Value == ((int)ControlAEState.FlashRequired)) { state = MediaCaptorState.WaitingNonPrecapture; } break; } case MediaCaptorState.WaitingNonPrecapture: { // ControlAeState can be null on some devices var aeState = (int?)result.Get(CaptureResult.ControlAeState); if (aeState == null || aeState.Value != ((int)ControlAEState.Precapture)) { state = MediaCaptorState.PictureTaken; CaptureStillPicture(); } break; } } }
// Run the precapture sequence for capturing a still image. This method should be called when // we get a response in captureCallback from LockFocus(). public void RunPrecaptureSequence() { try { // This is how to tell the camera to trigger. previewRequestBuilder.Set(CaptureRequest.ControlAePrecaptureTrigger, (int)ControlAEPrecaptureTrigger.Start); // Tell captureCallback to wait for the precapture sequence to be set. state = MediaCaptorState.WaitingPrecapture; captureSession.Capture(previewRequestBuilder.Build(), cameraCaptureCallback, backgroundHandler); } catch (CameraAccessException e) { e.PrintStackTrace(); } }
void UnlockFocus() { try { // Reset the auto-focus trigger previewRequestBuilder.Set(CaptureRequest.ControlAfTrigger, (int)ControlAFTrigger.Cancel); SetAutoFlash(previewRequestBuilder); captureSession.Capture(previewRequestBuilder.Build(), cameraCaptureCallback, backgroundHandler); // After this, the camera will go back to the normal state of preview. state = MediaCaptorState.Preview; captureSession.SetRepeatingRequest(previewRequest, cameraCaptureCallback, backgroundHandler); } catch (CameraAccessException e) { e.PrintStackTrace(); } }