This articleDemo Download
existPrevious articleIn this article, it is about realizing QR code generation and scanning based on ZXing. Among them, scanning QR codes is divided into two parts: scanning QR codes with a camera and identifying QR code pictures from the album. However, identifying QR code pictures from the album, and finding that there is a problem of failure in recognition, especially product barcodes. Using the camera to scan product barcodes can be scanned and recognized normally, but using the method of identifying QR code pictures from the album, the recognition failure occurs.
For this reason, I searched for other information. This article uses the help of the open source library.MLKitRealize barcode scanning and successfully identify product barcodes.
For more use, you can download the source code project and run the sample to view it. The library has a rich content. In addition to barcode recognition, there are also text recognition, image marking, face detection, etc. Barcode recognition also supports scanning and recognition of multiple QR codes, obtaining QR code pictures, etc.
This article only introduces the most basic barcode scanning use.
Introducing the library:
//Public libraryimplementation ''.:mlkit-common:2.0.0' //Barcode identificationimplementation '.:mlkit-barcode-scanning:2.0.0'
Scan the QR code using the camera
Custom BarcodeScanningActivity inherits BarcodeCameraScanActivity. BarcodeCameraScanActivity is the base class for scanning barcodes for cameras. Through inheritance, you can quickly realize the custom page for scanning barcodes for cameras:
class BarcodeScanningActivity : BarcodeCameraScanActivity() { override fun initCameraScan(cameraScan: CameraScan<MutableList<Barcode>>) { (cameraScan) (true) .setVibrate(true) } override fun onScanResultCallback(result: AnalyzeResult<MutableList<Barcode>>) { (false) (this, "result:${[0].displayValue}", Toast.LENGTH_SHORT).show() } override fun getLayoutId(): Int = .custom_camera_scan override fun getFlashlightId(): Int = View.NO_ID }
In the onScanResultCallback method we can get the scan result.
Override the getLayoutId method and pass in the custom page layout.
<?xml version="1.0" encoding="UTF-8"?> <FrameLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> < android: android:layout_width="match_parent" android:layout_height="match_parent"/> < android: android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- Just make sure there is a layout insidePreviewViewJust,Then you can add the controls as needed --> <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>
When using it, just jump to the custom scan page:
findViewById<Button>().setOnClickListener { startActivity(Intent(this, BarcodeScanningActivity::)) }
Identify QR code pictures from albums
Open the album to get pictures:
private fun openGallery() { val intent = Intent() = "image/*" = Intent.ACTION_GET_CONTENT ((intent, "Identify album QR code pictures")) }
private val openGalleryRequest = registerForActivityResult(()) { if ( == RESULT_OK) { ?.data?.let { uri -> handleImage(uri) } } }
Analyze the picture QR code:
private fun processPhoto(data: Uri?) { data?.let { try { val srcBitmap = (contentResolver, it) (srcBitmap, object : <List<Barcode>?> { override fun onSuccess(result: List<Barcode>) { if (()) { (this@MainActivity, "result:${result[0].displayValue}", Toast.LENGTH_SHORT).show() } else { (this@MainActivity, "result is null", Toast.LENGTH_SHORT).show() } } override fun onFailure(e: Exception?) { (this@MainActivity, "onFailure", Toast.LENGTH_SHORT).show() } // If you specify the specific identification barcode type, the speed will be faster }, Barcode.FORMAT_ALL_FORMATS) } catch (e: Exception) { () (this@MainActivity, , Toast.LENGTH_SHORT).show() } } }
This is the article about the code examples of Android's implementation of barcode scanning based on MLKit. For more related content on Android MLKit implementation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!