{"id":21446264,"url":"https://github.com/snorsnor9998/barcode-scanning-with-mlkit","last_synced_at":"2025-07-14T19:31:15.998Z","repository":{"id":164877026,"uuid":"426084864","full_name":"SnorSnor9998/Barcode-Scanning-with-MLKIT","owner":"SnorSnor9998","description":"Showcase of barcode scanning using Google MLKIT and CameraX","archived":false,"fork":false,"pushed_at":"2023-12-01T07:53:11.000Z","size":25407,"stargazers_count":23,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T13:52:44.689Z","etag":null,"topics":["android","barcode","barcode-scanner","camerax","camerax-sample","kotlin","kotlin-android","mlkit-android","qrcode-reader","qrcode-scanner"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SnorSnor9998.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-11-09T04:00:19.000Z","updated_at":"2025-03-07T02:06:35.000Z","dependencies_parsed_at":"2023-12-01T08:38:43.173Z","dependency_job_id":"ff828891-6eb3-48be-b90b-732736ce9fcc","html_url":"https://github.com/SnorSnor9998/Barcode-Scanning-with-MLKIT","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/SnorSnor9998/Barcode-Scanning-with-MLKIT","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnorSnor9998%2FBarcode-Scanning-with-MLKIT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnorSnor9998%2FBarcode-Scanning-with-MLKIT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnorSnor9998%2FBarcode-Scanning-with-MLKIT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnorSnor9998%2FBarcode-Scanning-with-MLKIT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SnorSnor9998","download_url":"https://codeload.github.com/SnorSnor9998/Barcode-Scanning-with-MLKIT/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnorSnor9998%2FBarcode-Scanning-with-MLKIT/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265336316,"owners_count":23749176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","barcode","barcode-scanner","camerax","camerax-sample","kotlin","kotlin-android","mlkit-android","qrcode-reader","qrcode-scanner"],"created_at":"2024-11-23T02:42:37.455Z","updated_at":"2025-07-14T19:31:15.288Z","avatar_url":"https://github.com/SnorSnor9998.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Barcode-Scanning-with-MLKIT\n\nShowcase of barcode scanning using Google MLKIT and CameraX\n\n## 📕 Dependencies\n\n```kotlin\n// View Binding\nimplementation(\"com.github.yogacp:android-viewbinding:1.0.4\")\n\n// Camera API\nimplementation(\"androidx.camera:camera-camera2:1.2.1\")\nimplementation(\"androidx.camera:camera-lifecycle:1.2.1\")\nimplementation(\"androidx.camera:camera-view:1.2.1\")\n\n// MLKit\nimplementation(\"com.google.mlkit:barcode-scanning:17.0.3\")\n```\n\nForgive me im lazy, so im just do some shortcut on viewbinding by using this [library](https://github.com/yogacp/android-viewbinding).\n\n## ⚙ Manifest\n\n```xml\n\u003cuses-permission android:name=\"android.permission.FLASHLIGHT\" /\u003e\n\u003cuses-permission android:name=\"android.permission.CAMERA\" /\u003e\n\u003cuses-feature android:name=\"android.hardware.camera.any\" /\u003e\n\n\n\n\u003capplication\n    ....\n    \n    \u003cactivity android:name=\".CamActivity\"\n              android:exported=\"false\"/\u003e\n    \n\u003c/application\u003e\n\n```\n\n\u003c/br\u003e\n\nAnd just copy\n- CamActivity\n- Image Convertor\n- BarcodeAnalyzer\n\n\u003c/br\u003e\n\n\n### ⚠️ ️Update (19/10/2022) ⚠️\nThere 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.\u003c/br\u003e\nSo if you want adjust the scan box size or customize the box please do it on emulator.\u003c/br\u003e\nDO NOT PASS THE IMAGE OUT FROM BARCODEANALYZER.\u003c/br\u003e\n[Preview Version](https://github.com/SnorSnor9998/Barcode-Scanning-with-MLKIT/tree/preview)\n\u003c/br\u003e\n\n## 🏃‍♂️ How to start \u003c/br\u003e\n\n```kotlin\nbinding.btnStart.setOnClickListener {\n    val i = Intent(this, CamActivity::class.java)\n    i.putExtra(\"title\", \"Example\")\n    i.putExtra(\"msg\", \"Scan Barcode\")\n    getContent.launch(i)\n}\n\n```\n\nHow you get your result\n\n```kotlin\nprivate val getContent =\n    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {\n        if (it.resultCode == Activity.RESULT_OK) {\n            val barcode = it?.data?.getStringExtra(\"BarcodeResult\")\n            \n            //In Android Emulator Only\n            //val pic = it?.data?.getStringExtra(\"Image\")\n            //val image = B64Image.decode(pic.toString())\n            //binding.imgResult.setImageBitmap(image)\n\n            binding.txtResult.text = barcode\n            \n\n        }\n    }\n\n```\n\n## 📺 Screenshot\n\n\u003cimg src=\"screenshot/gif1.gif\" width=\"300\" height=\"600\"/\u003e \u003c/br\u003e\n\n\u003c/br\u003e\n\n## ⚠ About Overlay ⚠\n\nIf you planning to scan the barcode inside the box than pay attention on `activity_cam.xml`\n\n```xml\n    \u003cView\n        android:id=\"@+id/img_qr_box\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"0dp\"\n        app:layout_constraintWidth_percent=\"0.75\"\n        app:layout_constraintHeight_percent=\"0.5\"\n        android:elevation=\"5dp\"\n        android:background=\"@drawable/scanning_box\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" /\u003e\n\n```\n\nInstead giving a fix width and height, i use percent for some purpose.\nThe 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.)\n\u003c/br\u003e\n\nIn order to get image inside the box we have to crop it, but how? \n\u003c/br\u003e\n\n### 1️⃣ First\n\nInside `BarcodeAnalyzer` before giving the image to `scanner.process()` the image have to be crop.\u003c/br\u003e\n\nHow i calculate?\n\n\u003cimg src=\"screenshot/ss2.png\" width=\"300\" height=\"500\"/\u003e\n\nBut 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.\u003c/br\u003e\n\n### ⚠️ ️Update (13/10/2022) ⚠️\nI 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. \u003c/br\u003e\nSo in the end we need to rotate the image 90 degree clockwise before feed to the scanner.\n\n\nFirst, getting the height and width of the picture\n\n```kotlin\nval height = mediaImage.height\nval width = mediaImage.width\n```\n\nU can refer [this](https://stackoverflow.com/a/26253377) about `Rect()`\n\n```kotlin\n//Since in the end the image will rotate clockwise 90 degree\n//left -\u003e top, top -\u003e right, right -\u003e bottom, bottom -\u003e left\n\n//Top    : (far) -value \u003e 0 \u003e +value (closer)\nval c1x = (width * 0.125).toInt() + 150\n//Right  : (far) -value \u003e 0 \u003e +value (closer)\nval c1y = (height * 0.25).toInt() - 25\n//Bottom : (closer) -value \u003e 0 \u003e +value (far)\nval c2x = (width * 0.875).toInt() - 150\n//Left   : (closer) -value \u003e 0 \u003e +value (far)\nval c2y = (height * 0.75).toInt() + 25\n\nval rect = Rect(c1x, c1y, c2x, c2y)\n\n```\n\n\n### 2️⃣ Second\n\nThanks [this](https://stackoverflow.com/a/62105972) amazing human, convert ImageProxy to bitmap\n\n```kotlin\nval ori: Bitmap = imageProxy.toBitmap()!!\n```\n\nSo we create a new image which is crop version from the original image than we need rotate the image.\n\n```kotlin\nval crop = Bitmap.createBitmap(ori, rect.left, rect.top, rect.width(), rect.height())\nval rImage = crop.rotate(90F)\n\nval image: InputImage =\n    InputImage.fromBitmap(rImage, imageProxy.imageInfo.rotationDegrees)\n\n// Pass image to the scanner and have it do its thing\nscanner.process(image)\n    .addOnSuccessListener { barcodes -\u003e\n        // Task completed successfully\n        for (barcode in barcodes) {\n            //For Android Emulator Only\n            //barcodeListener(barcode.rawValue ?: \"\", image.bitmapInternal!!)\n            barcodeListener(barcode.rawValue ?: \"\")\n            imageProxy.close()\n        }\n    }\n    .addOnFailureListener {\n        // You should really do something about Exceptions\n        imageProxy.close()\n    }\n    .addOnCompleteListener {\n        // It's important to close the imageProxy\n        imageProxy.close()\n    }\n\n```\n### Snap Snap? The End.....\n\nThat all. Happy Coding \u0026 keep suffering your life. \u003c/br\u003e\n### Side note 📝\n- if you wonder all those conversions ( `.toBitmap()` \u0026 `.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.\n- 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) \u003c/br\u003e\n- `.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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnorsnor9998%2Fbarcode-scanning-with-mlkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnorsnor9998%2Fbarcode-scanning-with-mlkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnorsnor9998%2Fbarcode-scanning-with-mlkit/lists"}