概述
说起Android扫描二维码,可能很多人都会想到用zxing,个人感觉不是很好用,后来使用华为的“统一扫码服务”,用着还行,不用开发者再辛苦钻研不同手机的分辨率以及方向了,如今机器学习热度这么高,各大厂商已经把条码识别归入机器学习SDK,本文要介绍的就是使用谷歌的 CameraX 以及机器学习,实现二维码识别,超级简单!
1、相机权限那是少不了的,添加如下权限:
<uses-permission android:name="android.permission.CAMERA"/>
2、添加相关依赖:
def camerax_version = "1.2.0-beta02"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-mlkit-vision:${camerax_version}"
implementation 'com.google.mlkit:barcode-scanning:17.0.3'//扫码库
implementation 'com.google.guava:guava:27.1-android'
3、从官方的示例代码复制两份代码
GraphicOverlay.java
BarcodeGraphic.kt
4、布局文件
<?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=".MainActivity">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<site.feiyuliuxing.camera2test.widget.GraphicOverlay
android:id="@+id/overlayView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="@id/previewView"
app:layout_constraintTop_toTopOf="@id/previewView"
app:layout_constraintEnd_toEndOf="@id/previewView"
app:layout_constraintBottom_toBottomOf="@id/previewView" />
</androidx.constraintlayout.widget.ConstraintLayout>
5、主要代码
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.mlkit.vision.MlKitAnalyzer
import androidx.camera.view.CameraController
import androidx.camera.view.LifecycleCameraController
import androidx.camera.view.PreviewView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.google.common.util.concurrent.ListenableFuture
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
class MainActivity : AppCompatActivity() {
private lateinit var previewView: PreviewView
private lateinit var cameraController: LifecycleCameraController
private lateinit var graphicOverlay: GraphicOverlay
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//申请权限
PermissionUtil.requestPermissions(this)
graphicOverlay = findViewById(R.id.overlayView)
cameraController = LifecycleCameraController(this)
cameraController.bindToLifecycle(this)
previewView = findViewById(R.id.previewView)
previewView.controller = cameraController
listenBarcodeScanning()
}
override fun onResume() {
super.onResume()
graphicOverlay.clear()
}
private fun listenBarcodeScanning() {
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.build()
val barcodeScanner = BarcodeScanning.getClient(options)
cameraController.setImageAnalysisAnalyzer(
ContextCompat.getMainExecutor(this),
MlKitAnalyzer(
listOf(barcodeScanner),
CameraController.COORDINATE_SYSTEM_VIEW_REFERENCED,
ContextCompat.getMainExecutor(this)
) { result ->
result.getValue(barcodeScanner)?.let { barcodeList ->
graphicOverlay.clear()
barcodeList.forEach { barcode ->
//barcode还包含其他一些信息,比如根据位置,区域等可以绘制自己的UI
println("### ${barcode.rawValue}")
graphicOverlay.add(BarcodeGraphic(graphicOverlay, barcode))
}
graphicOverlay.invalidate()
}
})
}
}
最后附上效果图
最后
以上就是开朗刺猬为你收集整理的Android摄像头识别多个二维码并绘制识别区域的全部内容,希望文章能够帮你解决Android摄像头识别多个二维码并绘制识别区域所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复