作者 / Donovan McMurray,Camera Developer Relations Engineer
CameraX 是 Android Jetpack 的相机库,可帮助您为不同的 Android 版本和设备打造一致的出色体验,随着 CameraX 1.3 版本的推出,这个库的实用性也更上一层楼。CameraX 已用于越来越多的 Android 应用中,拥有非常广泛的应用场景,包括简单高效的相机交互、高级图像处理等等。
CameraX
https://developer.android.google.cn/training/camerax
Android Jetpack
https://developer.android.google.cn/jetpack
CameraX 1.3 提供更多高级功能。借助双并发摄像头功能,应用可以同时操控两个摄像头。此外,CameraX 1.3 提供一些新的 HDR 视频功能,让用户可以轻松拍出满意的作品。现在,您还可添加图形库转换效果 (例如使用 OpenGL 或 Vulkan) 到 Preview、ImageCapture 和 VideoCapture 用例中,以便采用滤镜和特效。此外,还有许多视频方面的功能改进等您发掘。
OpenGL
https://www.opengl.org/Vulkan
https://www.vulkan.org/
CameraX 1.3 Beta 版已正式推出,让我们即刻了解详情吧!
双并发摄像头
CameraX 让复杂的相机功能轻松易用,新的双并发摄像头功能也不例外。CameraX 会处理低层级细节工作,例如确保以正确的顺序打开和关闭并发摄像头视频串流。在 CameraX 中,绑定双并发摄像头与绑定单个摄像头并无太大差别。
首先,使用 getAvailableConcurrentCameraInfos() 确定哪些相机支持并发连接。常见情形是选择前置摄像头和后置摄像头。
getAvailableConcurrentCameraInfos()
https://developer.android.google.cn/reference/androidx/camera/lifecycle/ProcessCameraProvider#getAvailableConcurrentCameraInfos%28%29
- var primaryCameraSelector: CameraSelector? = null
- var secondaryCameraSelector: CameraSelector? = null
-
-
- for (cameraInfos in cameraProvider.availableConcurrentCameraInfos) {
- primaryCameraSelector = cameraInfos.first {
- it.lensFacing == CameraSelector.LENS_FACING_FRONT
- }.cameraSelector
- secondaryCameraSelector = cameraInfos.first {
- it.lensFacing == CameraSelector.LENS_FACING_BACK
- }.cameraSelector
-
-
- if (primaryCameraSelector == null || secondaryCameraSelector == null) {
- // If either a primary or secondary selector wasn't found, reset both
- // to move on to the next list of CameraInfos.
- primaryCameraSelector = null
- secondaryCameraSelector = null
- } else {
- // If both primary and secondary camera selectors were found, we can
- // conclude the search.
- break
- }
- }
-
-
- if (primaryCameraSelector == null || secondaryCameraSelector == null) {
- // Front and back concurrent camera not available. Handle accordingly.
- }
然后,为每个摄像头创建一个 SingleCameraConfig,依次传入各摄像头选择器、UseCaseGroup 和 Life