Example usage for android.hardware.camera2 CaptureRequest CONTROL_AF_TRIGGER

List of usage examples for android.hardware.camera2 CaptureRequest CONTROL_AF_TRIGGER

Introduction

In this page you can find the example usage for android.hardware.camera2 CaptureRequest CONTROL_AF_TRIGGER.

Prototype

Key CONTROL_AF_TRIGGER

To view the source code for android.hardware.camera2 CaptureRequest CONTROL_AF_TRIGGER.

Click Source Link

Document

Whether the camera device will trigger autofocus for this request.

This entry is normally set to IDLE, or is not included at all in the request settings.

When included and set to START, the camera device will trigger the autofocus algorithm.

Usage

From source file:com.dastanapps.camera2.view.Cam2AutoFitTextureView.java

@Nullable
private Boolean touchTofocus2(MotionEvent event) {
    MotionEvent motionEvent = event;//from www. j  av a2s  .c o  m
    final int actionMasked = motionEvent.getActionMasked();
    if (actionMasked != MotionEvent.ACTION_DOWN) {
        return false;
    }
    if (mManualFocusEngaged) {
        Log.d(TAG, "Manual focus already engaged");
        return true;
    }

    final Rect sensorArraySize = mCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

    //TODO: here I just flip x,y, but this needs to correspond with the sensor orientation (via SENSOR_ORIENTATION)
    final int y = (int) ((motionEvent.getX() / (float) getWidth()) * (float) sensorArraySize.height());
    final int x = (int) ((motionEvent.getY() / (float) getHeight()) * (float) sensorArraySize.width());
    final int halfTouchWidth = 150; //(int)motionEvent.getTouchMajor(); //TODO: this doesn't represent actual touch size in pixel. Values range in [3, 10]...
    final int halfTouchHeight = 150; //(int)motionEvent.getTouchMinor();
    MeteringRectangle focusAreaTouch = new MeteringRectangle(Math.max(x - halfTouchWidth, 0),
            Math.max(y - halfTouchHeight, 0), halfTouchWidth * 2, halfTouchHeight * 2,
            MeteringRectangle.METERING_WEIGHT_MAX - 1);

    CameraCaptureSession.CaptureCallback captureCallbackHandler = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);
            mManualFocusEngaged = false;

            if (request.getTag() == "FOCUS_TAG") {
                //the focus trigger is complete -
                //resume repeating (preview surface will get frames), clear AF trigger
                mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);
                try {
                    mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
                CaptureFailure failure) {
            super.onCaptureFailed(session, request, failure);
            Log.e(TAG, "Manual AF failure: " + failure);
            mManualFocusEngaged = false;
        }
    };

    //first stop the existing repeating request
    try {
        mPreviewSession.stopRepeating();
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    //cancel any existing AF trigger (repeated touches, etc.)
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
    try {
        mPreviewSession.capture(mPreviewBuilder.build(), captureCallbackHandler, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    //Now add a new AF trigger with focus region
    if (isMeteringAreaAFSupported()) {
        mPreviewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[] { focusAreaTouch });
    }
    mPreviewBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
    mPreviewBuilder.setTag("FOCUS_TAG"); //we'll capture this later for resuming the preview

    //            //then we ask for a single request (not repeating!)
    //            mPreviewSession.capture(mPreviewBuilder.build(), captureCallbackHandler, mBackgroundHandler);
    return null;
}

From source file:com.dastanapps.camera2.view.Cam2AutoFitTextureView.java

protected void touchToFocus(MotionEvent event) {
    //first stop the existing repeating request
    try {/*from w  w w.  ja  v  a 2s .co m*/
        mPreviewSession.stopRepeating();
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    Rect rect = mCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
    Log.i(TAG, "SENSOR_INFO_ACTIVE_ARRAY_SIZE,,,,,,,,rect.left--->" + rect.left + ",,,rect.top--->" + rect.top
            + ",,,,rect.right--->" + rect.right + ",,,,rect.bottom---->" + rect.bottom);
    Size size = mCharacteristics.get(CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE);
    Log.i(TAG, "mCameraCharacteristics,,,,size.getWidth()--->" + size.getWidth() + ",,,size.getHeight()--->"
            + size.getHeight());
    int areaSize = 200;
    int right = rect.right;
    int bottom = rect.bottom;
    int viewWidth = getWidth();
    int viewHeight = 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);
    Log.i(TAG, "focusLeft--->" + focusLeft + ",,,focusTop--->" + focusBottom + ",,,focusRight--->"
            + (focusLeft + areaSize) + ",,,focusBottom--->" + (focusBottom + areaSize));
    newRect = new Rect(focusLeft, focusBottom, focusLeft + areaSize, focusBottom + areaSize);
    MeteringRectangle meteringRectangle = new MeteringRectangle(newRect, 500);
    MeteringRectangle[] meteringRectangleArr = { meteringRectangle };
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, meteringRectangleArr);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
}

