Android基于MLKit實現(xiàn)條形碼掃碼的代碼示例
本篇文章Demo下載
在上一篇文章中,講到了基于ZXing實現(xiàn)二維碼生成&掃描,其中掃描二維碼分為使用相機掃描二維碼和從相冊中識別二維碼圖片兩部分,但是從相冊中識別二維碼圖片,發(fā)現(xiàn)存在識別失敗的問題,尤其是商品條形碼,使用相機掃描商品條形碼是可以正常掃描識別出來的,但是將商品條形碼拍照保存進(jìn)相冊,使用從相冊中識別二維碼圖片方法,卻出現(xiàn)識別失敗的情況。
為此,又去查找了其他的資料,本篇借助開源庫 MLKit 實現(xiàn)條形碼掃描,對于商品條形碼也可以很好地識別成功。
更多的使用可以下載源碼工程跑下樣例查看,該庫的使用內(nèi)容非常豐富,除了條碼識別,還有文字識別、圖像標(biāo)記、人臉檢測等等,條碼識別也支持多張二維碼掃描識別、獲得二維碼圖片等等。
該篇文章就只介紹最基本的條形碼掃描使用。
引入庫:
//公共庫 implementation ''com.github.jenly1314.MLKit:mlkit-common:2.0.0' //條碼識別 implementation 'com.github.jenly1314.MLKit:mlkit-barcode-scanning:2.0.0'
使用相機掃描二維碼
自定義BarcodeScanningActivity繼承BarcodeCameraScanActivity,BarcodeCameraScanActivity為相機掃描條碼基類,通過繼承可以快速實現(xiàn)相機掃描條碼自定義頁面:
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方法,傳入自定義頁面布局,
<?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>
使用時,直接跳轉(zhuǎn)自定義掃描頁面即可:
findViewById<Button>(R.id.bt).setOnClickListener { startActivity(Intent(this, BarcodeScanningActivity::class.java)) }
從相冊中識別二維碼圖片
打開相冊獲取圖片:
private fun openGallery() { val intent = Intent() intent.type = "image/*" intent.action = Intent.ACTION_GET_CONTENT openGalleryRequest.launch(Intent.createChooser(intent, "識別相冊二維碼圖片")) }
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() } // 如果指定具體的識別條碼類型,速度會更快 }, Barcode.FORMAT_ALL_FORMATS) } catch (e: Exception) { e.printStackTrace() Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT).show() } } }
到此這篇關(guān)于Android基于MLKit實現(xiàn)條形碼掃碼的代碼示例的文章就介紹到這了,更多相關(guān)Android MLKit實現(xiàn)條形碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Android自定義ViewGroup實現(xiàn)標(biāo)簽流效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實現(xiàn)標(biāo)簽流效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06詳解Android 在 ViewPager 中使用 Fragment 的懶加載
本篇文章主要介紹了Android 在 ViewPager 中使用 Fragment 的懶加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Android ExpandableListView單選以及多選實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android ExpandableListView單選以及多選的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Android中使用socket通信實現(xiàn)消息推送的方法詳解
這篇文章主要介紹了Android中使用socket通信實現(xiàn)消息推送的方法,文中舉了一個消息發(fā)送端和一個消息接收端以及服務(wù)器端的例子來說明原理并且展示了客戶端的實現(xiàn),需要的朋友可以參考下2016-04-04探討Android 的屏幕滾動操作不如 iPhone 流暢順滑的原因
雖然很多Android手機的配置都比iPhone要高,比如大多數(shù)Andorid手機的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機在使用的時候總感覺沒有那么順滑,究竟為什么會出現(xiàn)這種現(xiàn)象呢?2014-07-07android開發(fā)環(huán)境搭建詳解(eclipse + android sdk)
這篇文章主要介紹了android開發(fā)環(huán)境搭建詳解(eclipse + android sdk),需要的朋友可以參考下2014-05-05