Android camera2 api surface preview

android camera2 api surface preview

Covers the setup for creating a camera preview using the android camera2 api.

The android camera2 api surface preview tutorial completes the setup for the preview display which should now be displaying on the device

CODE AVAILABLE ON GITHUB

You can get the code from here and then you will need to Tag createcamerapreviewsession or else you can run this command

git clone --branch createcamerapreviewsession https://github.com/mobapptuts/recyclerview_image_gallery.git

CODE SAMPLES

CamaraIntentActivity

Member Variables

private CaptureRequest mPreviewCaptureRequest;
   private CaptureRequest.Builder mPreviewCaptureRequestBuilder;
   private CameraCaptureSession mCameraCaptureSession;
   private CameraCaptureSession.CaptureCallback mSessionCaptureCallback
           = new CameraCaptureSession.CaptureCallback() {
       @Override
       public void onCaptureStarted(CameraCaptureSession session,
                                    CaptureRequest request,
                                    long timestamp,
                                    long frameNumber) {
           super.onCaptureStarted(session, request, timestamp, frameNumber);
       }
   };

Method createCameraPreviewSession

private void createCameraPreviewSession() {
    try {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);
        mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewCaptureRequestBuilder.addTarget(previewSurface);
        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        if(mCameraDevice == null) {
                            return;
                        }
                        try {
                            mPreviewCaptureRequest = mPreviewCaptureRequestBuilder.build();
                            mCameraCaptureSession = session;
                            mCameraCaptureSession.setRepeatingRequest(
                                    mPreviewCaptureRequest,
                                    mSessionCaptureCallback,
                                    mBackgroundHandler
                            );
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession session) {
                        Toast.makeText(
                                getApplicationContext(),
                                "create camera session failed!",
                                Toast.LENGTH_SHORT
                        ).show();
                    }
                }, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
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>