最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

 更新時(shí)間:2023年08月21日 09:08:44   作者:愿天深海  
這篇文章將借助開(kāi)源庫(kù)?MLKit?實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功,該庫(kù)的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測(cè)等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下

本篇文章Demo下載

上一篇文章中,講到了基于ZXing實(shí)現(xiàn)二維碼生成&掃描,其中掃描二維碼分為使用相機(jī)掃描二維碼和從相冊(cè)中識(shí)別二維碼圖片兩部分,但是從相冊(cè)中識(shí)別二維碼圖片,發(fā)現(xiàn)存在識(shí)別失敗的問(wèn)題,尤其是商品條形碼,使用相機(jī)掃描商品條形碼是可以正常掃描識(shí)別出來(lái)的,但是將商品條形碼拍照保存進(jìn)相冊(cè),使用從相冊(cè)中識(shí)別二維碼圖片方法,卻出現(xiàn)識(shí)別失敗的情況。

為此,又去查找了其他的資料,本篇借助開(kāi)源庫(kù) MLKit 實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功。

更多的使用可以下載源碼工程跑下樣例查看,該庫(kù)的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測(cè)等等,條碼識(shí)別也支持多張二維碼掃描識(shí)別、獲得二維碼圖片等等。

該篇文章就只介紹最基本的條形碼掃描使用。

引入庫(kù):

//公共庫(kù)
implementation ''com.github.jenly1314.MLKit:mlkit-common:2.0.0'
//條碼識(shí)別
implementation 'com.github.jenly1314.MLKit:mlkit-barcode-scanning:2.0.0'

使用相機(jī)掃描二維碼

自定義BarcodeScanningActivity繼承BarcodeCameraScanActivity,BarcodeCameraScanActivity為相機(jī)掃描條碼基類(lèi),通過(guò)繼承可以快速實(shí)現(xiàn)相機(jī)掃描條碼自定義頁(yè)面:

class BarcodeScanningActivity : BarcodeCameraScanActivity() {
    override fun initCameraScan(cameraScan: CameraScan<MutableList<Barcode>>) {
        super.initCameraScan(cameraScan)
        cameraScan.setPlayBeep(true)
            .setVibrate(true)
    }
    override fun onScanResultCallback(result: AnalyzeResult<MutableList<Barcode>>) {
        cameraScan.setAnalyzeImage(false)
        Toast.makeText(this, "result:${result.result[0].displayValue}", Toast.LENGTH_SHORT).show()
    }
    override fun getLayoutId(): Int = R.layout.custom_camera_scan
    override fun getFlashlightId(): Int = View.NO_ID
}

在onScanResultCallback方法中我們可以得到掃描結(jié)果。

覆寫(xiě)getLayoutId方法,傳入自定義頁(yè)面布局,

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.camera.view.PreviewView
        android:id="@+id/previewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.king.view.viewfinderview.ViewfinderView
        android:id="@+id/viewfinderView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- 只需保證有布局內(nèi)有PreviewView即可,然后自己可根據(jù)需要添加的控件 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Put the barcode in the window"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="50dp"
        android:textColor="@color/white"/>
</FrameLayout>

使用時(shí),直接跳轉(zhuǎn)自定義掃描頁(yè)面即可:

        findViewById<Button>(R.id.bt).setOnClickListener {
            startActivity(Intent(this, BarcodeScanningActivity::class.java))
        }

從相冊(cè)中識(shí)別二維碼圖片

打開(kāi)相冊(cè)獲取圖片:

    private fun openGallery() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        openGalleryRequest.launch(Intent.createChooser(intent, "識(shí)別相冊(cè)二維碼圖片"))
    }
    private val openGalleryRequest =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK) {
                it.data?.data?.let { uri -> handleImage(uri) }
            }
        }

解析圖片二維碼:

    private fun processPhoto(data: Uri?) {
        data?.let {
            try {
                val srcBitmap = MediaStore.Images.Media.getBitmap(contentResolver, it)
                BarcodeDecoder.process(srcBitmap, object : Analyzer.OnAnalyzeListener<List<Barcode>?> {
                    override fun onSuccess(result: List<Barcode>) {
                        if (result.isNotEmpty()) {
                            Toast.makeText(this@MainActivity, "result:${result[0].displayValue}", Toast.LENGTH_SHORT).show()
                        } else {
                            Toast.makeText(this@MainActivity, "result is null", Toast.LENGTH_SHORT).show()
                        }
                    }
                    override fun onFailure(e: Exception?) {
                        Toast.makeText(this@MainActivity, "onFailure", Toast.LENGTH_SHORT).show()
                    }
                    // 如果指定具體的識(shí)別條碼類(lèi)型,速度會(huì)更快
                }, Barcode.FORMAT_ALL_FORMATS)
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT).show()
            }
        }
    }

到此這篇關(guān)于Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例的文章就介紹到這了,更多相關(guān)Android MLKit實(shí)現(xiàn)條形碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

景宁| 额敏县| 和硕县| 揭东县| 新密市| 锦州市| 巫山县| 长武县| 开封市| 临安市| 大田县| 时尚| 卓尼县| 紫阳县| 贵州省| 稻城县| 汶上县| 清徐县| 丰城市| 昭平县| 台山市| 株洲县| 沂水县| 张家川| 综艺| 临夏市| 开封市| 华蓥市| 绵阳市| 五华县| 武川县| 运城市| 镇巴县| 南江县| 罗定市| 石狮市| 凭祥市| 香格里拉县| 通州区| 类乌齐县| 太原市|