相機AF_TRIGGER_START不會開始對觸摸進行對焦 [android]


Answers

在CaptureRequest.Builder上設置CONTROL_AF_TRIGGER_CANCEL之後CaptureSession.capture(mPreviewCaptureRequestBuilder.build(),mCallback,mHandler)您需要調用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
Question

我有一個攝像頭的應用程序使用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;

        }
    });
}