Example usage for android.hardware.camera2 CaptureRequest CONTROL_AF_MODE

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

Introduction

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

Prototype

Key CONTROL_AF_MODE

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

Click Source Link

Document

Whether auto-focus (AF) is currently enabled, and what mode it is set to.

Only effective if CaptureRequest#CONTROL_MODE android.control.mode = AUTO and the lens is not fixed focus (i.e.

Usage

From source file:com.android.camera2.its.ItsService.java

private void do3A(JSONObject params) throws ItsException {
    try {// w  w w. j  a  v  a2  s.  c  om
        // Start a 3A action, and wait for it to converge.
        // Get the converged values for each "A", and package into JSON result for caller.

        // 3A happens on full-res frames.
        Size sizes[] = ItsUtils.getYuvOutputSizes(mCameraCharacteristics);
        int widths[] = new int[1];
        int heights[] = new int[1];
        int formats[] = new int[1];
        widths[0] = sizes[0].getWidth();
        heights[0] = sizes[0].getHeight();
        formats[0] = ImageFormat.YUV_420_888;
        int width = widths[0];
        int height = heights[0];

        prepareCaptureReader(widths, heights, formats, 1);
        List<Surface> outputSurfaces = new ArrayList<Surface>(1);
        outputSurfaces.add(mCaptureReaders[0].getSurface());
        BlockingSessionCallback sessionListener = new BlockingSessionCallback();
        mCamera.createCaptureSession(outputSurfaces, sessionListener, mCameraHandler);
        mSession = sessionListener.waitAndGetSession(TIMEOUT_IDLE_MS);

        // Add a listener that just recycles buffers; they aren't saved anywhere.
        ImageReader.OnImageAvailableListener readerListener = createAvailableListenerDropper(mCaptureCallback);
        mCaptureReaders[0].setOnImageAvailableListener(readerListener, mSaveHandlers[0]);

        // Get the user-specified regions for AE, AWB, AF.
        // Note that the user specifies normalized [x,y,w,h], which is converted below
        // to an [x0,y0,x1,y1] region in sensor coords. The capture request region
        // also has a fifth "weight" element: [x0,y0,x1,y1,w].
        MeteringRectangle[] regionAE = new MeteringRectangle[] {
                new MeteringRectangle(0, 0, width, height, 1) };
        MeteringRectangle[] regionAF = new MeteringRectangle[] {
                new MeteringRectangle(0, 0, width, height, 1) };
        MeteringRectangle[] regionAWB = new MeteringRectangle[] {
                new MeteringRectangle(0, 0, width, height, 1) };
        if (params.has(REGION_KEY)) {
            JSONObject regions = params.getJSONObject(REGION_KEY);
            if (regions.has(REGION_AE_KEY)) {
                regionAE = ItsUtils.getJsonWeightedRectsFromArray(regions.getJSONArray(REGION_AE_KEY), true,
                        width, height);
            }
            if (regions.has(REGION_AF_KEY)) {
                regionAF = ItsUtils.getJsonWeightedRectsFromArray(regions.getJSONArray(REGION_AF_KEY), true,
                        width, height);
            }
            if (regions.has(REGION_AWB_KEY)) {
                regionAWB = ItsUtils.getJsonWeightedRectsFromArray(regions.getJSONArray(REGION_AWB_KEY), true,
                        width, height);
            }
        }

        // If AE or AWB lock is specified, then the 3A will converge first and then lock these
        // values, waiting until the HAL has reported that the lock was successful.
        mNeedsLockedAE = params.optBoolean(LOCK_AE_KEY, false);
        mNeedsLockedAWB = params.optBoolean(LOCK_AWB_KEY, false);

        // By default, AE and AF both get triggered, but the user can optionally override this.
        // Also, AF won't get triggered if the lens is fixed-focus.
        boolean doAE = true;
        boolean doAF = true;
        if (params.has(TRIGGER_KEY)) {
            JSONObject triggers = params.getJSONObject(TRIGGER_KEY);
            if (triggers.has(TRIGGER_AE_KEY)) {
                doAE = triggers.getBoolean(TRIGGER_AE_KEY);
            }
            if (triggers.has(TRIGGER_AF_KEY)) {
                doAF = triggers.getBoolean(TRIGGER_AF_KEY);
            }
        }
        if (doAF && mCameraCharacteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE) == 0) {
            // Send a dummy result back for the code that is waiting for this message to see
            // that AF has converged.
            Logt.i(TAG, "Ignoring request for AF on fixed-focus camera");
            mSocketRunnableObj.sendResponse("afResult", "0.0");
            doAF = false;
        }

        mInterlock3A.open();
        mIssuedRequest3A = false;
        mConvergedAE = false;
        mConvergedAWB = false;
        mConvergedAF = false;
        mLockedAE = false;
        mLockedAWB = false;
        long tstart = System.currentTimeMillis();
        boolean triggeredAE = false;
        boolean triggeredAF = false;

        Logt.i(TAG, String.format("Initiating 3A: AE:%d, AF:%d, AWB:1, AELOCK:%d, AWBLOCK:%d", doAE ? 1 : 0,
                doAF ? 1 : 0, mNeedsLockedAE ? 1 : 0, mNeedsLockedAWB ? 1 : 0));

        // Keep issuing capture requests until 3A has converged.
        while (true) {

            // Block until can take the next 3A frame. Only want one outstanding frame
            // at a time, to simplify the logic here.
            if (!mInterlock3A.block(TIMEOUT_3A * 1000)
                    || System.currentTimeMillis() - tstart > TIMEOUT_3A * 1000) {
                throw new ItsException("3A failed to converge (timeout)");
            }
            mInterlock3A.close();

            // If not converged yet, issue another capture request.
            if ((doAE && (!triggeredAE || !mConvergedAE)) || !mConvergedAWB
                    || (doAF && (!triggeredAF || !mConvergedAF)) || (doAE && mNeedsLockedAE && !mLockedAE)
                    || (mNeedsLockedAWB && !mLockedAWB)) {

                // Baseline capture request for 3A.
                CaptureRequest.Builder req = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                req.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                req.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
                req.set(CaptureRequest.CONTROL_CAPTURE_INTENT, CaptureRequest.CONTROL_CAPTURE_INTENT_PREVIEW);
                req.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                req.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, 0);
                req.set(CaptureRequest.CONTROL_AE_LOCK, false);
                req.set(CaptureRequest.CONTROL_AE_REGIONS, regionAE);
                req.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
                req.set(CaptureRequest.CONTROL_AF_REGIONS, regionAF);
                req.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);
                req.set(CaptureRequest.CONTROL_AWB_LOCK, false);
                req.set(CaptureRequest.CONTROL_AWB_REGIONS, regionAWB);

                if (mConvergedAE && mNeedsLockedAE) {
                    req.set(CaptureRequest.CONTROL_AE_LOCK, true);
                }
                if (mConvergedAWB && mNeedsLockedAWB) {
                    req.set(CaptureRequest.CONTROL_AWB_LOCK, true);
                }

                // Trigger AE first.
                if (doAE && !triggeredAE) {
                    Logt.i(TAG, "Triggering AE");
                    req.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                            CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
                    triggeredAE = true;
                }

                // After AE has converged, trigger AF.
                if (doAF && !triggeredAF && (!doAE || (triggeredAE && mConvergedAE))) {
                    Logt.i(TAG, "Triggering AF");
                    req.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
                    triggeredAF = true;
                }

                req.addTarget(mCaptureReaders[0].getSurface());

                mIssuedRequest3A = true;
                mSession.capture(req.build(), mCaptureResultListener, mResultHandler);
            } else {
                mSocketRunnableObj.sendResponse("3aConverged", "");
                Logt.i(TAG, "3A converged");
                break;
            }
        }
    } catch (android.hardware.camera2.CameraAccessException e) {
        throw new ItsException("Access error: ", e);
    } catch (org.json.JSONException e) {
        throw new ItsException("JSON error: ", e);
    } finally {
        mSocketRunnableObj.sendResponse("3aDone", "");
    }
}

From source file:research.dlsu.cacaoapp.Camera2BasicFragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *//* www.j a  va  2  s .  c o  m*/
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                // saveEntryToDatabase();

                // call match activity to save matched file
                Intent i = new Intent(getActivity(), MatchActivity27Jan.class);
                i.putExtra(ChoosePhotoActivity.EXTRA_PATH, mFile.getAbsolutePath());
                // cacaoImagePath = getIntent().getStringExtra(ChoosePhotoActivity.EXTRA_PATH);
                startActivityForResult(i, REQUEST_MATCH);

                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

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

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *///from  w w  w  .  ja  va  2 s  .  c o m
private void createCameraPreviewSession() {
    try {// ?texture
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;

        // We configure the size of default buffer to be the size of camera preview we want.
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

        // This is the output Surface we need to start preview.
        Surface surface = new Surface(texture);

        // We set up a CaptureRequest.Builder with the output Surface.
        // 
        //             CameraDeviceCameraCaptureSession?CameraCaptureSession??
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        // Here, we create a CameraCaptureSession for camera preview.
        /*???
        ???CameraDeviceCameraCaptureSession?
             CameraCaptureSession??
        CameraCaptureSession???
        ?Surface?
        ??CameraCaptureSession
        ?Handler?
        */
        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        // The camera is already closed
                        if (null == mCameraDevice) {
                            return;
                        }

                        // When the session is ready, we start displaying the preview.
                        // ??, ?field?? mCaptureSession
                        mCaptureSession = cameraCaptureSession;
                        try {
                            // Auto focus should be continuous for camera preview.
                            // 
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                            // Flash is automatically enabled when necessary.
                            setAutoFlash(mPreviewRequestBuilder); // 

                            // Finally, we start displaying the camera preview.
                            // 
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            // ???, 
                            mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
                                    mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                        showToast("Failed");
                    }
                }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:org.tensorflow.demo.Camera2BasicFragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *//*from ww w .  jav a2 s. c o m*/
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
        sensorOrientation = rotation + getOrientation(rotation);
        frameToCropTransform = ImageUtils.getTransformationMatrix(previewWidth, previewHeight, INPUT_SIZE,
                INPUT_SIZE, sensorOrientation, MAINTAIN_ASPECT);

        cropToFrameTransform = new Matrix();
        frameToCropTransform.invert(cropToFrameTransform);
        classifier = TensorFlowImageClassifier.create(getActivity().getAssets(), MODEL_FILE, LABEL_FILE,
                INPUT_SIZE, IMAGE_MEAN, IMAGE_STD, INPUT_NAME, OUTPUT_NAME);

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                Log.d(TAG, mFile.toString());
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.ape.camera2raw.Camera2RawFragment.java

/**
 * Configure the given {@link CaptureRequest.Builder} to use auto-focus, auto-exposure, and
 * auto-white-balance controls if available.
 * <p/>// ww  w . j  ava  2s.c om
 * Call this only with {@link #mCameraStateLock} held.
 *
 * @param builder the builder to configure.
 */
private void setup3AControlsLocked(CaptureRequest.Builder builder) {
    // Enable auto-magical 3A run by camera device
    builder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);

    Float minFocusDist = mCharacteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);

    // If MINIMUM_FOCUS_DISTANCE is 0, lens is fixed-focus and we need to skip the AF run.
    mNoAFRun = (minFocusDist == null || minFocusDist == 0);

    if (!mNoAFRun) {
        // If there is a "continuous picture" mode available, use it, otherwise default to AUTO.
        if (contains(mCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES),
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE)) {
            builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        } else {
            builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
        }
    }

    // If there is an auto-magical flash control mode available, use it, otherwise default to
    // the "on" mode, which is guaranteed to always be available.
    if (contains(mCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES),
            CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH)) {
        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
    } else {
        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
    }

    // If there is an auto-magical white balance control mode available, use it.
    if (contains(mCharacteristics.get(CameraCharacteristics.CONTROL_AWB_AVAILABLE_MODES),
            CaptureRequest.CONTROL_AWB_MODE_AUTO)) {
        // Allow AWB to run auto-magically if this device supports this
        builder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);
    }
}

