在Camera2 API中切换闪光灯 [英] Switch flash in Camera2 API
问题描述
我的问题是,当我在不同的闪光模式之间切换然后想要捕获图像时,我的captureBuilder不会设置所选的闪光模式.它仅在我关闭并重新打开相机后才起作用.
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.
我将 https://github.com/googlesamples/android-Camera2Basic 用作起点.
我的方法:
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);
}
这是setCurrentFlash方法:
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 *****
通过调用像Eddy Talvala建议的runPrecaptureSequence()时将闪光灯设置为PreviewRequestBuilder来解决的问题
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();
}
推荐答案
您也想在预览请求中更新Flash模式;通常,在触发预捕获序列(使用AE_PRECAPTURE_TRIGGER)时,摄像头设备希望了解所需的闪光模式,以便知道是否应打开预捕获闪光灯,这需要确定最终的闪光功率.
You want to update the flash mode in your preview request as well; generally the camera device wants to know your desired flash mode when you trigger the precapture sequence (with AE_PRECAPTURE_TRIGGER), so that it knows if it should turn on the precapture flash, which it needs to determine the final flash power.
通常的事件顺序是:
- 将预览闪光模式设置为所需模式
- 等待用户按下快门按钮
- 发出具有预捕获触发器集的单个预览请求(但保留重复的预览请求).
- 等待AE_STATE_PRECAPTURE在捕获结果中停止成为AE状态
- 发出最终捕获请求(保持相同的闪光模式)
- 在ImageReader中获取最终的JPEG
(这忽略了确保聚焦良好,通常在开始预捕获序列之前/同时完成)
(This ignores ensuring focus is good, which generally is done before/in parallel to starting the precapture sequence)
这篇关于在Camera2 API中切换闪光灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!