相机AF_TRIGGER_START无法开始对焦onTouch

时间:2015-10-24 19:50:19

标签: android camera touch focus area

我有一个使用camera2 Api的相机应用程序,我将onTouchListener实现到我用于从相机预览的TextureView,我想要的是当用户点击屏幕以便能够专注于该区域时,就像内置的Android相机一样。我不知道它出了什么问题,它在我使用AF_MODE_AUTO时有效,但它不精确,它只关注屏幕中心的什么。

这是我的代码:

        mTextureView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                Rect rect = cameraCharacteristics
                        .get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
                Size size = cameraCharacteristics
                        .get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE);
                int areaSize = 200;
                int right = rect.right;
                int bottom = rect.bottom;
                int viewWidth = mTextureView.getWidth();
                int viewHeight = mTextureView.getHeight();
                int ll,
                rr;
                Rect newRect;
                int centerX = (int) event.getX();
                int centerY = (int) event.getY();
                ll = ((centerX * right) - areaSize) / viewWidth;
                rr = ((centerY * bottom) - areaSize) / viewHeight;

                int focusLeft =  clamp(ll, 0, right);
                int focusBottom = clamp(rr, 0, bottom);

                newRect = new Rect(focusLeft, focusBottom, focusLeft
                        + areaSize, focusBottom + areaSize);
                MeteringRectangle meteringRectangle = new MeteringRectangle(
                        newRect, 500);
                MeteringRectangle[] meteringRectangleArr = { meteringRectangle };


                System.out.println("metering rectARR" + meteringRectangleArr);

                mPreviewCaptureRequestBuilder.set(
                        CaptureRequest.CONTROL_AF_TRIGGER,
                        CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);

                mPreviewCaptureRequestBuilder.set(
                        CaptureRequest.CONTROL_AF_REGIONS,
                        meteringRectangleArr);

                mPreviewCaptureRequestBuilder.set(
                        CaptureRequest.CONTROL_AF_TRIGGER,
                        CameraMetadata.CONTROL_AF_TRIGGER_START);

                try {
                    mCameraCaptureSession.setRepeatingRequest(
                            mPreviewCaptureRequest,
                            mSessionCaptureCallback,
                            mBackgroundHandler);
                } catch (CameraAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                break;
            }

            return true;

        }
    });
}

2 个答案:

答案 0 :(得分:0)

我将google's Camera2Basic example分支并将其更改为使用CaptureRequest.CONTROL_AF_MODE_AUTO而不是CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE

您可以从git中获取项目并对其进行测试 - https://github.com/pinhassi/android-Camera2Basic

答案 1 :(得分:0)

您需要在CaptureRequest.Builder上设置CONTROL_AF_TRIGGER_CANCEL后调用CaptureSession.capture(mPreviewCaptureRequestBuilder.build(),mCallback,mHandler)

mPreviewCaptureRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER,
                    CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
//TODO call capture
//TODO set new focus region
//TODO call capture/setRepeatingRequest