android video app starting preview display

The android video app starting preview display tutorial describes the steps involved to get the preview screen running on the android camera2 video app.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-preview-display

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-preview-display

Steps

Create the CameraRequest Builder member

private CaptureRequest.Builder mCaptureRequestBuilder;

 Method for creating the capture session for the preview display

 private void startPreview() {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);

        try {
            mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mCaptureRequestBuilder.addTarget(previewSurface);

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

                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {
                            Toast.makeText(getApplicationContext(),
                                    "Unable to setup camera preview", Toast.LENGTH_SHORT).show();

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

 Call the start preview method from the camera device state callback

private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
       @Override
       public void onOpened(CameraDevice camera) {
           mCameraDevice = camera;
           startPreview();
           // Toast.makeText(getApplicationContext(),
           //         "Camera connection made!", Toast.LENGTH_SHORT).show();
       }

       @Override
       public void onDisconnected(CameraDevice camera) {
           camera.close();
           mCameraDevice = null;
       }

       @Override
       public void onError(CameraDevice camera, int error) {
           camera.close();
           mCameraDevice = null;
       }
   };

 

Android video app starting preview display summary

In the android video app starting preview display tutorial we have learned the steps involved in creating a camera capture request for the preview display which includes

  • Creating a surface texture
  • Creating a surface based on the surface texture
  • Setting the preview default size for the surface texture
  • Creating a capture request builder
  • Adding the preview surface to the capture request builder
  • Starting a camera capture session request
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>