在运行时更改Android Camera 2的Flash设置 [英] Changing Flash setting of Android Camera 2 at runtime
问题描述
基本上,我要做的是通过应用程序中的按钮单击更改CONTROL_AE_MODE。用户可以使用AUTO flash(ON_AUTO_FLASH),如果ON(ON_ALWAYS_FLASH)或OFF(CONTROL_AE_MODE_OFF)则关闭。
Basically, what I am trying to do is change the CONTROL_AE_MODE by button click in the app. The user can use AUTO flash(ON_AUTO_FLASH), turn if ON(ON_ALWAYS_FLASH), or OFF(CONTROL_AE_MODE_OFF).
第818行,他们设置闪光灯一次:
Line 818, they set the flash once:
// Use the same AE and AF modes as the preview.
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
setAutoFlash(captureBuilder);
// Orientation
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
CameraCaptureSession.CaptureCallback CaptureCallback
= new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
showToast("Saved: " + mFile);
Log.d(TAG, mFile.toString());
unlockFocus();
}
};
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
然后在840行构建CaptureSession。
And then builds the CaptureSession at line 840.
有没有办法在预览后更改CONTROL_AE_MODE?
Is there a way to change the CONTROL_AE_MODE after the preview is made?
我尝试重新制作会话,这有点有用:
I have tried remaking the session, which kinda worked:
if(flashMode == CameraView.CAMERA_FLASH_ON){
Log.e("CAMERA 2", "FLASH ON");
mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
}else if(flashMode == CameraView.CAMERA_FLASH_OFF){
Log.e("CAMERA 2", "FLASH OFF");
mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
}else if(flashMode == CameraView.CAMERA_FLASH_AUTO){
Log.e("CAMERA 2", "FLASH AUTO");
mPreviewCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
}
mFlashMode = flashMode;
if (mCameraCaptureSession != null) {
mCameraCaptureSession.close();
mCameraCaptureSession = null;
}
createCameraPreviewSession();
出于某种原因,CONTROL_AE_MODE_OFF会将整个预览变为黑色。
我尝试在文档中查找更新方法,但没有找到任何内容。
For some reason, CONTROL_AE_MODE_OFF would turn the whole preview black. I tried looking in the docs for methods to update but haven't found anything.
非常感谢任何教程或文档。
Any tutorials or docs is much appreciated.
推荐答案
我不喜欢不知道为什么你的预览会变黑,但你不需要手动关闭捕获会话。从 .close()
方法的文档:
I don't know why your preview turn black, but you don't need to close capture session manually. From .close()
method's docs:
使用
createCaptureSession(List,CameraCaptureSession.StateCallback,
直接不关闭是
Handler)
快速切换到新会话的推荐方法,因为未更改的目标输出可以
be更有效地重用。
Using
createCaptureSession(List , CameraCaptureSession.StateCallback, Handler)
directly without closing is the recommended approach for quickly switching to a new session, since unchanged target outputs can be reused more efficiently.
因此,您可以重用现有的CaptureRequest.Builder,设置更改后的值,构建新的PreviewRequest并启动新的会话这个新请求,如下所示:
So you can reuse existing CaptureRequest.Builder, set your changed value, build new PreviewRequest and just start new session with this new request, like this:
try {
// Change some capture settings
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
// Build new request (we can't just edit existing one, as it is immutable)
mPreviewRequest = mPreviewRequestBuilder.build();
// Set new repeating request with our changed one
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
它会更快(几乎没有任何可见的预览冻结)。
It will be much faster (almost without any visible freeze of preview).
这篇关于在运行时更改Android Camera 2的Flash设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!