app/src/main/java/cn/lytcom/camera2kit/Camera2Fragment.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;
                        }
                        mPreviewSession = cameraCaptureSession;
                        try {
                            updateAutoFocus();
                            updateFlash(mPreviewRequestBuilder);
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mPreviewSession.setRepeatingRequest(mPreviewRequest,
                                mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onConfigureFailed(
                        @NonNull CameraCaptureSession cameraCaptureSession) {
                        showToast("Create preview configure failed");
                    }
                }, mBackgroundHandler);
        } 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 ; } mPreviewSession = cameraCaptureSession ; try { updateAutoFocus ( ) ; updateFlash ( mPreviewRequestBuilder ) ; mPreviewRequest = mPreviewRequestBuilder . build ( ) ; mPreviewSession . setRepeatingRequest ( mPreviewRequest , mCaptureCallback , mBackgroundHandler ) ; } catch ( CameraAccessException e ) { e . printStackTrace ( ) ; } } @ Override public void onConfigureFailed ( @ NonNull CameraCaptureSession cameraCaptureSession ) { showToast ( "Create preview configure failed" ) ; } } , mBackgroundHandler ) ; } 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. //                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, //                                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // Flash is automatically enabled when necessary. // Finally, we start displaying the camera preview.