Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
本篇文章Demo下載
在上一篇文章中,講到了基于ZXing實(shí)現(xiàn)二維碼生成&掃描,其中掃描二維碼分為使用相機(jī)掃描二維碼和從相冊(cè)中識(shí)別二維碼圖片兩部分,但是從相冊(cè)中識(shí)別二維碼圖片,發(fā)現(xiàn)存在識(shí)別失敗的問題,尤其是商品條形碼,使用相機(jī)掃描商品條形碼是可以正常掃描識(shí)別出來的,但是將商品條形碼拍照保存進(jìn)相冊(cè),使用從相冊(cè)中識(shí)別二維碼圖片方法,卻出現(xiàn)識(shí)別失敗的情況。
為此,又去查找了其他的資料,本篇借助開源庫(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ī)掃描條碼基類,通過繼承可以快速實(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é)果。
覆寫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í)別二維碼圖片
打開相冊(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í)別條碼類型,速度會(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復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
詳解Android 在 ViewPager 中使用 Fragment 的懶加載
本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android Application級(jí)別自定義Toast
這篇文章主要為大家詳細(xì)介紹了Android Application級(jí)別自定義Toast,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android ExpandableListView單選以及多選的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Flutter質(zhì)感設(shè)計(jì)之表單輸入
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之表單輸入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android中使用socket通信實(shí)現(xiàn)消息推送的方法詳解
這篇文章主要介紹了Android中使用socket通信實(shí)現(xiàn)消息推送的方法,文中舉了一個(gè)消息發(fā)送端和一個(gè)消息接收端以及服務(wù)器端的例子來說明原理并且展示了客戶端的實(shí)現(xiàn),需要的朋友可以參考下2016-04-04
探討Android 的屏幕滾動(dòng)操作不如 iPhone 流暢順滑的原因
雖然很多Android手機(jī)的配置都比iPhone要高,比如大多數(shù)Andorid手機(jī)的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機(jī)在使用的時(shí)候總感覺沒有那么順滑,究竟為什么會(huì)出現(xiàn)這種現(xiàn)象呢?2014-07-07
android開發(fā)環(huán)境搭建詳解(eclipse + android sdk)
這篇文章主要介紹了android開發(fā)環(huán)境搭建詳解(eclipse + android sdk),需要的朋友可以參考下2014-05-05

