java – Android:如何检测相机是否闪烁

我正在使用Android的Camera2 API,我目前希望相机在准备闪光时执行某项操作.

构建CaptureRequest时,以下行:

captureRequest.set(CaptureRequest.CONTROL_AE_MODE,CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

允许相机在低光照条件下闪光.但是,我不知道如何检测相机是否准备闪光.似乎在线关于这一特定行动的文献非常稀少.

我已经尝试在处理相机的CaptureCallback中的部分结果时检查FLASH_STATE是否在FLASH_STATE_READY中,但看起来密钥不可用 – 它一直返回null.也许我不是在正确的地方检查?

相机的CaptureCallback,如下所示(基于Google的Camera2Basic代码示例):

private CameraCaptureSession.CaptureCallback mCaptureCallback
        = new CameraCaptureSession.CaptureCallback()    {
    private void process(CaptureResult result)  {
        switch(mState)  {
            case STATE_PREVIEW: break;
            case STATE_WAITING_LOCK:
                // checking if result.get(CaptureResult.FLASH_STATE) == 
                // CaptureResult.FLASH_READY over here didn't work because
                // null was returned
                int afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                        CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
                    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                    if (aeState == null ||
                            aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                        mState = STATE_WAITING_NON_PRECAPTURE;
                        captureStillPicture();
                    } else {
                        runPrecaptureSequence();
                    }
                }
                break;
            case STATE_WAITING_PRECAPTURE:
                Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                if (aeState == null ||
                        aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
                        aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
                    mState = STATE_WAITING_NON_PRECAPTURE;
                }
                break;
            case STATE_WAITING_NON_PRECAPTURE:
                Integer aeState1 = result.get(CaptureResult.CONTROL_AE_STATE);
                if (aeState1 == null || aeState1 != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
                    mState = STATE_PICTURE_TAKEN;
                    captureStillPicture();
                }
                break;
        }
    }

    @Override
    public void onCaptureProgressed(CameraCaptureSession session,
                                    CaptureRequest request, CaptureResult partialResult) {
        super.onCaptureProgressed(session, request, partialResult);
        process(partialResult);
    }

    @Override
    public void onCaptureCompleted(CameraCaptureSession session,
                                   CaptureRequest request, TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        process(result);
    }
};
最佳答案
documentation中,您可以读取FLASH_STATE键的以下语句:

Optional – This value may be null on some devices.

基本上即使您使用Lollipop设备,也无法确定是否支持Camera2 APi的所有功能.功能有限的设备称为“受限设备”.

有关更多信息,请查看this页面.这很好地描述了它.

编辑:
要回答基本上错误的评论:
文件说:

When the camera device doesn’t have flash unit (i.e. android.flash.info.available == false), this state will always be UNAVAILABLE. Other states indicate the current flash status.

因此,如果设备上没有闪存,则FLASH_STATE也会出现并返回UNAVAILABLE.

文档说明了你必须要知道的一切.最重要的是,它取决于硬件级/硬件模式.

让我们总结一下

对于LEGACY设备

它将返回以下内容

>如果AE模式设置为ON_ALWAYS_FLASH,则为FIRED
>如果闪光模式设为TORCH,则为FIRED
>如果相机没有闪光灯,则无法使用.

当设备至少LEVEL_LIMITED时:

这些值是可能的,但不能保证它们都能正常工作:

>无法使用
>充电
>准备好了
> FIRED
>部分

转载注明原文:java – Android:如何检测相机是否闪烁 - 代码日志