近期,Google 的 Jetpack 组件又出了新的库: CameraX 。
顾名思义: CameraX 就是用来进行 Camera 开发的官方库了,而且后续会有 Google 进行维护和升级。这对于广大 Camera 开发工程师和即将成为 Camera 的程序员来说,真是个好消息。
CameraX 介绍
简述 Camera 开发
关于 Camera 的开发,之前也有写过相关的文章。
CameraX 开发实践
CameraX 目前的版本是 1.0.0-alpha01 ,在使用时要添加如下的依赖:
// CameraX
def camerax_version = "1.0.0-alpha01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// // Apply declared configs to CameraX using the same lifecycle owner
CameraX.bindToLifecycle(
lifecycleOwner: this, preview, imageCapture, imageAnalyzer)
// Unbinds all use cases from the lifecycle and removes them from CameraX.
CameraX.unbindAll()
fun buildPreviewUseCase(): Preview {
val previewConfig = PreviewConfig.Builder()
// 宽高比
.setTargetAspectRatio(aspectRatio)
// 旋转
.setTargetRotation(rotation)
// 分辨率
.setTargetResolution(resolution)
// 前后摄像头
.setLensFacing(lensFacing)
.build()
// 创建 Preview 对象
val preview = Preview(previewConfig)
// 设置监听
preview.setOnPreviewOutputUpdateListener { previewOutput ->
// PreviewOutput 会返回一个 SurfaceTexture
cameraTextureView.surfaceTexture = previewOutput.surfaceTexture
}
return preview
}
通过建造者模式创建 Preview 对象,并且一定要给 Preview 对象设置 OnPreviewOutputUpdateListener 接口回调。
相机预览的图像流是通过 SurfaceTexture 来返回的,而在项目例子中,是通过把 TextureView 的 SurfaceTexture 替换成 CameraX 返回的 SurfaceTexture,这样实现了 TextureView 控件显示 Camera 预览内容。
另外,还需要考虑到设备的选择方向,当设备横屏变为竖屏了,TextureView 也要相应的做旋转。
preview.setOnPreviewOutputUpdateListener { previewOutput ->
cameraTextureView.surfaceTexture = previewOutput.surfaceTexture
// Compute the center of preview (TextureView)
val centerX = cameraTextureView.width.toFloat() / 2
val centerY = cameraTextureView.height.toFloat() / 2
// Correct preview output to account for display rotation
val rotationDegrees = when (cameraTextureView.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> return@setOnPreviewOutputUpdateListener
}
val matrix = Matrix()
matrix.postRotate(-rotationDegrees.toFloat(), centerX, centerY)
// Finally, apply transformations to TextureView
cameraTextureView.setTransform(matrix)
}
TextureView 旋转的设置同样在 OnPreviewOutputUpdateListener 接口中去完成。
图像分析
在 bindToLifecycle 方法中,imageAnalyzer 参数并不是必需的。
ImageAnalysis 可以帮助我们做一些图像质量的分析,需要我们去实现 ImageAnalysis.Analyzer 接口的 analyze 方法。
fun buildImageAnalysisUseCase(): ImageAnalysis {
// 分析器配置 Config 的建造者
val analysisConfig = ImageAnalysisConfig.Builder()
// 宽高比例
.setTargetAspectRatio(aspectRatio)
// 旋转
.setTargetRotation(rotation)
// 分辨率
.setTargetResolution(resolution)
// 图像渲染模式
.setImageReaderMode(readerMode)
// 图像队列深度
.setImageQueueDepth(queueDepth)
// 设置回调的线程
.setCallbackHandler(handler)
.build()
// 创建分析器 ImageAnalysis 对象
val analysis = ImageAnalysis(analysisConfig)
// setAnalyzer 传入实现了 analyze 接口的类
analysis.setAnalyzer { image, rotationDegrees ->
// 可以得到的一些图像信息,参见 ImageProxy 类相关方法
val rect = image.cropRect
val format = image.format
val width = image.width
val height = image.height
val planes = image.planes
}
return analysis
}
ImageQueueDepth 会指定相机管线中图像的个数,提高 ImageQueueDepth 的数量会对相机的性能和内存的使用造成影响。
fun buildImageCaptureUseCase(): ImageCapture {
val captureConfig = ImageCaptureConfig.Builder()
.setTargetAspectRatio(aspectRatio)
.setTargetRotation(rotation)
.setTargetResolution(resolution)
.setFlashMode(flashMode)
// 拍摄模式
.setCaptureMode(captureMode)
.build()
// 创建 ImageCapture 对象
val capture = ImageCapture(captureConfig)
cameraCaptureImageButton.setOnClickListener {
// Create temporary file
val fileName = System.currentTimeMillis().toString()
val fileFormat = ".jpg"
val imageFile = createTempFile(fileName, fileFormat)
// Store captured image in the temporary file
capture.takePicture(imageFile, object : ImageCapture.OnImageSavedListener {
override fun onImageSaved(file: File) {
// You may display the image for example using its path file.absolutePath
}
override fun onError(useCaseError: ImageCapture.UseCaseError, message: String, cause: Throwable?) {
// Display error message
}
})
}
return capture
}/
在图像拍摄的相关配置中,也有个 CaptureMode 的设置。
MIN_LATENCY
该模式下,拍摄速度会相对快一点,但图像质量会打折扣
MAX_QUALITY
该模式下,拍摄速度会慢一点,但图像质量好
OpenGL 渲染
CameraX 的拓展
如果你看了 Google I/O 大会的视频,那肯定了解 CameraX 的拓展属性。
在视频中提到 Google 也正在和三星、LG、摩托摩拉等厂商进行合作,为了获得厂商系统相机的一些能力,比如 HDR 等。
期待 CameraX 能给带来更多的新特性。
"开发者说·DTalk" 面向中国开发者们征集 Google 移动应用 (apps & games) 相关的产品/技术内容。欢迎大家前来分享您对移动应用的行业洞察或见解、移动开发过程中的心得或新发现、以及应用出海的实战经验总结和相关产品的使用反馈等。我们由衷地希望可以给这些出众的中国开发者们提供更好展现自己、充分发挥自己特长的平台。我们将通过大家的技术内容着重选出优秀案例进行谷歌开发技术专家 (GDE) 的推荐。
点击屏末 | 阅读原文 | 了解更多 "开发者说·DTalk" 活动详情与参与方式
长按右侧二维码
报名参与