Android摄像头识别多个二维码并绘制识别区域

说起Android扫描二维码,可能很多人都会想到用zxing,个人感觉不是很好用,后来使用华为的“统一扫码服务”,用着还行,不用开发者再辛苦钻研不同手机的分辨率以及方向了,如今机器学习热度这么高,各大厂商已经把条码识别归入机器学习SDK,本文要介绍的就是使用谷歌的 CameraX 以及机器学习,实现二维码识别,超级简单!

 

1、相机权限那是少不了的,添加如下权限:

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、布局文件




    

    

 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摄像头识别多个二维码并绘制识别区域_第1张图片

你可能感兴趣的:(android)