当前位置:  开发笔记 > 编程语言 > 正文

Android camera2 api触控焦点的例子?

如何解决《Androidcamera2api触控焦点的例子?》经验,为你挑选了0个好方法。

嗨,我正在使用camera2basic示例来实现我的camera2应用程序.我找不到任何好的例子来实现触摸以使用camera2 api进行聚焦.目前我用于触控的代码是:

    private void setFocusArea(MotionEvent event) {
    if (mCameraId == null) return;
    CameraManager cm = (CameraManager)getActivity().getSystemService(Context.CAMERA_SERVICE);
    CameraCharacteristics cc = null;
    try {
        cc = cm.getCameraCharacteristics(mCameraId);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    int myX = (int)event.getX();
    int myY = (int)event.getY();
    MeteringRectangle focusArea = new MeteringRectangle(myX-100,myY-100,200,200,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_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
    }
}

但问题是它显示出奇怪的行为,其上的自动闪光不断重复自动对焦序列无限次,而且它似乎并不专注于触摸区域.我试过改变

mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);

至:

mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);

这停止了​​重复的自动对焦序列,但它仍然没有聚焦在触摸区域上,闪光灯只闪烁不到一秒而不是正常的对焦序列.请帮我解决这个问题,或者引导我进行工作,以便集中注意力.谢谢

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有