Android Camera2 API background handler

android camera2 api background handler

Android tutorial on how to add a background thread handler for tasks in the android camera2 api application.

This android camera2 api tutorial explain how to move time consuming tasks off the main ui thread to the android camera2 api background handler.

DOWNLOAD THE CODE

The code samples are now provided on github here

https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-handler

or else run this command

git clone —branch camera2-handler https://github.com/mobapptuts/recyclerview_image_gallery.git

CODE SAMPLES

Creating the HandlerThread & Handler members

private HandlerThread mBackgroundThread;
    private Handler mBackgroundHandler;

Creating open & close background thread methods

private void openBackgroundThread() {
       mBackgroundThread = new HandlerThread("Camera2 background thread");
       mBackgroundThread.start();
       mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
   }

   private void closeBackgoundThread() {
       mBackgroundThread.quitSafely();
       try {
           mBackgroundThread.join();
           mBackgroundThread = null;
           mBackgroundHandler = null;
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }

Adding the open & close background thread methods to the Activity’s onResume & onPause methods

@Override
   public void onResume() {
       super.onResume();

       openBackgroundThread();

       if(mTextureView.isAvailable()) {
           setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
           openCamera();
       } else {
           mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
       }
   }

   @Override
   public void onPause() {

       closeCamera();
       closeBackgoundThread();

       super.onPause();
   }

Adding background handler to the CameraManager openCamera callback and the CameraCaptureSession setRepeatingRequest callback.

private void openCamera() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

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>