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.google.android.apps.watchme.StreamerActivity.java

void startPreview() {
    SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
    surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
    Surface previewSurface = new Surface(surfaceTexture);
    try {/*from   w  ww .  j  a v a2s  .  c  om*/
        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        captureRequestBuilder.addTarget(previewSurface);

        cameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession session) {
                        try {
                            session.setRepeatingRequest(captureRequestBuilder.build(), null, backgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                        Toast.makeText(getApplicationContext(), "The preview failed to setup",
                                Toast.LENGTH_SHORT).show();
                    }
                }, null);

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

From source file:com.andrasta.dashi.MainActivity.java

@SuppressWarnings("MissingPermission")
private void openCamera(int width, int height) {
    try {// w  w w.j  a va2s.  c  om
        configBuilder = CameraUtils.initCameraConfig(this, display, width, height);
        onCameraOrientationSet(configBuilder.getCameraOrientation());

        int cameraWidth = configBuilder.getSize().getWidth();
        int cameraHeight = configBuilder.getSize().getHeight();

        // We fit the aspect ratio of TextureView to the size of preview we picked.
        int orientation = this.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            textureView.setAspectRatio(cameraWidth, cameraHeight);
            laneView.setAspectRatio(cameraWidth, cameraHeight);
        } else {
            textureView.setAspectRatio(cameraHeight, cameraWidth);
            laneView.setAspectRatio(cameraHeight, cameraWidth);
        }
        Matrix matrix = CameraUtils.configureTransform(display.getRotation(), width, height, cameraWidth,
                cameraHeight);
        textureView.setTransform(matrix);
        SurfaceTexture texture = textureView.getSurfaceTexture();
        if (texture == null) {
            Log.d(TAG, "No SurfaceTexture");
            return;
        }
        // We configure the size of default buffer to be the size of camera preview we want.
        texture.setDefaultBufferSize(cameraWidth, cameraHeight);

        CameraConfig.Request request = new CameraConfig.Request(CameraDevice.TEMPLATE_PREVIEW,
                new Surface(texture));
        request.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        configBuilder.addRequest(request);
        Log.d(TAG, "Display camera resolution " + cameraWidth + 'x' + cameraHeight);

        imageReader = ImageReader.newInstance(cameraRecSize.getWidth(), cameraRecSize.getHeight(),
                ImageFormat.YUV_420_888, alprHandler.getThreadsNum() + 1);
        imageReader.setOnImageAvailableListener(this, null);
        request = new CameraConfig.Request(CameraDevice.TEMPLATE_PREVIEW, imageReader.getSurface());
        configBuilder.addRequest(request);
        Log.d(TAG,
                "Recognition camera resolution " + cameraRecSize.getWidth() + 'x' + cameraRecSize.getHeight());

        camera.open(configBuilder.build());
        Log.d(TAG, "Camera opened: " + configBuilder.getCameraId());
    } catch (CameraAccessException e) {
        onError(false, e);
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the device
        onError(true, e);
    }
}

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

private void startPrieview() {
    try {/*from  w w  w. jav  a  2  s.  com*/
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);

        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
                CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
        mPreviewRequest = mPreviewRequestBuilder.build();

        mCaptureSession.setRepeatingRequest(mPreviewRequest, null, null);

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

From source file:com.microsoft.projectoxford.emotionsample.RecognizeActivity.java

protected void createCameraPreview() {
    try {//from   w  w w  .jav  a2s. c  o  m
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
        Surface surface = new Surface(texture);
        captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        captureRequestBuilder.addTarget(surface);
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                //The camera is already closed
                if (null == cameraDevice) {
                    return;
                }
                // When the session is ready, we start displaying the preview.
                cameraCaptureSessions = cameraCaptureSession;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                Toast.makeText(RecognizeActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
            }
        }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.raulh82vlc.face_detection_sample.camera2.presentation.FDCamera2Presenter.java

private void createCameraPreview() {
    if (isViewAvailable()) {
        try {/*w w w . j a v a2 s . c  o m*/
            SurfaceTexture texture = activityView.getTextureView().getSurfaceTexture();
            assert texture != null;

            texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
            Surface surface = new Surface(texture);
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            captureRequestBuilder.addTarget(surface);
            cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    //The camera is already closed
                    if (null == cameraDevice) {
                        return;
                    }
                    // When the session is ready, we start displaying the preview.
                    cameraCaptureSessions = cameraCaptureSession;
                    updatePreview();
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    //                Toast.makeText(this, "Configuration change", Toast.LENGTH_SHORT).show();
                }
            }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
}

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

/**
 * Start the camera preview./* ww  w .  j av a 2 s . c  om*/
 */
private void startPreview() {
    if (null == cameraDevice || !textureView.isAvailable() || null == previewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = textureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
        previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        Surface previewSurface = new Surface(texture);
        previewBuilder.addTarget(previewSurface);

        cameraDevice.createCaptureSession(Collections.singletonList(previewSurface),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession session) {
                        previewSession = session;
                        updatePreview();
                    }

                    @Override
                    public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                        Activity activity = getActivity();
                        if (null != activity) {
                            ToastUtils.showShortToast("Failed");
                        }
                    }
                }, backgroundHandler);
    } catch (CameraAccessException e) {
        Timber.e(e);
    }
}

From source file:com.android.rahul.myselfieapp.Fragment.CamVideoFragment.java

/**
 * Start the camera preview.//w ww . j  av a2s.  co  m
 */
private void startPreview() {
    if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        Surface previewSurface = new Surface(texture);
        mPreviewBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                        mPreviewSession = cameraCaptureSession;
                        updatePreview();
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                        Activity activity = getActivity();
                        if (null != activity) {
                            Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

From source file:com.caseystalnaker.android.popinvideodemo.fragments.Camera2VideoFragment.java

/**
 * Start the camera preview.//from w ww  .  ja  v  a 2 s  .  c om
 */
private void startPreview() {
    Log.d(LOGTAG, "startPreview()");
    if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
        return;
    }
    try {
        closePreviewSession();
        final SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        final Surface previewSurface = new Surface(texture);
        mPreviewBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(CameraCaptureSession cameraCaptureSession) {
                        mPreviewSession = cameraCaptureSession;
                        updatePreview();
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                        final Activity activity = getActivity();
                        if (null != activity) {
                            Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

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

/**
 * Creates a new {@link CameraCaptureSession} for camera preview.
 */// www .  j  a v a  2 s . co  m
private void createCameraPreviewSession() {
    try {

        Surface surface = new Surface(mTextureView.getSurfaceTexture());
        mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        // Auto focus should be continuous for camera preview.
        //            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
        //                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                        mCaptureSession = cameraCaptureSession;
                        try {
                            mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null,
                                    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.mebene.ACHud.Camera2VideoFragment.java

/**
 * Start the camera preview.//ww  w .  j  a  v a2  s .  c o  m
 */
private void startPreview() {
    if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

        Surface previewSurface = new Surface(texture);
        mPreviewBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Collections.singletonList(previewSurface),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(@NonNull CameraCaptureSession session) {
                        mPreviewSession = session;
                        updatePreview();
                    }

                    @Override
                    public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                        Activity activity = getActivity();
                        if (null != activity) {
                            Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}