Android设置camera2只在游戏中时光倒流开始时关注一次

时间:2016-12-13 19:15:10

标签: android android-camera2 camera2

我想制作一个简单的应用程序来捕捉游戏中时光倒流。最后我做到了。但我想知道是否有可能只在第一张照片上锁定焦点一次?然后使用保存的焦点值(如果可能的话)?一般来说这是个好主意?我认为每隔几秒钟调用lockFocus()就太贵了。设备固定放置,不动。

其实我的代码就像google示例代码一样。我将CaptureRequest.Builder字段CONTROL_AF_TRIGGER设置为CONTROL_AF_TRIGGER_STARTCaptureCallback我正在检查CaptureResult.CONTROL_AF_STATE,如果ok则会抓取最终照片。也许第一张照片进入LENS_FOCUS_DISTANCE然后以某种方式设置此值。

PS:此外,我不会向用户显示预览,整个相机逻辑正在服务和其他线程中运行。我还实施了ImageReader以及更多。

我的代码是:

// before is `CameraManager`, setting outputs, opening camera - all ok

public void makePhoto() { // this I call from outer 
// plus some observer, not important conditions for this issue

createCameraPreviewSession();
}

private void createCameraPreviewSession() {
    mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    mPreviewRequestBuilder.addTarget(previewSurface);

    mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
        public void onConfigured(CameraCaptureSession session) {
            mCaptureSession = session;
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
            mPreviewRequest = mPreviewRequestBuilder.build();

            startedRepeatedRequest = false; // simple flag to start lockFocus after below request has started, once
            mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, null);
        }
    }
}


private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
    public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) {
        process(partialResult);
    }

    private void process(CaptureResult result) {
        if(!startedRepeatedRequest) {
            // this is called only once, on the beginning of session
            lockFocus();
            startedRepeatedRequest = true;
        }

        switch(mState) {
            case STATE_PREVIEW:
                break;
            case STATE_WAITING_LOCK:
                Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (afState == null) {
                    captureStillPicture();
                } else 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_PICTURE_TAKEN;
                        captureStillPicture(); // this leads to final capture photo
                    } else {
                        runPrecaptureSequence();
                    }
                }
                break;
            // There are also flags for STATE_PRECAPTURE, and STATE_WAITING_NON_PRECAPTURE but this doesn't matter here
        }
    }
}

private void lockFocus() {
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        mState = STATE_WAITING_LOCK;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
}

private void captureStillPicture() {
    final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
    Surface surfaceTarget = mImageReader.getSurface();
    captureBuilder.addTarget(surfaceTarget);
    captureBuilder.set(CaptureRequest.JPEG_QUALITY, (byte) 90);
    captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

    // callback for final capture
    CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
        mState = STATE_PREVIEW;
        session.close();
    }

    mCaptureSession.stopRepeating();
    mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
}

0 个答案:

没有答案