在Android Camrea2中与Flash有关的只有两个字段:
首先先说说CaptureRequest.FLAH_MODE,这个字段是可以由用户控制的,要使用它的话,还要保证CaptureRequest.CONTROL_AE_MODE的值为ON或者OFF,它有三个值:
然后再看看CaptureRequest.CONTROL_AE_MODE,用于选择当前自动曝光模式的控件:
通过CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES进行查询支持的模式。
在AE的状态中,有两个与闪光灯有关的状态:
9. AE_STAET_FLASH_REQUIRED:HAL已聚焦曝光,但认为需要启动闪光灯才能保证亮度充足。用于确定是否可使用零快门延迟帧。
10. AE_STATE_PRECATURE:HAL正在处理预拍序列。根据AE模式,该模式可能包括触发闪光灯以进行测光或发出闪光脉冲以减轻红眼现象
录像情况下,闪光灯的模式很简单,就两个:
相对于录像,拍照模式下,闪光灯的设置就多了很多
前置摄像头
对比于系统摄像机的闪光灯模式,在我们自己的APP中的设置:
对焦正常,前后摄像头切换正常
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
对焦正常,前后摄像头切换正常
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
亮光条件下:闪光灯不打开,对焦正常
暗光条件下:闪光灯不打开,对焦正常,在对焦时,闪光灯打开
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_OFF);
不要忘记设置FLASH_MODE为OFF,否则会影响闪光灯的开启。
在预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
拍照时设置:也可以不用再设置
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
拍照时设置:
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
若拍摄时,不设置,会造成拍照时闪光灯关闭。
若拍照时设置为
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
会是在拍照时闪光灯关闭。
亮光条件下:
预览时闪光灯不会打开,拍照时闪光灯不打开
暗光条件下:
预览和拍照时闪光灯打开。(没有实现出来)
预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_OFF);
拍照时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
若拍照时设置 AE_MODE_ON_AUTO_FLASH ,则拍照时闪光灯会关闭。在系统摄像头的表现上应该是闪光灯一直开启。
亮光条件下: 预览时闪光灯不会打开,拍照时闪光灯不打开
暗光条件下:预览时闪光灯不打开,对焦时闪光灯打开,拍照时闪光灯打开
预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_OFF);
拍照时设置:
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
前置摄像头:
预览时闪光灯不打开,拍照时闪光灯打开
预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
拍照时设置:
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
builder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_SINGLE);
后置摄像头:
预览时设置:
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
mCaptureRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
拍照时设置:
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
builder.set(CaptureRequest.FLASH_MODE,CaptureRequest.FLASH_MODE_TORCH);
若拍照时设置为 FLASH_MODE_SINGLE ,在后置摄像头上,第一次拍照亮,后续拍照时闪光灯不亮。
解决方案时:
用后置摄像头拍照时,设置为torch,或者统一设置为torch。
一般情况下,不需要设置这个,大多数手机不支持。