Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
本篇文章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)文章
Android用TextView實(shí)現(xiàn)跑馬燈效果代碼
大家好,本篇文章主要講的是Android?TextView實(shí)現(xiàn)跑馬燈效果代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)小米相機(jī)底部滑動(dòng)指示器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Retrofit網(wǎng)絡(luò)請(qǐng)求框架之注解解析和動(dòng)態(tài)代理
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請(qǐng)求框架之注解解析和動(dòng)態(tài)代理,Retrofit是目前Android平臺(tái)上比較流行的網(wǎng)絡(luò)請(qǐng)求框架之一,它提供了一種簡(jiǎn)潔、靈活的方式來(lái)處理HTTP請(qǐng)求和響應(yīng)2023-03-03
Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面
這篇文章主要介紹了Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01
Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單實(shí)例
本篇文章主要介紹了Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06

