My problem is when I switch between different flashmodes and then want to capture an image, my captureBuilder won't set the chosen flashmode. It only works when i close and reopen the camera.
I took the https://github.com/googlesamples/android-Camera2Basic as a starting point.
my method:
private void captureStillPicture() {
try {
final Activity activity = (Activity) context;
if (null == activity || null == mCameraDevice) {
return;
}
// This is the CaptureRequest.Builder that we use to take a picture.
CaptureRequest.Builder captureBuilder =
mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(mImageReader.getSurface());
// Use the same AE and AF modes as the preview.
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
setCurrentFlash(captureBuilder);
// Orientation
int rotation = activity.getWindowManager()
.getDefaultDisplay()
.getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
CameraCaptureSession.CaptureCallback captureCallback =
new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(context, "image captured", Toast.LENGTH_SHORT)
.show();
unlockFocus();
}
};
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), captureCallback, null);
} catch (CameraAccessException e) {
Log.e(this.getClass()
.getSimpleName(), e.getMessage(), e);
}
This is the setCurrentFlash method:
private void setCurrentFlash(CaptureRequest.Builder requestBuilder) {
if (mFlashSupported) {
switch (flashMode.name()) {
case FLASH_AUTO:
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
break;
case FLASH_ON:
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
break;
case FLASH_OFF:
requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
break;
default:
requestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
break;
}
}
Any ideas why the builder is not setting the flash correctly before capturing?
***** EDIT *****
Solved issue by setting the flash to previewRequestBuilder when calling runPrecaptureSequence() like Eddy Talvala suggested
private void runPrecaptureSequence() {
try {
// This is how to tell the camera to trigger.
setCurrentFlash(previewRequestBuilder);
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
// Tell #mCaptureCallback to wait for the precapture sequence to be set.
state = STATE_WAITING_PRECAPTURE;
captureSession.capture(previewRequestBuilder.build(), mCaptureCallback, backgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}