Camera AF_TRIGGER_START doesn't start focusing onTouch
Asked Answered
C

2

9

i have a camera app using camera2 Api, i implemented onTouchListener to the TextureView i use for Previewing from Camera, what i want is when a user taps the screen to be able to focus on that area exactly like the built in Android Camera. i don't know what it's going wrong, it works when i use AF_MODE_AUTO but it's not precise , it focuses only whats on the center of the screen.

Here it's my code:

        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;

        }
    });
}
Cephalothorax answered 24/10, 2015 at 19:50 Comment(1)
You might need to set 3A to auto: mPreviewCaptureRequestBuilder.set( CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);Difficult
W
0

I branched form google's Camera2Basic example and changed it to use CaptureRequest.CONTROL_AF_MODE_AUTO instead of CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE

You can take the project from git and test it - https://github.com/pinhassi/android-Camera2Basic

Wilie answered 12/9, 2016 at 8:14 Comment(0)
T
0

You need to call CaptureSession.capture(mPreviewCaptureRequestBuilder.build(),mCallback,mHandler) after you set the CONTROL_AF_TRIGGER_CANCEL on the CaptureRequest.Builder

mPreviewCaptureRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER,
                    CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
//TODO call capture
//TODO set new focus region
//TODO call capture/setRepeatingRequest
Tissue answered 28/12, 2016 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.