How to programmatically take Photos while recording Video using Camera2 API in Android

Khushbu Shah picture Khushbu Shah · Mar 23, 2016 · Viewed 7.8k times · Source

I want to capture image while recording video using camera2 API.

Two separate demos are available. 1. To capture image and 2. To record video

I tried to combine them into one app. But confused in many things. Like

  • CameraDevice is single which representation of a single camera connected to an Android device.
  • Different template needed in request. For image : mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); and for video mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
  • In createCaptureSession method surface of ImageReader is needed and for video MediaRecorder is needed.

CameraCaptureSession.StateCallback is needed in createCaptureSession method for both image and video.

And camera2 APIs are new. So no more examples available on Google.

Anyone has any idea about this? like how to capture photo while recording in android?

Answer

Eddy Talvala picture Eddy Talvala · Mar 31, 2016

For camera2 API, and video snapshots, you'll need to improve the camera2video sample somewhat.

  • 3 capture target Surfaces: Preview (a TextureView or SurfaceView), JPEG (an ImageReader), and recording (MediaRecorder or MediaCodec).
  • Pass surfaces from all three to createCaptureSession
  • When not recording video, set the repeating request to just target the preview
  • When recording video, set the repeating request to target both preview and the recording Surface, and start the mediarecorder (as camera2video does); when stopping recording switch back to just targeting preview in the repeating request.
  • When you want to take a picture, issue a single capture() call with a request that targets the JPEG surface, the preview surface, and if recording is active, the recording surface.
  • Get the JPEG from the ImageReader when its onImageAvailable callback fires.