app/src/main/java/com/wj/camera/Camera2Activity.java
/**
     * Creates a new {@link CameraCaptureSession} for camera preview.
     */
Creates a new  CameraCaptureSession for camera preview.
Creates a new CameraCaptureSession for camera preview .
private void createCameraPreviewSession() {
        try {
            SurfaceTexture texture = mTextureView.getSurfaceTexture();
            assert texture != null;
            texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            Surface surface = new Surface(texture);
            mPreviewRequestBuilder
                    = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(surface);
            mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                            if (null == mCameraDevice) {
                                return;
                            }
                            mCaptureSession = cameraCaptureSession;
                            try {
                                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                        CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                                setAutoFlash(mPreviewRequestBuilder);
                                mPreviewRequest = mPreviewRequestBuilder.build();
                                mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                        mCaptureCallback, mBackgroundHandler);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onConfigureFailed(
                                @NonNull CameraCaptureSession cameraCaptureSession) {
                        }
                    }, null
            );
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
private void createCameraPreviewSession ( ) { try { SurfaceTexture texture = mTextureView . getSurfaceTexture ( ) ; assert texture != null ; texture . setDefaultBufferSize ( mPreviewSize . getWidth ( ) , mPreviewSize . getHeight ( ) ) ; Surface surface = new Surface ( texture ) ; mPreviewRequestBuilder = mCameraDevice . createCaptureRequest ( CameraDevice . TEMPLATE_PREVIEW ) ; mPreviewRequestBuilder . addTarget ( surface ) ; mCameraDevice . createCaptureSession ( Arrays . asList ( surface , mImageReader . getSurface ( ) ) , new CameraCaptureSession . StateCallback ( ) { @ Override public void onConfigured ( @ NonNull CameraCaptureSession cameraCaptureSession ) { if ( null == mCameraDevice ) { return ; } mCaptureSession = cameraCaptureSession ; try { mPreviewRequestBuilder . set ( CaptureRequest . CONTROL_AF_MODE , CaptureRequest . CONTROL_AF_MODE_CONTINUOUS_PICTURE ) ; setAutoFlash ( mPreviewRequestBuilder ) ; mPreviewRequest = mPreviewRequestBuilder . build ( ) ; mCaptureSession . setRepeatingRequest ( mPreviewRequest , mCaptureCallback , mBackgroundHandler ) ; } catch ( CameraAccessException e ) { e . printStackTrace ( ) ; } } @ Override public void onConfigureFailed ( @ NonNull CameraCaptureSession cameraCaptureSession ) { } } , null ) ; } catch ( CameraAccessException e ) { e . printStackTrace ( ) ; } }
Creates a new {@link CameraCaptureSession} for camera preview.
Creates a new { @link CameraCaptureSession } for camera preview .
// We configure the size of default buffer to be the size of camera preview we want. // This is the output Surface we need to start preview. // We set up a CaptureRequest.Builder with the output Surface. // Here, we create a CameraCaptureSession for camera preview. // The camera is already closed // When the session is ready, we start displaying the preview. // Auto focus should be continuous for camera preview. // Flash is automatically enabled when necessary. // Finally, we start displaying the camera preview.