Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/snorsnor9998/barcode-scanning-with-mlkit
Showcase of barcode scanning using Google MLKIT and CameraX
https://github.com/snorsnor9998/barcode-scanning-with-mlkit
android barcode barcode-scanner camerax camerax-sample kotlin kotlin-android mlkit-android qrcode-reader qrcode-scanner
Last synced: about 1 month ago
JSON representation
Showcase of barcode scanning using Google MLKIT and CameraX
- Host: GitHub
- URL: https://github.com/snorsnor9998/barcode-scanning-with-mlkit
- Owner: SnorSnor9998
- Created: 2021-11-09T04:00:19.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-01T07:53:11.000Z (about 1 year ago)
- Last Synced: 2023-12-01T08:42:44.124Z (about 1 year ago)
- Topics: android, barcode, barcode-scanner, camerax, camerax-sample, kotlin, kotlin-android, mlkit-android, qrcode-reader, qrcode-scanner
- Language: Kotlin
- Homepage:
- Size: 24.2 MB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Barcode-Scanning-with-MLKIT
Showcase of barcode scanning using Google MLKIT and CameraX
## 📕 Dependencies
```kotlin
// View Binding
implementation("com.github.yogacp:android-viewbinding:1.0.4")// Camera API
implementation("androidx.camera:camera-camera2:1.2.1")
implementation("androidx.camera:camera-lifecycle:1.2.1")
implementation("androidx.camera:camera-view:1.2.1")// MLKit
implementation("com.google.mlkit:barcode-scanning:17.0.3")
```Forgive me im lazy, so im just do some shortcut on viewbinding by using this [library](https://github.com/yogacp/android-viewbinding).
## ⚙ Manifest
```xml
```
And just copy
- CamActivity
- Image Convertor
- BarcodeAnalyzer### ⚠️ ️Update (19/10/2022) ⚠️
There is a bug that will cause a crash when you start the app, apparently the preview is only able to show on Android Emulator but not on a real device.
So if you want adjust the scan box size or customize the box please do it on emulator.
DO NOT PASS THE IMAGE OUT FROM BARCODEANALYZER.
[Preview Version](https://github.com/SnorSnor9998/Barcode-Scanning-with-MLKIT/tree/preview)## 🏃♂️ How to start
```kotlin
binding.btnStart.setOnClickListener {
val i = Intent(this, CamActivity::class.java)
i.putExtra("title", "Example")
i.putExtra("msg", "Scan Barcode")
getContent.launch(i)
}```
How you get your result
```kotlin
private val getContent =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val barcode = it?.data?.getStringExtra("BarcodeResult")
//In Android Emulator Only
//val pic = it?.data?.getStringExtra("Image")
//val image = B64Image.decode(pic.toString())
//binding.imgResult.setImageBitmap(image)binding.txtResult.text = barcode
}
}```
## 📺 Screenshot
## ⚠ About Overlay ⚠
If you planning to scan the barcode inside the box than pay attention on `activity_cam.xml`
```xml
```
Instead giving a fix width and height, i use percent for some purpose.
The way i approch is bit dumb but is the most easy way to do so you can understand more easily (a complex code doesn't show how pro you are.)In order to get image inside the box we have to crop it, but how?
### 1️⃣ First
Inside `BarcodeAnalyzer` before giving the image to `scanner.process()` the image have to be crop.
How i calculate?
But sure this method will not always perfect, but it work most of the time and i dont give a damn, you can do some minor changes by add/minus value at the back to adjust the rectangle.
### ⚠️ ️Update (13/10/2022) ⚠️
I realize the old version that image end up rotate anti-clockwise 90 degree and end up some device had some issue scanning barcode like CODE-128.
So in the end we need to rotate the image 90 degree clockwise before feed to the scanner.First, getting the height and width of the picture
```kotlin
val height = mediaImage.height
val width = mediaImage.width
```U can refer [this](https://stackoverflow.com/a/26253377) about `Rect()`
```kotlin
//Since in the end the image will rotate clockwise 90 degree
//left -> top, top -> right, right -> bottom, bottom -> left//Top : (far) -value > 0 > +value (closer)
val c1x = (width * 0.125).toInt() + 150
//Right : (far) -value > 0 > +value (closer)
val c1y = (height * 0.25).toInt() - 25
//Bottom : (closer) -value > 0 > +value (far)
val c2x = (width * 0.875).toInt() - 150
//Left : (closer) -value > 0 > +value (far)
val c2y = (height * 0.75).toInt() + 25val rect = Rect(c1x, c1y, c2x, c2y)
```
### 2️⃣ Second
Thanks [this](https://stackoverflow.com/a/62105972) amazing human, convert ImageProxy to bitmap
```kotlin
val ori: Bitmap = imageProxy.toBitmap()!!
```So we create a new image which is crop version from the original image than we need rotate the image.
```kotlin
val crop = Bitmap.createBitmap(ori, rect.left, rect.top, rect.width(), rect.height())
val rImage = crop.rotate(90F)val image: InputImage =
InputImage.fromBitmap(rImage, imageProxy.imageInfo.rotationDegrees)// Pass image to the scanner and have it do its thing
scanner.process(image)
.addOnSuccessListener { barcodes ->
// Task completed successfully
for (barcode in barcodes) {
//For Android Emulator Only
//barcodeListener(barcode.rawValue ?: "", image.bitmapInternal!!)
barcodeListener(barcode.rawValue ?: "")
imageProxy.close()
}
}
.addOnFailureListener {
// You should really do something about Exceptions
imageProxy.close()
}
.addOnCompleteListener {
// It's important to close the imageProxy
imageProxy.close()
}```
### Snap Snap? The End.....That all. Happy Coding & keep suffering your life.
### Side note 📝
- if you wonder all those conversions ( `.toBitmap()` & `.rotate()`) will affect the performance don't worry i test on my cheap ass phone (please donate me money) it only take average `0.1 sec` to process.
- And there is some [guidelines](https://developers.google.com/ml-kit/vision/barcode-scanning/android#input-image-guidelines) you can follow and some tips for [performance](https://developers.google.com/ml-kit/vision/barcode-scanning/android#performance-tips)
- `.setTargetResolution` to 1080p for most of the case is really enough epically some high end phone like Samsung S20 it run buttery smooth and fast.