|
| 1 | +[](https://jitpack.io/#javaherisaber/MLBarcodeScanner) |
1 | 2 | # MLBarcodeScanner
|
2 |
| -A demo project to show how to implement barcode scanner using Google ML-Kit Vision api |
| 3 | +Android barcode scanner using Google ML-Kit Vision |
3 | 4 |
|
4 |
| -<img src="Screenshot.png" width="256" height="455"> |
| 5 | +<img src="Screenshot.jpg" width="256" height="455"> |
| 6 | + |
| 7 | +# Usage |
| 8 | +## Dependency |
| 9 | +Top level build.gradle |
| 10 | +```groovy |
| 11 | +allprojects { |
| 12 | + repositories { |
| 13 | + maven { url 'https://jitpack.io' } |
| 14 | + // other repositories |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +Module level build.gradle |
| 20 | +```groovy |
| 21 | +dependencies { |
| 22 | + implementation "com.github.javaherisaber:MLBarcodeScanner:1.0.0" |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Create your layout |
| 27 | +```xml |
| 28 | +<androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent" |
| 29 | + android:layout_width="match_parent" |
| 30 | + xmlns:android="http://schemas.android.com/apk/res/android" |
| 31 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 32 | + xmlns:tools="http://schemas.android.com/tools"> |
| 33 | + |
| 34 | + <androidx.camera.view.PreviewView |
| 35 | + android:id="@+id/previewView_cameraScanning" |
| 36 | + android:layout_height="0dp" |
| 37 | + android:layout_width="match_parent" |
| 38 | + app:layout_constraintTop_toTopOf="parent" |
| 39 | + app:layout_constraintBottom_toBottomOf="parent" /> |
| 40 | + |
| 41 | + <com.buildtoapp.mlbarcodescanner.GraphicOverlay |
| 42 | + android:id="@+id/graphic_overlay" |
| 43 | + android:layout_height="0dp" |
| 44 | + android:layout_width="0dp" |
| 45 | + app:layout_constraintBottom_toBottomOf="@id/previewView_cameraScanning" |
| 46 | + app:layout_constraintLeft_toLeftOf="@id/previewView_cameraScanning" |
| 47 | + app:layout_constraintRight_toRightOf="@id/previewView_cameraScanning" |
| 48 | + app:layout_constraintTop_toTopOf="@id/previewView_cameraScanning" /> |
| 49 | + |
| 50 | + <ImageView |
| 51 | + android:layout_height="264dp" |
| 52 | + android:layout_width="264dp" |
| 53 | + android:src="@drawable/ic_scan_area" |
| 54 | + app:layout_constraintBottom_toBottomOf="parent" |
| 55 | + app:layout_constraintEnd_toEndOf="parent" |
| 56 | + app:layout_constraintStart_toStartOf="parent" |
| 57 | + app:layout_constraintTop_toTopOf="parent" |
| 58 | + tools:ignore="ContentDescription" /> |
| 59 | + |
| 60 | +</androidx.constraintlayout.widget.ConstraintLayout> |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +Make sure to grant `android.permission.CAMERA` and then use the library |
| 65 | +```kotlin |
| 66 | +class MainActivity : AppCompatActivity() { |
| 67 | + private lateinit var binding: ActivityMainBinding |
| 68 | + private lateinit var barcodeScanner: MLBarcodeScanner |
| 69 | + |
| 70 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 71 | + super.onCreate(savedInstanceState) |
| 72 | + binding = ActivityMainBinding.inflate(layoutInflater) |
| 73 | + setContentView(binding.root) |
| 74 | + |
| 75 | + if (!PermissionUtils.allRuntimePermissionsGranted(this, REQUIRED_RUNTIME_PERMISSIONS)) { |
| 76 | + PermissionUtils.getRuntimePermissions(this, REQUIRED_RUNTIME_PERMISSIONS) |
| 77 | + } |
| 78 | + initBarcodeScanner() |
| 79 | + } |
| 80 | + |
| 81 | + private fun initBarcodeScanner() { |
| 82 | + barcodeScanner = MLBarcodeScanner( |
| 83 | + callback = { displayValue, rawValue -> |
| 84 | + // you can process your barcode here |
| 85 | + }, |
| 86 | + focusBoxSize = MetricUtils.dpToPx(264), |
| 87 | + graphicOverlay = binding.graphicOverlay, |
| 88 | + previewView = binding.previewViewCameraScanning, |
| 89 | + lifecycleOwner = this, |
| 90 | + context = this, |
| 91 | + drawOverlay = true, // show rectangle around detected barcode |
| 92 | + drawBanner = true // show detected barcode value on top of it |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + companion object { |
| 97 | + private val REQUIRED_RUNTIME_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
5 | 101 |
|
6 | 102 | # Supported barcode types
|
7 | 103 | - **2D formats**: QR Code, Aztec, Data Matrix, PDF417
|
8 | 104 | - **Linear formats**: Codabar, Code 39, Code 93, Code 128, EAN-8, EAN-13, EAN-128, ITF, UPC-A, UPC-E
|
9 | 105 |
|
10 |
| -# Usage |
| 106 | +# Model types |
11 | 107 | There are two types of dependency for barcode scanning using ML-Kit vision
|
12 | 108 |
|
13 | 109 | ## Bundled model
|
|
0 commit comments