camera2 java 层框架分析和使用案例

本文深入探讨了android.hardware.camera2包,它提供了访问Android设备上camera设备的接口,替代了已废弃的android.hardware.Camera API。文章详细介绍了CameraCaptureSession、CameraMetadata、CaptureRequest和CaptureResult等关键类,以及如何创建相机捕获会话和输出Surface。通过CameraManager枚举和打开相机设备,利用CameraCharacteristics获取硬件设备的静态属性。内容涵盖了从预览图像到JPEG或RAW缓冲区的捕获,以及如何构建CaptureRequest和处理捕获结果。此外,还讨论了CameraCaptureSession的创建和生命周期,强调了异步回调的重要性。
摘要由CSDN通过智能技术生成

android.hardware.camera2 package 综述

android.hardware.camera2 提供了访问android device上camera devices的接口

The android.hardware.camera2 package provides an interface to
individual camera devices connected to an Android device. It replaces
the deprecated {android.hardware.Camera}

android.hardware.camera2把camera device抽象为管道:接收request, 处理request, 输出结果
This package models a camera device as a pipeline, which 
takes in input requests for capturing a single frame, 
captures the single image per the request, and then 
outputs one capture result metadata packet, plus a set of output image buffers for the request. 
The requests are processed in-order, and multiple requests can be in flight at
once. Since the camera device is a pipeline with multiple stages,
having multiple requests in flight is required to maintain full framerate on most Android devices.

通过 android.hardware.camera2.CameraManager 枚举、查询、打开 camera devices
To enumerate, query, and open available camera devices, obtain a
{android.hardware.camera2.CameraManager} instance.

CameraCharacteristics描述 hardware device的静态属性,可用设置和输出参数
Individual {android.hardware.camera2.CameraDevice
CameraDevices} provide a set of static property information that
describes the hardware device and the available settings and output
parameters for the device. This information is provided through the
{android.hardware.camera2.CameraCharacteristics} object, and is
available through { android.hardware.camera2.CameraManager#getCameraCharacteristics}

拍照或视频流:创建android.hardware.camera2.CameraCaptureSession 和 output Surfaces
To capture or stream images from a camera device, the application
must first create a {android.hardware.camera2.CameraCaptureSession camera capture session}
with a set of output Surfaces for use with the camera device, with
{android.hardware.camera2.CameraDevice#createCaptureSession}.
每个surface 提前配置size and format, 根据StreamConfigurationMap: (surface的参数: size and format)
Each Surface has to be pre-configured with an {android.hardware.camera2.params.StreamConfigurationMap appropriate
size and format} (if applicable) to match the sizes and formats available from the camera device.
获得surface的方法:
A target Surface can be obtained from a variety of classes, including 
{android.view.SurfaceView},
{android.graphics.SurfaceTexture} via
{android.view.Surface#Surface(SurfaceTexture)},
{android.media.MediaCodec}, 
{android.media.MediaRecorder},
{android.renderscript.Allocation}, and 
{android.media.ImageReader}.


一般而言,preview images发送到android.view.SurfaceView或者android.view.TextureView
Generally, camera preview images are sent to {android.view.SurfaceView} or 
{android.view.TextureView} (via its {android.graphics.SurfaceTexture}). 

Capture of JPEG images or RAW buffers:
Capture of JPEG images or RAW buffers for {android.hardware.camera2.DngCreator} can be
done with {android.media.ImageReader} with the {android.graphics.ImageFormat#JPEG} and {
android.graphics.ImageFormat#RAW_SENSOR} formats.  

Application-driven processing of camera data in RenderScript, OpenGL ES, or directly in
managed or native code is best done through {android.renderscript.Allocation} with a YUV 
{android.renderscript.Type}, {android.graphics.SurfaceTexture},
and {android.media.ImageReader} with a {android.graphics.ImageFormat#YUV_420_888} format, respectively.
应用需要构建一个CaptureRequest, CaptureRequest定义了需要的参数和 输出surface
The application then needs to construct a {android.hardware.camera2.CaptureRequest}, 
which defines all the capture parameters needed by a camera device to capture a single
image. The request also lists which of the configured output Surfaces should be used as targets for this capture. 

CaptureRequest.Builder: 构建CaptureRequest的方法 (CaptureRequest 参数)
The CameraDevice has a{android.hardware.camera2.CameraDevice#createCaptureRequest
factory method} for creating a {android.hardware.camera2.CaptureRequest.Builder request builder} for a
given use case, which is optimized for the Android device the
application is running on.

capture session处理request
Once the request has been set up, it can be handed to the active
capture session either for a one-shot {android.hardware.camera2.CameraCaptureSession#capture capture} 
or for an endlessly { android.hardware.camera2.CameraCaptureSession#setRepeatingRequest
repeating} use. 
Both methods also have a variant that accepts a list
of requests to use as a burst capture/repeating burst. Repeating
requests have a lower priority than captures.

处理请求后,camera device 生成一个TotalCaptureResult object其中包含状态信息
After processing a request, the camera device will produce a {
android.hardware.camera2.TotalCaptureResult} object, which contains
information about the state of the camera device at time of capture,
and the final settings used. These may vary somewhat from the request,
if rounding or resolving contradictory parameters was necessary.
camera device 发送一帧image data 到Surfaces
The camera device will also send a frame of image data into each of the
output {Surfaces} included in the request. These are produced
asynchronously relative to the output CaptureResult, som

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值