flowchart TD
capturePhoto --> flashMode
flashMode{FlashMode is ON or AUTO}
flashMode == No ==> doCapture
flashMode == Yes ==> preCapture
preCapture[capture with request AE_PRECAPTURE_TRIGGER to START]
preCapture --> aeSearching
aeSearching[AE state is searching]
aeSearching --> aeConverged
aeConverged[AE state is converged or flashRequired]
aeConverged --> flashTorch
flashTorch[set flashMode to TORCH]
flashTorch --> doCapture
doCapture --> onCaptureCompleted
onCaptureCompleted --> flashSingle
flashSingle[set flashMode to SINGLE]
voidcapturePhoto(Consumer<PhotoCaptureStatus>consumer,PhotoSaveAgentimageSaveAgent,intorientation){Log.d(LOG_TAG,"takeSnapshot");if(!cameraDevice.isPresent()||!captureSession.isPresent()){Log.d(LOG_TAG,"takeSnapshot, cannot snap");consumer.accept(null);return;}parameters.setFlashRequired(previewCallback.flashRequired());finalbooleanneedPreCapture=parameters.needPreCapture();RunnableactionPostCapture=null;if(needPreCapture){actionPostCapture=()->{Log.d(LOG_TAG,"postCapture reset flash status for post capture.");parameters.applyForPreview(requestBuilder);previewCallback.setAEState(CameraParameters.Preview3AState.PICTURE_TAKEN);try{captureSession.get().setRepeatingRequest(requestBuilder.build(),previewCallback,cameraHandler);}catch(CameraAccessExceptione){e.printStackTrace();}};}finalintjpegOrientation=CameraUtils.calculateRelativeRotation(characteristics,orientation);parameters.setJpegRotation(jpegOrientation);finalPhotoStillCapturestillCapture=newPhotoStillCapture(consumer,imageSaveAgent,actionPostCapture);finalRunnableaction=()->{Log.d(LOG_TAG,"capture doCapture");try{captureSession.get().capture(stillCapture.generateRequest(cameraDevice.get(),imageSaveAgent.getOutputTarget(),parameters),stillCapture.getCaptureCallback(),cameraHandler);}catch(CameraAccessExceptione){e.printStackTrace();}};if(needPreCapture){pendingCaptureAction=action;preCapture();}else{action.run();pendingCaptureAction=null;}}privatevoidpreCapture(){Log.d(LOG_TAG,"preCapture");try{CaptureRequest.Builderbuilder=cameraDevice.get().createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);for(Surfacesurface:previewSurfaces){builder.addTarget(surface);}parameters.applyForPreCapture(builder);previewCallback.setAEState(CameraParameters.Preview3AState.WAITING_PRECAPTURE);captureSession.get().capture(builder.build(),previewCallback,cameraHandler);}catch(CameraAccessExceptione){e.printStackTrace();}}