一.介绍
1.什么是CameraX?
CameraX是在Google I / O 2019上宣布的Jetpack支持库。该库的主要目的是通过提供一致且易于使用的API来帮助开发人员简化相机应用程序的开发。
2.什么是Firebase ML Kit?
Firebase ML Kit是适用于Android和iOS的移动SDK,已在Google I / O 2018上发布。ML Kit随附了自然语言(文本识别,面部检测,条形码扫描,图像标记,对象检测和跟踪,地标识别)和视觉(识别文本的语言,翻译文本,生成智能回复)的常见用例。
二.设置项目
1.在Android Studio中,从File⇒New Project创建一个新项目,然后从模板中选择Empty Activity,我的包名称是com.natigbabayev.qrscanner。
2.将 Firebase 添加到你的 Android 项目
3.打开app / build.gradle并添加Firebase ML Vision和Jetpack CameraX依赖项:
dependencies {
//...
// Make sure you have correct version of appcompat library
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
// Firebase ML Kit dependencies
implementation 'com.google.firebase:firebase-ml-vision:21.0.0'
// CameraX
def camerax_version = "1.0.0-alpha03"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
}
4.打开AndroidManifest.xml文件添加所需的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.CAMERA" />
...
</manifest>
5.将以下代码添加到你的AndroidManifest.xml文件中,以配置你的应用,以便在从Play商店安装应用后将ML模型自动下载到设备中:
<application ...>
...
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="barcode" />
</application>
6.打开activity_main.xml并添加TextureView,我们将使用它来传输摄像机输入:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="@+id/texture_view"
android:layout_width="match_parent"
android:layout_height