Example usage for android.hardware.camera2 CameraDevice TEMPLATE_PREVIEW

List of usage examples for android.hardware.camera2 CameraDevice TEMPLATE_PREVIEW

Introduction

In this page you can find the example usage for android.hardware.camera2 CameraDevice TEMPLATE_PREVIEW.

Prototype

int TEMPLATE_PREVIEW

To view the source code for android.hardware.camera2 CameraDevice TEMPLATE_PREVIEW.

Click Source Link

Document

Create a request suitable for a camera preview window.

Usage

From source file:com.example.android.camera2basic.Fragment.Camera2BasicFragment.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *//* ww w.  j  a  v  a2s.c om*/
private void createCameraPreviewSession() {
    try {
        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.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
        //

        // Here, we create a CameraCaptureSession for camera preview.
        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.
                        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:com.askjeffreyliu.camera2barcode.camera.CameraSource.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *//*from ww  w .j av a  2 s.c  o  m*/
private void createCameraPreviewSession() {
    try {
        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());

        mImageReaderPreview = ImageReader.newInstance(mPreviewSize.getWidth(), mPreviewSize.getHeight(),
                ImageFormat.YUV_420_888, 1);
        mImageReaderPreview.setOnImageAvailableListener(mOnPreviewAvailableListener, mBackgroundHandler);

        // 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.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        mPreviewRequestBuilder.addTarget(mImageReaderPreview.getSurface());

        // Here, we create a CameraCaptureSession for camera preview.
        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReaderPreview.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.
                        mCaptureSession = cameraCaptureSession;
                        try {
                            // Auto focus should be continuous for camera preview.
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, mFocusMode);
                            if (mFlashSupported) {
                                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, mFlashMode);
                            }

                            // 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) {
                        Log.d(TAG, "Configuration failed!");
                    }
                }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

/**
 * Request a stream of images.//from  ww w . ja v  a 2s  .c  o  m
 *
 * @return true if successful, false if there was an error submitting the
 *         capture request.
 */
private boolean sendRepeatingCaptureRequest() {
    Timber.v("sendRepeatingCaptureRequest()");
    try {
        CaptureRequest.Builder builder;
        if (ZSL_ENABLED) {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        } else {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        }

        builder.addTarget(previewSurface);

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

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

        builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);

        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

        addRegionsToCaptureRequestBuilder(builder);

        captureSession.setRepeatingRequest(builder.build(), captureManager, cameraHandler);
        return true;
    } catch (CameraAccessException e) {
        if (ZSL_ENABLED) {
            Timber.w(e, "Could not execute zero-shutter-lag repeating request.");
        } else {
            Timber.w(e, "Could not execute preview request.");
        }
        return false;
    }
}

From source file:com.tenth.space.ui.fragment.HomeFragment.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *//* w w  w .j a  va2s .  com*/
private void createCameraPreviewSession() {
    try {
        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.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                updatePreview(cameraCaptureSession, true);

            }

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

From source file:edu.umich.eecs.lab11.camera.CameraFragment.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 */// w ww .  j  a va2s.  c om
private void createCameraPreviewSession() {
    try {
        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.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_EFFECT_MODE, CaptureRequest.CONTROL_EFFECT_MODE_MONO);

        // Here, we create a CameraCaptureSession for camera preview.
        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.
                        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:research.dlsu.cacaoapp.Camera2BasicFragment1.java

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 *//*from  ww  w.  j  av  a  2  s.c  o  m*/
private void createCameraPreviewSession() {
    try {
        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.
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        // Here, we create a CameraCaptureSession for camera preview.
        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.
                        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 onClosed(CameraCaptureSession session) {
                        super.onClosed(session);
                        stopBackgroundThread();
                    }

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

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

/**
 * Request a stream of images./*from www . j  a v a2  s  .  c  o m*/
 *
 * @return true if successful, false if there was an error submitting the
 *         capture request.
 */
private boolean sendRepeatingCaptureRequest() {
    Log.v(TAG, "sendRepeatingCaptureRequest()");
    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);

        builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE);

        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);

        addRegionsToCaptureRequestBuilder(builder);

        mCaptureSession.setRepeatingRequest(builder.build(), mCaptureManager, mCameraHandler);
        return true;
    } catch (CameraAccessException e) {
        if (ZSL_ENABLED) {
            Log.v(TAG, "Could not execute zero-shutter-lag repeating request.", e);
        } else {
            Log.v(TAG, "Could not execute preview request.", e);
        }
        return false;
    }
}

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

private boolean sendAutoExposureTriggerRequest(Flash flashMode) {
    Timber.v("sendAutoExposureTriggerRequest()");
    try {/* w  ww .j a v  a  2s .co  m*/
        CaptureRequest.Builder builder;
        if (ZSL_ENABLED) {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        } else {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        }

        builder.addTarget(previewSurface);

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

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

        builder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
                CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);

        addRegionsToCaptureRequestBuilder(builder);
        addFlashToCaptureRequestBuilder(builder, flashMode);

        captureSession.capture(builder.build(), captureManager, cameraHandler);

        return true;
    } catch (CameraAccessException e) {
        Timber.w(e, "Could not execute auto exposure trigger request.");
        return false;
    }
}

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

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

        builder.addTarget(previewSurface);

        if (ZSL_ENABLED) {
            builder.addTarget(captureImageReader.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);

        captureSession.capture(builder.build(), captureManager, cameraHandler);

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

From source file:com.obviousengine.android.focus.ZslFocusCamera.java

/**
 * Like {@link #sendRepeatingCaptureRequest()}, but with the focus held
 * constant.// w ww  .  ja v  a  2s  . co m
 *
 * @return true if successful, false if there was an error submitting the
 *         capture request.
 */
private boolean sendAutoFocusHoldRequest() {
    Timber.v("sendAutoFocusHoldRequest()");
    try {
        CaptureRequest.Builder builder;
        if (ZSL_ENABLED) {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        } else {
            builder = device.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        }

        builder.addTarget(previewSurface);

        if (ZSL_ENABLED) {
            builder.addTarget(captureImageReader.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.

        captureSession.setRepeatingRequest(builder.build(), captureManager, cameraHandler);

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