Android Camera入门
上篇文章Android Camera系列1 - Camera2在textureView预览理了理如何实现最简单的TextureView预览Camera2。
饭要一口一口的吃,胖子要一斤一斤的长。
开门见山,我们需要用到ImageReader这个类去得到一个Image。
Image:
Image类允许应用通过一个或多个ByteBuffers直接访问Image的像素数据, ByteBuffer包含在Image.Plane类中,同时包含了这些像素数据的配置信息。因为是作为提供raw数据使用的,Image不像Bitmap类可以直接填充到UI上使用。
因为Image的生产消费是跟硬件直接挂钩的,所以为了效率起见,Image如果不被使用了应该尽快的被销毁掉。比如说,当我们使用ImageReader从不用的媒体来源获取到Image的时候,如果Image的数量到达了maxImages,不关闭之前老的Image,新的Image就不会继续生产。
close : 关掉当前帧for reuse。调用此方法后再调用其他Image的方法都会报IllegalStateException
getFormat : 获取当前Image的格式,format决定了Image需要提供的ByteBuffers数量和每个ByteBuffer的像素数量。这里还涉及到Image.Plane.
Image.Plane : plane这里翻译为一个平面。通过作为一个数组返回,数组的数量由Image的格式决定,比如ImageFormat.JPEG返回的数组size就是1,ImageFormat.YUV_420_888返回的数字size就是3。一旦Image被关闭了,再去尝试获取plane的ByteBuffer将会失败。
Format
Plane count
Layout Details
JPEG
1
压缩过的数据,所以行数为0,解压缩需要使用BitmapFactory#decodeByteArray
YUV_420_888
3
一个明度通道+两个色彩CbCr通道,UV的宽高是Y的一半。
附一部分ImageFormat的描述。
Constants
Descriptions
JPEG
Encoded formats.
NV16
YCbCr format, used for video.
NV21
YCrCb format used for images, which uses the NV21 encoding format.
RGB_565
RGB format used for pictures encoded as RGB_565.
YUV_420_888
Multi-plane Android YUV format,This format is a generic YCbCr format, capable of describing any 4:2:0 chroma-subsampled planar or semiplanar buffer (but not fully interleaved), with 8 bits per color sample.
YUY2
YCbCr format used for images, which uses YUYV (YUY2) encoding format.
YV1