如何在Android中使用Camera2 API录制视频时以编程方式拍摄照片 [英] How to programmatically take Photos while recording Video using Camera2 API in Android

查看:1225
本文介绍了如何在Android中使用Camera2 API录制视频时以编程方式拍摄照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用camera2 API录制视频时捕捉图像。

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

可以使用两个单独的演示。
1. 捕获图像
2. 录制视频

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是单个连接到Android设备的单个摄像头的表示。

  • 不同请求中需要的模板。对于图像: mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 和视频 mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);

  • 在createCaptureSession方法中,需要ImageReader的表面,并且需要视频MediaRecorder。

  • 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 createCaptureSession方法需要.StateCallback用于图像和视频。

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

而camera2 API是新的。因此,Google上没有更多示例。

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

任何人都对此有任何想法?比如如何在Android中录制时捕捉照片?

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

推荐答案

对于camera2 API和视频快照,你需要改进camera2video示例。

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


  • 3捕获目标曲面:预览(TextureView或SurfaceView),JPEG(ImageReader)和录制(MediaRecorder或MediaCodec)。

  • 将所有三个表面传递给createCaptureSession

  • 当不录制视频时,将重复请求设置为仅定位预览

  • 录制视频时,将重复请求设置为同时预览和录制Surface,并启动mediarecorder(如camera2video所做);当停止录制切换回到重复请求中的目标预览时。

  • 当你想拍照时,发出一个捕获()调用,其中一个请求是针对JPEG表面的,预览曲面,如果录制有效,则为录制曲面。

  • 当onRemageAvailable回调触发时,从ImageReader获取JPEG。

  • 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.

这篇关于如何在Android中使用Camera2 API录制视频时以编程方式拍摄照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