Android Jetpack架构组件(三)CameraX使用入门

Android中使用相机从来就不是一件容易的事。Camera1要自己管理Camera相机实例,要处理SufraceView相关的一堆东西,还有预览尺寸跟画面尺寸的选择,页面生命周期切换等等问题

后来推出了Camera2,从官方Demo 就上千行代码来看,Camera2并不解决用起来复杂的问题,它提供了更多的调用接口,可定制性更好,结果就是对普通开发者来说更加难用

终于Google也意识到这个问题,推出了最终版CameraXCameraX实际上还是用的Camera2,但它对调用API进行了很好的封装,使用起来非常方便。官方教程也很详细:https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0

参考:
https://developer.android.google.cn/training/camerax
https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0

注意:CameraXCamera2一样最低支持API 21,也就是5.0及以上

导入依赖

在app的build.gradle中加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Use the most recent version of CameraX, currently that is alpha04
def camerax_core_version = "1.0.0-beta03"
def camerax_version = "1.0.0-alpha10"
implementation "androidx.camera:camera-core:${camerax_core_version}"
implementation "androidx.camera:camera-camera2:${camerax_core_version}"
// If you want to use the CameraX View class
implementation "androidx.camera:camera-view:${camerax_version}"
// If you want to use the CameraX Extensions library
implementation "androidx.camera:camera-extensions:${camerax_version}"
// If you want to use the CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
}

CameraX需要一些Java 8的方法,app的build.gradle的buildTypes之后添加以下内容:

1
2
3
4
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

布局文件和权限

放一个PreviewView即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<androidx.camera.view.PreviewView
android:id="@+id/preview_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

权在AndroidManifest.xml中加入相机权限

1
<uses-permission android:name="android.permission.CAMERA" />

并加入动态申请权限代码,此处省略

启动相机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
private void startCamera() {
executor = ContextCompat.getMainExecutor(this);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
// No errors need to be handled for this Future.
// This should never be reached.
e.printStackTrace();
}
}, executor);
}

private void bindPreview(ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_16_9)
.setTargetRotation(mBinding.previewView.getDisplay().getRotation())
.build();

//preview.setSurfaceProvider(mBinding.previewView.getPreviewSurfaceProvider());

// 相机选择器
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();

// 构建图像捕获用例
ImageCapture imageCapture = new ImageCapture.Builder()
.setFlashMode(ImageCapture.FLASH_MODE_AUTO)
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.build();

// 图像分析
ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
// 分辨率
.setTargetResolution(new Size(1280, 720))
// 非阻塞模式
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();

// Camera camera = cameraProvider.bindToLifecycle(this,
// cameraSelector, imageCapture, imageAnalysis, preview);
Camera camera = cameraProvider.bindToLifecycle(this,
CameraSelector.DEFAULT_BACK_CAMERA, preview);

CameraInfo cameraInfo = camera.getCameraInfo();

preview.setSurfaceProvider(mBinding.previewView.createSurfaceProvider(cameraInfo));
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :