CameraX 是对 Jetpack 的补充,可让您更轻松地利用 Camera2 API 的功能。

添加依赖项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dependencies {
def camerax_version = "1.0.2"
// The following line is optional, as the core library is included indirectly by camera-camera2
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
// If you want to additionally use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
// If you want to additionally use the CameraX VideoCapture library
implementation "androidx.camera:camera-video:1.1.0-beta02"
// If you want to additionally use the CameraX View class
implementation "androidx.camera:camera-view:1.1.0-beta02"
// If you want to additionally use the CameraX Extensions library
implementation "androidx.camera:camera-extensions:1.1.0-beta02"

}

使用 CameraX 开启预览

在向应用添加预览时,请使用 PreviewView,这是一种可以剪裁、缩放和旋转以确保正确显示的 View。

当相机处于活动状态时,图片预览会流式传输到 PreviewView 中的 Surface。

将 PreviewView 添加到布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".camera.CameraActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
>

<androidx.camera.view.PreviewView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/previewView"
/>

</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

请求 CameraProvider

以下代码展示了如何请求 CameraProvider:

1
2
3
4
5
6
7
8
9
10
11
12
class CameraActivity : AppCompatActivity() {

private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
private lateinit var dataBinding: ActivityCameraBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataBinding = ActivityCameraBinding.inflate(layoutInflater)
setContentView(dataBinding.root)
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
}
}

检查 CameraProvider 可用性

请求 CameraProvider 后,请验证它能否在视图创建后成功初始化。以下代码展示了如何执行此操作:

1
2
3
4
5
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)

}, ContextCompat.getMainExecutor(this))

选择相机并绑定生命周期和用例

创建并确认 CameraProvider 后,请执行以下操作:

  • 1、创建 Preview。

  • 2、指定所需的相机 LensFacing 选项。

  • 3、将所选相机和任意用例绑定到生命周期。

  • 4、将 Preview 连接到 PreviewView。

以下代码展示了一个示例:

1
2
3
4
5
6
7
8
9
10
11
private fun bindPreview(cameraProvider: ProcessCameraProvider?) {
var preview: Preview = Preview.Builder()
.build()

var cameraSelector: CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()

preview.setSurfaceProvider(dataBinding.previewView.getSurfaceProvider())
var camera = cameraProvider?.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
}

请注意,bindToLifecycle() 会返回一个 Camera 对象。如需详细了解如何控制相机输出(如变焦和曝光),请参阅此指南。

现在,您已完成实现相机预览的操作。请构建您的应用,然后确认预览是否出现在您的应用中并能按预期工作。

PS: 记得需要相机权限