android video app still capture recording

The android video app still capture recording tutorial describes the how to take a photo or photos while recording a video.

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-still-record

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-still-record

Steps

 Add record capture session & record capture session members

private CameraCaptureSession mRecordCaptureSession;
   private final CameraCaptureSession.CaptureCallback mRecordCaptureCallback =
           new CameraCaptureSession.CaptureCallback() {

               private void process(CaptureResult captureResult) {
                   switch (mState) {
                       case STATE_PREVIEW:
                           break;
                       case STATE__WAIT_LOCK:
                           Integer afState = captureResult.get(CaptureResult.CONTROL_AF_STATE);
                           if(afState == CaptureRequest.CONTROL_AF_STATE_FOCUSED_LOCKED ||
                                   afState == CaptureRequest.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
                               Toast.makeText(getApplicationContext(), "Focus Lock Successful", Toast.LENGTH_SHORT).show();
                               mState = STATE_PREVIEW;
                               startStillCapture();
                           }
                           break;
                   }
               }
               @Override
               public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                   super.onCaptureCompleted(session, request, result);

                   process(result);
               }
           };

 In the start record session initialise the record capture session member & add the image reader surface

private void startRecord() {

        try {
            setupMediaRecorder();
            SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
            surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            Surface previewSurface = new Surface(surfaceTexture);
            Surface recordSurface = mMediaRecorder.getSurface();
            mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            mCaptureRequestBuilder.addTarget(previewSurface);
            mCaptureRequestBuilder.addTarget(recordSurface);

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

                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {

                        }
                    }, null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 In the lockFocus method add a request for auto focus trigger on the record capture session

private void lockFocus() {
       mState = STATE__WAIT_LOCK;
       mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
               CaptureRequest.CONTROL_AF_TRIGGER_START);
       try {
           if(mIsRecording) {
               mRecordCaptureSession.capture(mCaptureRequestBuilder.build(),
                       mRecordCaptureCallback, mBackgroundHandler);
           } else {
               mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(),
                       mPreviewCaptureCallback, mBackgroundHandler);
           }
       } catch (CameraAccessException e) {
           e.printStackTrace();
       }

   }

In the startStillCapture method add support for the record capture session

private void startStillCapture() {
       try {
           if(mIsRecording) {
               mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_VIDEO_SNAPSHOT);
           } else {
               mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
           }
           mCaptureRequestBuilder.addTarget(mImageReader.getSurface());

           CameraCaptureSession.CaptureCallback stillImageCaptureCallback = new
                   CameraCaptureSession.CaptureCallback() {
                       @Override
                       public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
                           super.onCaptureStarted(session, request, timestamp, frameNumber);
                           try {
                               createImageFileName();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }

                       @Override
                       public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                           super.onCaptureCompleted(session, request, result);
                           unLockFocus();
                       }
                   };
           if(mIsRecording) {
               mRecordCaptureSession.capture(mCaptureRequestBuilder.build(), stillImageCaptureCallback, mBackgroundHandler);
           } else {
               mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), stillImageCaptureCallback, mBackgroundHandler);
           }

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

 

Android video app still capture recording summary

In the android video app still capture recording tutorial we created the camera capture session and its callback to support the still image capture request while recording.

This was similar to the previous tutorial with the difference being the camera capture session being setup for recording.

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>