Android Camera2 每次拍照前触发闪光灯打闪的实现

如下内容方便有Camera2基础人查看,如不了解Camera2的人不建议查看。

拍照流程
先执行CaptureRequest.CONTROL_AF_TRIGGER_START锁定AF,然后根据回调不断判断AF状态,伪代码实现如下

Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (afState == null) {
    //--执行拍照`
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
    //--判断AE状态
}

判断AF状态是lock或者unlock之后还需要在进一步判断AE状态是否收敛,然后在执行拍照。AF状态lock或unlock后判断AE状态判断伪代码如下

if (afState == null) {
    `//--执行拍照`
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
    if (aeState == null ||
        aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
        //--执行拍照
    } else {
        //--AE未收敛,主动执行AE_trigger_start
    }
}

如果AE未收敛则主动触发一次AE操作执行CONTROL_AE_PRECAPTURE_TRIGGER_START,然后根据Capture回调判断AE状态,伪代码如下:

Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null ||
    aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
    aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
    //--锁定AE
}

AE收敛完成,为了确保场景亮度变化自动触发AE,这里需要锁定AE,伪代码如下

final CaptureRequest.Builder captureBuilder;
try {
    captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    captureBuilder.addTarget(mPreviewSurface);
    setAutoFlash(captureBuilder);
    //--锁定AE
    captureBuilder.set(CaptureRequest.CONTROL_AE_LOCK, Boolean.TRUE);
    try {
        mCaptureSession.setRepeatingRequest(captureBuilder.build(), mCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
        Log.d("ArMn", "--------STATE_WATTING_LOCK_AE--------Excepture" );
    }
} catch (CameraAccessException e) {
    e.printStackTrace();
        }

//---监听AE LOCK状态
if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_LOCKED) {
    //--------执行拍照
}

在AF和AE都满足拍照条件后,执行拍照。如上是拍照中AF和AE状态处理流程,也是确保图片质量的关键点。

现在我们讨论下如何在每次拍照前触发一次闪光灯
在每次拍照前触发闪光灯需要遵循多个条件

  • 条件一
    AE_MODE需要设置成CONTROL_AE_MODE_ON_ALWAYS_FLASH
  • 条件二
    FLASH_MODE需要设置成FLASH_MODE_SINGLE

完整代码实现如下:

按下拍照按钮

private void takePicture() {
    lockFocus();
}

锁AF(如果在锁定AF之前设置了CONTROL_AE_REGIONS,则AF LOCK会触发打闪)

private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the lock.
        mState = STATE_WAITING_LOCK;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

判断AF状态

case STATE_WAITING_LOCK: {
    Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
    Log.d("ArMn", "--------- STATE_WAITING_LOCK-------afState: " + afState);
    if (afState == null) {
        captureStillPicture();
    } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
            CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
        // CONTROL_AE_STATE can be null on some devices
        Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
        if (aeState == null ||
                aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
            mState = STATE_PICTURE_TAKEN;
            Log.d("ArMn", "--------captureStillPicture--------");
            captureStillPicture();
        } else {
            Log.d("ArMn", "--------runPrecaptureSequence--------");
              runPrecaptureSequence();
        }
    }
    break;
}

执行AE打闪(如果AF没有触发打闪,则此处会触发打闪)

private void runPrecaptureSequence() {
    try {
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
        captureBuilder.addTarget(mImageReader.getSurface());
        setAutoFlash(captureBuilder);
        captureBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
        // This is how to tell the camera to trigger.
        // Tell #mCaptureCallback to wait for the precapture sequence to be set.
        mState = STATE_WAITING_PRECAPTURE;
        mCaptureSession.capture(captureBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

判断AE打闪状态

case STATE_WAITING_PRECAPTURE: {
    // CONTROL_AE_STATE can be null on some devices
    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
    Log.d("ArMn", "--------STATE_WAITING_PRECAPTURE--------aeState: " + aeState);
    if (aeState == null ||
            aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
            aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
        mState = STATE_WATTING_LOCK_AE;
    }
    break;

锁定AE状态

    private void lockAe() {
        mState = STATE_WAITING_NON_PRECAPTURE;
        final CaptureRequest.Builder captureBuilder;
        try {
            captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            captureBuilder.addTarget(mPreviewSurface);
            setAutoFlash(captureBuilder);
            captureBuilder.set(CaptureRequest.CONTROL_AE_LOCK, Boolean.TRUE);
            try {
                mCaptureSession.setRepeatingRequest(captureBuilder.build(), mCaptureCallback, mBackgroundHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
                Log.d("ArMn", "--------STATE_WATTING_LOCK_AE--------Excepture" );
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

判断AE锁定状态

case STATE_WAITING_NON_PRECAPTURE: {
    // CONTROL_AE_STATE can be null on some devices
    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
    Log.d("ArMn", "--------captureStillPicture-- STATE_WAITING_NON_PRECAPTURE ------state: " + aeState);
    if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_LOCKED) {
        mState = STATE_PICTURE_TAKEN;
        captureStillPicture();
    }
    break;
}

执行拍照

private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final 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);
        setAutoFlash(captureBuilder);
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

注意: 不要在锁定AE的时候下发CONTROL_AE_PRECAPTURE_TRIGGER,不然会导致闪光灯常亮。 建议每触发一次capture都重新创建一次CapureRequest对象。 如果在触发CONTROL_AE_PRECAPTURE_TRIGGER成功之后不锁定AE,闪光灯会很快关闭。如果执行了锁定AE会在AE状态lock后关闭

Flash常用的如下设置(摘抄高通Camera源码)

    private void applyFlash(CaptureRequest.Builder request, String value) {

        switch (value) {
            case "on":
                if (mLongshotActive) {
                    request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                    request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
                } else {
                    request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                    request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_SINGLE);
                }
                break;
            case "auto":
                if (mLongshotActive) {
                    // When long shot is active, turn off the flash in auto mode
                    request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                    request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                } else {
                    request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                    request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_SINGLE);
                }
                break;
            case "off":
                request.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                request.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                break;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,313评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,369评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,916评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,333评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,425评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,481评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,491评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,268评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,719评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,004评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,179评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,832评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,510评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,153评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,402评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,045评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,071评论 2 352