From source file:research.dlsu.crabapp.Camera2BasicFragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *//*from w  w  w.  j  a v a2  s  . c  o  m*/
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                cu = saveEntryToDatabase();
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.android.camera.one.v2.OneCameraZslImpl.java

/**
 *//*from  www .  j  a va2  s . c o m*/
private boolean sendAutoFocusTriggerRequest() {
    Log.v(TAG, "sendAutoFocusTriggerRequest()");
    try {
        CaptureRequest.Builder builder;
        if (ZSL_ENABLED) {
            builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        } else {
            builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        }

        builder.addTarget(mPreviewSurface);

        if (ZSL_ENABLED) {
            builder.addTarget(mCaptureImageReader.getSurface());
        }

        builder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);

        addRegionsToCaptureRequestBuilder(builder);

        builder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);

        mCaptureSession.capture(builder.build(), mCaptureManager, mCameraHandler);

        return true;
    } catch (CameraAccessException e) {
        Log.v(TAG, "Could not execute auto focus trigger request.", e);
        return false;
    }
}

From source file:research.dlsu.cacaoapp.Camera2BasicFragment1.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *///from w w  w .j av a 2  s .c  om
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                saveEntryToDatabase();
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.android.camera.one.v2.OneCameraZslImpl.java

/**
 * Like {@link #sendRepeatingCaptureRequest()}, but with the focus held
 * constant.// w  ww .ja v a2s. c om
 *
 * @return true if successful, false if there was an error submitting the
 *         capture request.
 */
private boolean sendAutoFocusHoldRequest() {
    Log.v(TAG, "sendAutoFocusHoldRequest()");
    try {
        CaptureRequest.Builder builder;
        if (ZSL_ENABLED) {
            builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        } else {
            builder = mDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        }

        builder.addTarget(mPreviewSurface);

        if (ZSL_ENABLED) {
            builder.addTarget(mCaptureImageReader.getSurface());
        }

        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);

        builder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);

        addRegionsToCaptureRequestBuilder(builder);
        // TODO: This should fire the torch, if appropriate.

        mCaptureSession.setRepeatingRequest(builder.build(), mCaptureManager, mCameraHandler);

        return true;
    } catch (CameraAccessException e) {
        Log.v(TAG, "Could not execute auto focus hold request.", e);
        return false;
    }
}

From source file:net.ddns.mlsoftlaberge.trycorder.TryviscamFragment.java

/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 *///w  w  w . j av  a2s. c o  m
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice
                .createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        setVisionMode(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

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

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                    @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                showToast("Saved: " + mFile);
                Log.d(TAG, mFile.toString());
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}