From source file:com.example.camera2apidemo.Camera2Fragment.java

private void lockFocus() {
    try {/*from   ww  w  . j  ava  2  s  . c  o  m*/
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);

        CameraCaptureSession.CaptureCallback mLockFocusCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {

                Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState
                        || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
                    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                    if (aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                        captureStillPicture();
                    } else {
                        runPrecaptureSequence();
                    }
                }
            }
        };

        mState = STATE_WAITING_LOCK;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mLockFocusCallback, mBackgroundHandler);

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.example.camera2apidemo.Camera2Fragment.java

private void unlockFocus() {
    try {/*  w  w w.  jav  a 2 s  .co  m*/
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);

        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);

        mCaptureSession.capture(mPreviewRequestBuilder.build(), null, mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
        mCaptureSession.setRepeatingRequest(mPreviewRequest, null, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:freed.cam.apis.camera2.modules.PictureModuleApi2.java

protected void finishCapture(Builder captureBuilder) {
    try {//from  www .j  av a2 s  . c  om
        Log.d(TAG, "CaptureDone");
        cameraHolder.CaptureSessionH.StartRepeatingCaptureSession();
        if (cameraHolder
                .get(CaptureRequest.CONTROL_AF_MODE) == CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE
                || cameraHolder.get(
                        CaptureRequest.CONTROL_AF_MODE) == CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO) {
            cameraHolder.SetParameterRepeating(CaptureRequest.CONTROL_AF_TRIGGER,
                    CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
            cameraHolder.SetParameterRepeating(CaptureRequest.CONTROL_AF_TRIGGER,
                    CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
        }
    } catch (NullPointerException ex) {
        ex.printStackTrace();
        ;
    }

    isWorking = false;
}

From source file:com.example.testcamera.Camera2BasicFragmentGoogle.java

/**
 * Lock the focus as the first step for a still image capture.
 *///w ww  .  j  av a 2  s. co  m
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.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.example.android.learnmore.Camera2BasicFragment.java

private void lockFocus() {
    try {//from   w ww.  j a  v  a  2  s. com
        // 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();
    }
}

From source file:com.example.testcamera.Camera2BasicFragmentGoogle.java

/**
 * Unlock the focus. This method should be called when still image capture sequence is finished.
 *//*from   ww w. ja  va  2 s.  co  m*/
private void unlockFocus() {
    try {
        // Reset the autofucos trigger
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
        // After this, the camera will go back to the normal state of preview.
        mState = STATE_PREVIEW;
        mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:info.staticfree.mqtt_camera.fragment.CameraFragment.java

/**
 * Lock the focus as the first step for a still image capture.
 *///w ww. j  a  v a2  s . c o m
private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        previewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        // Tell #captureCallback to wait for the lock.
        state = STATE_WAITING_LOCK;
        captureSession.capture(previewRequestBuilder.build(), captureCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        Log.e(TAG, "Error locking focus", e);
    }
}

From source file:org.odk.collect.android.fragments.Camera2Fragment.java

/**
 * Lock the focus as the first step for a still image capture.
 *///from  w  ww  .  j  a  v  a2  s  .  co m
private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        previewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
        // Tell #captureCallback to wait for the lock.
        state = STATE_WAITING_LOCK;
        captureSession.capture(previewRequestBuilder.build(), captureCallback, backgroundHandler);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}