实现Tap to Focus in Camera2 API

时间:2015-10-15 14:30:01

标签: android

我想在我的自定义相机中实现点按焦点功能。 这是Google https://github.com/googlesamples/android-Camera2Basic

提供的基本代码

这是代码片段,我想我应该添加我的功能 如果有人实施了Camera2 API,请提供帮助!

  private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CameraMetadata.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();
    }
}

3 个答案:

答案 0 :(得分:14)

您需要将自动对焦和自动曝光区域设置为用户点按的区域。

密钥为CONTROL_AF_REGIONSCONTROL_AE_REGIONS

它们的单位位于传感器active array coordinate system中,因此您必须从UI触摸坐标转换为相对于预览视图的坐标,并从那里转换为活动阵列坐标。

如果预览的宽高比与传感器的宽高比相匹配,那么这很简单;如果没有,您将必须调整为创建预览输出所做的裁剪。关于裁剪如何工作的最佳图表here。请注意,如果您还要应用缩放,则还需要在计算中包含缩放系数。

计算完区域后,您可能希望将自动对焦模式设置为自动(而不是通常用于正常预览的CONTINUOUS_PICTURE),然后触发自动对焦。一旦你收敛AF(在捕捉结果中查看AF状态,等待AF_STATE_FOCUSED_LOCKED),你就可以拍出一张焦点对准的照片。如果您想在一段时间后恢复正常操作或者用户取消触摸以进行对焦,请将AF模式切换回CONTINUOUS_PICTURE。

答案 1 :(得分:3)

  1. 使用onTouch侦听器获取用户触摸屏幕的位置。
  2. 根据该位置计算一个/ MeteringRectangle(s)
  3. 使用此测光矩形设置CaptureRequest.CONTROL_AF_REGION& CaptureRequest.CONTROL_AE_REGION

  4. CaptureRequest.CONTROL_AF_MODE设置为CaptureRequest.CONTROL_AF_MODE_AUTO

  5. CaptureRequest.CONTROL_AF_TRIGGER至CameraMetadata.CONTROL_AF_TRIGGER_START
  6. CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER至CameraMetadata.CONTROL_AE_TRIGGER_START

  7. 然后构建捕获请求

  8.   

    Here你可以找到一个完整的例子。

答案 2 :(得分:-1)

 public void handleFocus(MotionEvent event) {
    int pointerId = event.getPointerId(0);
    int pointerIndex = event.findPointerIndex(pointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);

    Rect touchRect = new Rect(
            (int) (x - 100),
            (int) (y - 100),
            (int) (x + 100),
            (int) (y + 100) );


    if (mCameraId == null) return;
    CameraManager cm = (CameraManager)this.getSystemService(Context.CAMERA_SERVICE);
    CameraCharacteristics cc = null;
    try {
        cc = cm.getCameraCharacteristics(mCameraId);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }


    MeteringRectangle focusArea = new MeteringRectangle(touchRect,MeteringRectangle.METERING_WEIGHT_DONT_CARE);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    try {
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
    } catch (CameraAccessException e){
        // log
    }

    /* if (isMeteringAreaAESupported(cc)) {
     *//*mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
                new MeteringRectangle[]{focusArea});*//*
    }
    if (isMeteringAreaAFSupported(cc)) {
        *//*mPreviewRequestBuilder
                .set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_AUTO);*//*
    }*/
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
            new MeteringRectangle[]{focusArea});
    mPreviewRequestBuilder
            .set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
            CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
            CameraMetadata.CONTROL_AF_TRIGGER_START);
    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
            CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
    try {
        mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
        /* mManualFocusEngaged = true;*/
    } catch (CameraAccessException e) {
        // error handling
    }
}

触摸事件中的触摸事件调用方法