├── .gitignore ├── README.md ├── Screenshot.jpg ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── nl │ │ └── storegear │ │ └── android │ │ └── mlbarcodescanner │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── nl │ │ │ └── storegear │ │ │ └── android │ │ │ └── mlbarcodescanner │ │ │ ├── MainActivity.kt │ │ │ └── util │ │ │ ├── MetricUtils.kt │ │ │ └── PermissionUtils.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_close.xml │ │ ├── ic_launcher_background.xml │ │ └── ic_scan_area.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── nl │ └── storegear │ └── android │ └── mlbarcodescanner │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── buildtoapp │ │ └── mlbarcodescanner │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── buildtoapp │ │ └── mlbarcodescanner │ │ ├── BarcodeGraphic.kt │ │ ├── BarcodeScannerProcessor.kt │ │ ├── BitmapUtils.java │ │ ├── CameraImageGraphic.java │ │ ├── CameraXViewModel.java │ │ ├── FrameMetadata.java │ │ ├── GraphicOverlay.java │ │ ├── MLBarcodeScanner.kt │ │ ├── ScopedExecutor.java │ │ ├── VisionImageProcessor.kt │ │ └── VisionProcessorBase.kt │ └── test │ └── java │ └── com │ └── buildtoapp │ └── mlbarcodescanner │ └── ExampleUnitTest.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | local.properties 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![](https://jitpack.io/v/javaherisaber/MLBarcodeScanner.svg)](https://jitpack.io/#javaherisaber/MLBarcodeScanner) 2 | [![Download](https://img.shields.io/badge/Android%20Arsenal-MLBarcodeScanner-green.svg)](https://android-arsenal.com/details/1/8461) 3 | 4 | # MLBarcodeScanner 5 | Android barcode scanner using Google ML-Kit Vision 6 | 7 | 8 | 9 | # Usage 10 | ## Dependency 11 | Top level build.gradle 12 | ```groovy 13 | allprojects { 14 | repositories { 15 | maven { url 'https://jitpack.io' } 16 | // other repositories 17 | } 18 | } 19 | ``` 20 | 21 | Module level build.gradle 22 | ```groovy 23 | dependencies { 24 | implementation "com.github.javaherisaber:MLBarcodeScanner:1.0.0" 25 | } 26 | ``` 27 | 28 | Create your layout 29 | ```xml 30 | 35 | 36 | 42 | 43 | 51 | 52 | 61 | 62 | 63 | 64 | ``` 65 | 66 | Make sure to grant `android.permission.CAMERA` and then use the library 67 | ```kotlin 68 | class MainActivity : AppCompatActivity() { 69 | private lateinit var binding: ActivityMainBinding 70 | private lateinit var barcodeScanner: MLBarcodeScanner 71 | 72 | override fun onCreate(savedInstanceState: Bundle?) { 73 | super.onCreate(savedInstanceState) 74 | binding = ActivityMainBinding.inflate(layoutInflater) 75 | setContentView(binding.root) 76 | 77 | if (!PermissionUtils.allRuntimePermissionsGranted(this, REQUIRED_RUNTIME_PERMISSIONS)) { 78 | PermissionUtils.getRuntimePermissions(this, REQUIRED_RUNTIME_PERMISSIONS) 79 | } 80 | initBarcodeScanner() 81 | } 82 | 83 | private fun initBarcodeScanner() { 84 | barcodeScanner = MLBarcodeScanner( 85 | callback = { displayValue, rawValue -> 86 | // you can process your barcode here 87 | }, 88 | focusBoxSize = MetricUtils.dpToPx(264), 89 | graphicOverlay = binding.graphicOverlay, 90 | previewView = binding.previewViewCameraScanning, 91 | lifecycleOwner = this, 92 | context = this, 93 | drawOverlay = true, // show rectangle around detected barcode 94 | drawBanner = true // show detected barcode value on top of it 95 | ) 96 | } 97 | 98 | companion object { 99 | private val REQUIRED_RUNTIME_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) 100 | } 101 | } 102 | ``` 103 | 104 | # Supported barcode types 105 | - **2D formats**: QR Code, Aztec, Data Matrix, PDF417 106 | - **Linear formats**: Codabar, Code 39, Code 93, Code 128, EAN-8, EAN-13, EAN-128, ITF, UPC-A, UPC-E 107 | 108 | # Model types 109 | There are two types of dependency for barcode scanning using ML-Kit vision 110 | 111 | ## Bundled model 112 | (with 3-10 MB increase in apk size) 113 | We added this model to the `debug variant` so that you don't have to wait for GooglePlay services to download it in your testing 114 | 115 | ## Unbundled 116 | (with no increase in apk size but downloaded from Google Play Services on demand) 117 | We added this model to the `release variant` for end users to reduce apk size 118 | 119 | # Reference 120 | - https://developers.google.com/ml-kit/vision/barcode-scanning/android 121 | - https://github.com/googlesamples/mlkit/tree/master/android/vision-quickstart 122 | -------------------------------------------------------------------------------- /Screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/Screenshot.jpg -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | namespace 'nl.storegear.android.mlbarcodescanner' 8 | compileSdk 32 9 | 10 | defaultConfig { 11 | applicationId "nl.storegear.android.mlbarcodescanner" 12 | minSdk 21 13 | targetSdk 32 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | buildFeatures { 20 | viewBinding true 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation project(path: ':lib') 39 | 40 | implementation 'androidx.core:core-ktx:1.7.0' 41 | implementation 'androidx.appcompat:appcompat:1.5.1' 42 | implementation 'com.google.android.material:material:1.6.1' 43 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 44 | testImplementation 'junit:junit:4.13.2' 45 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 46 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 47 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/nl/storegear/android/mlbarcodescanner/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package nl.storegear.android.mlbarcodescanner 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("nl.storegear.android.mlbarcodescanner", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/nl/storegear/android/mlbarcodescanner/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package nl.storegear.android.mlbarcodescanner 2 | 3 | import android.Manifest 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import com.buildtoapp.mlbarcodescanner.MLBarcodeCallback 7 | import com.buildtoapp.mlbarcodescanner.MLBarcodeScanner 8 | import nl.storegear.android.mlbarcodescanner.databinding.ActivityMainBinding 9 | import nl.storegear.android.mlbarcodescanner.util.MetricUtils 10 | import nl.storegear.android.mlbarcodescanner.util.PermissionUtils 11 | 12 | class MainActivity : AppCompatActivity(), MLBarcodeCallback { 13 | private lateinit var binding: ActivityMainBinding 14 | private lateinit var barcodeScanner: MLBarcodeScanner 15 | 16 | override fun onCreate(savedInstanceState: Bundle?) { 17 | super.onCreate(savedInstanceState) 18 | binding = ActivityMainBinding.inflate(layoutInflater) 19 | setContentView(binding.root) 20 | 21 | if (!PermissionUtils.allRuntimePermissionsGranted(this, REQUIRED_RUNTIME_PERMISSIONS)) { 22 | PermissionUtils.getRuntimePermissions(this, REQUIRED_RUNTIME_PERMISSIONS) 23 | } 24 | initBarcodeScanner() 25 | } 26 | 27 | private fun initBarcodeScanner() { 28 | barcodeScanner = MLBarcodeScanner( 29 | callback = this, 30 | focusBoxSize = MetricUtils.dpToPx(264), 31 | graphicOverlay = binding.graphicOverlay, 32 | previewView = binding.previewViewCameraScanning, 33 | lifecycleOwner = this, 34 | context = this, 35 | drawOverlay = true, // show rectangle around detected barcode 36 | drawBanner = true // show detected barcode value on top of it 37 | ) 38 | } 39 | 40 | override fun onNewBarcodeScanned(displayValue: String, rawValue: String) { 41 | // todo: you can process your barcode here 42 | } 43 | 44 | companion object { 45 | private val REQUIRED_RUNTIME_PERMISSIONS = arrayOf(Manifest.permission.CAMERA) 46 | } 47 | } -------------------------------------------------------------------------------- /app/src/main/java/nl/storegear/android/mlbarcodescanner/util/MetricUtils.kt: -------------------------------------------------------------------------------- 1 | package nl.storegear.android.mlbarcodescanner.util 2 | 3 | import android.content.res.Resources 4 | 5 | object MetricUtils { 6 | fun dpToPx(dp: Int): Int { 7 | return (dp * Resources.getSystem().displayMetrics.density).toInt() 8 | } 9 | } -------------------------------------------------------------------------------- /app/src/main/java/nl/storegear/android/mlbarcodescanner/util/PermissionUtils.kt: -------------------------------------------------------------------------------- 1 | package nl.storegear.android.mlbarcodescanner.util 2 | 3 | import android.app.Activity 4 | import android.content.Context 5 | import android.content.pm.PackageManager 6 | import android.util.Log 7 | import androidx.core.app.ActivityCompat 8 | import androidx.core.content.ContextCompat 9 | 10 | object PermissionUtils { 11 | private const val TAG = "MLBarcodeScanner" 12 | private const val PERMISSION_REQUESTS = 1 13 | 14 | fun allRuntimePermissionsGranted(activity: Activity, requiredPermissions: Array): Boolean { 15 | for (permission in requiredPermissions) { 16 | permission.let { 17 | if (!isPermissionGranted(activity, it)) { 18 | return false 19 | } 20 | } 21 | } 22 | return true 23 | } 24 | 25 | fun getRuntimePermissions(activity: Activity, requiredPermissions: Array) { 26 | val permissionsToRequest = ArrayList() 27 | for (permission in requiredPermissions) { 28 | permission.let { 29 | if (!isPermissionGranted(activity, it)) { 30 | permissionsToRequest.add(permission) 31 | } 32 | } 33 | } 34 | 35 | if (permissionsToRequest.isNotEmpty()) { 36 | ActivityCompat.requestPermissions(activity, permissionsToRequest.toTypedArray(), PERMISSION_REQUESTS) 37 | } 38 | } 39 | 40 | private fun isPermissionGranted(context: Context, permission: String): Boolean { 41 | if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) { 42 | Log.i(TAG, "Permission granted: $permission") 43 | return true 44 | } 45 | Log.i(TAG, "Permission NOT granted: $permission") 46 | return false 47 | } 48 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_close.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_scan_area.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 13 | 14 | 22 | 23 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MLBarcodeScanner 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /app/src/test/java/nl/storegear/android/mlbarcodescanner/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package nl.storegear.android.mlbarcodescanner 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | plugins { 3 | id 'com.android.application' version '7.3.1' apply false 4 | id 'com.android.library' version '7.3.1' apply false 5 | id 'org.jetbrains.kotlin.android' version '1.7.20' apply false 6 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Kotlin code style for this project: "official" or "obsolete": 19 | kotlin.code.style=official 20 | # Enables namespacing of each library's R class so that its R class includes only the 21 | # resources declared in the library itself and none from the library's dependencies, 22 | # thereby reducing the size of the R class for that library 23 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 20 10:41:42 IRDT 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /lib/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'org.jetbrains.kotlin.android' 4 | id 'maven-publish' 5 | } 6 | 7 | android { 8 | namespace 'com.buildtoapp.mlbarcodescanner' 9 | compileSdk 32 10 | defaultConfig { 11 | minSdk 21 12 | targetSdk 32 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | consumerProguardFiles "consumer-rules.pro" 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | compileOptions { 24 | sourceCompatibility JavaVersion.VERSION_1_8 25 | targetCompatibility JavaVersion.VERSION_1_8 26 | } 27 | kotlinOptions { 28 | jvmTarget = '1.8' 29 | } 30 | } 31 | 32 | dependencies { 33 | debugImplementation 'com.google.mlkit:barcode-scanning:17.0.2' // With bundled tflite models 34 | releaseImplementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:17.0.0' // Unbundled tflite models 35 | implementation 'androidx.camera:camera-camera2:1.1.0' 36 | implementation 'androidx.camera:camera-lifecycle:1.1.0' 37 | api 'androidx.camera:camera-view:1.1.0' 38 | 39 | implementation 'androidx.core:core-ktx:1.7.0' 40 | implementation 'androidx.appcompat:appcompat:1.5.1' 41 | implementation 'com.google.android.material:material:1.7.0' 42 | testImplementation 'junit:junit:4.13.2' 43 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 44 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 45 | } 46 | 47 | afterEvaluate { 48 | publishing { 49 | publications { 50 | release(MavenPublication) { 51 | from components.release 52 | groupId = 'com.github.javaherisaber' 53 | artifactId = 'MLBarcodeScanner' 54 | version = '1.0.0' 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /lib/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/javaherisaber/MLBarcodeScanner/32a2e8a274575c60c8f10fa8464dfefced91e2a3/lib/consumer-rules.pro -------------------------------------------------------------------------------- /lib/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /lib/src/androidTest/java/com/buildtoapp/mlbarcodescanner/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.buildtoapp.mlbarcodescanner.test", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/BarcodeGraphic.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import android.graphics.* 4 | import com.google.mlkit.vision.barcode.common.Barcode 5 | import kotlin.math.max 6 | import kotlin.math.min 7 | 8 | /** Graphic instance for rendering Barcode position and content information in an overlay view. */ 9 | internal class BarcodeGraphic( 10 | private val barcode: Barcode?, 11 | private val overlay: GraphicOverlay, 12 | private val drawOverlay: Boolean, 13 | private val drawBanner: Boolean, 14 | private val focusBoxSize: Int, 15 | private val result: BarcodeGraphicFocusResult, 16 | ) : GraphicOverlay.Graphic(overlay) { 17 | private val rectPaint: Paint = Paint() 18 | private val barcodePaint: Paint 19 | private val labelPaint: Paint 20 | 21 | init { 22 | rectPaint.style = Paint.Style.STROKE 23 | rectPaint.strokeWidth = STROKE_WIDTH 24 | barcodePaint = Paint() 25 | barcodePaint.color = Color.BLACK 26 | barcodePaint.textSize = TEXT_SIZE 27 | labelPaint = Paint() 28 | labelPaint.color = Color.WHITE 29 | labelPaint.style = Paint.Style.FILL 30 | } 31 | 32 | /** 33 | * Draws the barcode block annotations for position, size, and raw value on the supplied canvas. 34 | */ 35 | override fun draw(canvas: Canvas) { 36 | checkNotNull(barcode) { "Attempting to draw a null barcode." } 37 | // Draws the bounding box around the BarcodeBlock. 38 | val rect = RectF(barcode.boundingBox) 39 | // If the image is flipped, the left will be translated to right, and the right to left. 40 | val x0 = translateX(rect.left) 41 | val x1 = translateX(rect.right) 42 | rect.left = min(x0, x1) 43 | rect.right = max(x0, x1) 44 | rect.top = translateY(rect.top) 45 | rect.bottom = translateY(rect.bottom) 46 | val focusArea = calculateFocusArea() 47 | val isInFocusArea = targetIsInFocusArea(focusArea, rect) 48 | if (isInFocusArea) { 49 | rectPaint.color = Color.WHITE 50 | } else { 51 | rectPaint.color = Color.RED 52 | } 53 | if (isInFocusArea && drawBanner) { 54 | // Draws other object info. 55 | val lineHeight = TEXT_SIZE + 2 * STROKE_WIDTH 56 | val textWidth = barcodePaint.measureText(barcode.displayValue) 57 | canvas.drawRect( 58 | rect.left - STROKE_WIDTH, 59 | rect.top - lineHeight, 60 | rect.left + textWidth + 2 * STROKE_WIDTH, 61 | rect.top, 62 | labelPaint 63 | ) 64 | // Renders the barcode at the bottom of the box. 65 | canvas.drawText(barcode.displayValue!!, rect.left, rect.top - STROKE_WIDTH, barcodePaint) 66 | } 67 | if (drawOverlay) { 68 | canvas.drawRect(rect, rectPaint) 69 | } 70 | result.onGraphicDrawnInFocusArea(isInFocusArea) 71 | } 72 | 73 | private fun calculateFocusArea(): RectF { 74 | val height = overlay.measuredHeight.toFloat() 75 | val width = overlay.measuredWidth.toFloat() 76 | val left = (width / 2) - (focusBoxSize / 2) 77 | val right = (width / 2) + (focusBoxSize / 2) 78 | val top = (height / 2) - (focusBoxSize / 2) 79 | val bottom = (height / 2) + (focusBoxSize / 2) 80 | return RectF(left, top, right, bottom) 81 | } 82 | 83 | private fun targetIsInFocusArea(focus: RectF, target: RectF): Boolean { 84 | return (focus.left < target.left) && (target.right < focus.right) 85 | && (focus.top < target.top) && (focus.bottom > target.bottom) 86 | } 87 | 88 | companion object { 89 | private const val TEXT_SIZE = 54.0f 90 | private const val STROKE_WIDTH = 4.0f 91 | } 92 | } 93 | 94 | fun interface BarcodeGraphicFocusResult { 95 | fun onGraphicDrawnInFocusArea(isInFocus: Boolean) 96 | } 97 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/BarcodeScannerProcessor.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import com.google.android.gms.tasks.Task 4 | import com.google.mlkit.vision.barcode.BarcodeScanner 5 | import com.google.mlkit.vision.barcode.BarcodeScannerOptions 6 | import com.google.mlkit.vision.barcode.BarcodeScanning 7 | import com.google.mlkit.vision.barcode.common.Barcode 8 | import com.google.mlkit.vision.common.InputImage 9 | 10 | /** Barcode Detector Demo. */ 11 | internal class BarcodeScannerProcessor( 12 | private val callback: MLBarcodeCallback, 13 | private val drawOverlay: Boolean, 14 | private val drawBanner: Boolean, 15 | private val focusBoxSize: Int, 16 | supportedBarcodeFormats: List = listOf(Barcode.FORMAT_ALL_FORMATS) 17 | ) : VisionProcessorBase>() { 18 | 19 | // Note that if you know which format of barcode your app is dealing with, detection will be 20 | // faster to specify the supported barcode formats one by one 21 | private val barcodeScanner: BarcodeScanner = BarcodeScanning.getClient( 22 | if (supportedBarcodeFormats.size == 1) { 23 | BarcodeScannerOptions.Builder().setBarcodeFormats(supportedBarcodeFormats.first()).build() 24 | } else { 25 | val moreFormats = supportedBarcodeFormats.subList(1, supportedBarcodeFormats.size).toIntArray() 26 | BarcodeScannerOptions.Builder().setBarcodeFormats(supportedBarcodeFormats.first(), *moreFormats).build() 27 | } 28 | ) 29 | 30 | override fun stop() { 31 | super.stop() 32 | barcodeScanner.close() 33 | } 34 | 35 | override fun detectInImage(image: InputImage): Task> { 36 | return barcodeScanner.process(image) 37 | } 38 | 39 | override fun onSuccess(results: List, graphicOverlay: GraphicOverlay) { 40 | for (barcode in results) { 41 | val graphic = BarcodeGraphic(barcode, graphicOverlay, drawOverlay, drawBanner, focusBoxSize) { isInFocus -> 42 | val displayValue = barcode.displayValue 43 | val rawValue = barcode.rawValue 44 | if (isInFocus && displayValue != null && rawValue != null) { 45 | callback.onNewBarcodeScanned(displayValue, rawValue) 46 | } 47 | } 48 | graphicOverlay.add(graphic) 49 | } 50 | } 51 | 52 | override fun onFailure(e: Exception) { 53 | // do nothing 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/BitmapUtils.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapFactory; 5 | import android.graphics.ImageFormat; 6 | import android.graphics.Matrix; 7 | import android.graphics.Rect; 8 | import android.graphics.YuvImage; 9 | import android.media.Image; 10 | import android.media.Image.Plane; 11 | import android.util.Log; 12 | 13 | import androidx.annotation.Nullable; 14 | import androidx.camera.core.ExperimentalGetImage; 15 | import androidx.camera.core.ImageProxy; 16 | 17 | import java.io.ByteArrayOutputStream; 18 | import java.nio.ByteBuffer; 19 | 20 | /** Utils functions for bitmap conversions. */ 21 | class BitmapUtils { 22 | /** Converts NV21 format byte buffer to bitmap. */ 23 | @Nullable 24 | public static Bitmap getBitmap(ByteBuffer data, FrameMetadata metadata) { 25 | data.rewind(); 26 | byte[] imageInBuffer = new byte[data.limit()]; 27 | data.get(imageInBuffer, 0, imageInBuffer.length); 28 | try { 29 | YuvImage image = 30 | new YuvImage( 31 | imageInBuffer, ImageFormat.NV21, metadata.getWidth(), metadata.getHeight(), null); 32 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); 33 | image.compressToJpeg(new Rect(0, 0, metadata.getWidth(), metadata.getHeight()), 80, stream); 34 | 35 | Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); 36 | 37 | stream.close(); 38 | return rotateBitmap(bmp, metadata.getRotation()); 39 | } catch (Exception e) { 40 | Log.e("VisionProcessorBase", "Error: " + e.getMessage()); 41 | } 42 | return null; 43 | } 44 | 45 | /** Converts a YUV_420_888 image from CameraX API to a bitmap. */ 46 | @Nullable 47 | @ExperimentalGetImage 48 | public static Bitmap getBitmap(ImageProxy image) { 49 | FrameMetadata frameMetadata = 50 | new FrameMetadata.Builder() 51 | .setWidth(image.getWidth()) 52 | .setHeight(image.getHeight()) 53 | .setRotation(image.getImageInfo().getRotationDegrees()) 54 | .build(); 55 | 56 | ByteBuffer nv21Buffer = yuv420ThreePlanesToNV21(image.getImage().getPlanes(), image.getWidth(), image.getHeight()); 57 | return getBitmap(nv21Buffer, frameMetadata); 58 | } 59 | 60 | /** Rotates a bitmap if it is converted from a bytebuffer. */ 61 | private static Bitmap rotateBitmap(Bitmap bitmap, int rotationDegrees) { 62 | Matrix matrix = new Matrix(); 63 | 64 | // Rotate the image back to straight. 65 | matrix.postRotate(rotationDegrees); 66 | 67 | // Mirror the image along the X or Y axis. 68 | matrix.postScale(1.0f, 1.0f); 69 | Bitmap rotatedBitmap = 70 | Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 71 | 72 | // Recycle the old bitmap if it has changed. 73 | if (rotatedBitmap != bitmap) { 74 | bitmap.recycle(); 75 | } 76 | return rotatedBitmap; 77 | } 78 | 79 | /** 80 | * Converts YUV_420_888 to NV21 bytebuffer. 81 | * 82 | *

The NV21 format consists of a single byte array containing the Y, U and V values. For an 83 | * image of size S, the first S positions of the array contain all the Y values. The remaining 84 | * positions contain interleaved V and U values. U and V are subsampled by a factor of 2 in both 85 | * dimensions, so there are S/4 U values and S/4 V values. In summary, the NV21 array will contain 86 | * S Y values followed by S/4 VU values: YYYYYYYYYYYYYY(...)YVUVUVUVU(...)VU 87 | * 88 | *

YUV_420_888 is a generic format that can describe any YUV image where U and V are subsampled 89 | * by a factor of 2 in both dimensions. {@link Image#getPlanes} returns an array with the Y, U and 90 | * V planes. The Y plane is guaranteed not to be interleaved, so we can just copy its values into 91 | * the first part of the NV21 array. The U and V planes may already have the representation in the 92 | * NV21 format. This happens if the planes share the same buffer, the V buffer is one position 93 | * before the U buffer and the planes have a pixelStride of 2. If this is case, we can just copy 94 | * them to the NV21 array. 95 | */ 96 | private static ByteBuffer yuv420ThreePlanesToNV21( 97 | Plane[] yuv420888planes, int width, int height) { 98 | int imageSize = width * height; 99 | byte[] out = new byte[imageSize + 2 * (imageSize / 4)]; 100 | 101 | if (areUVPlanesNV21(yuv420888planes, width, height)) { 102 | // Copy the Y values. 103 | yuv420888planes[0].getBuffer().get(out, 0, imageSize); 104 | 105 | ByteBuffer uBuffer = yuv420888planes[1].getBuffer(); 106 | ByteBuffer vBuffer = yuv420888planes[2].getBuffer(); 107 | // Get the first V value from the V buffer, since the U buffer does not contain it. 108 | vBuffer.get(out, imageSize, 1); 109 | // Copy the first U value and the remaining VU values from the U buffer. 110 | uBuffer.get(out, imageSize + 1, 2 * imageSize / 4 - 1); 111 | } else { 112 | // Fallback to copying the UV values one by one, which is slower but also works. 113 | // Unpack Y. 114 | unpackPlane(yuv420888planes[0], width, height, out, 0, 1); 115 | // Unpack U. 116 | unpackPlane(yuv420888planes[1], width, height, out, imageSize + 1, 2); 117 | // Unpack V. 118 | unpackPlane(yuv420888planes[2], width, height, out, imageSize, 2); 119 | } 120 | 121 | return ByteBuffer.wrap(out); 122 | } 123 | 124 | /** Checks if the UV plane buffers of a YUV_420_888 image are in the NV21 format. */ 125 | private static boolean areUVPlanesNV21(Plane[] planes, int width, int height) { 126 | int imageSize = width * height; 127 | 128 | ByteBuffer uBuffer = planes[1].getBuffer(); 129 | ByteBuffer vBuffer = planes[2].getBuffer(); 130 | 131 | // Backup buffer properties. 132 | int vBufferPosition = vBuffer.position(); 133 | int uBufferLimit = uBuffer.limit(); 134 | 135 | // Advance the V buffer by 1 byte, since the U buffer will not contain the first V value. 136 | vBuffer.position(vBufferPosition + 1); 137 | // Chop off the last byte of the U buffer, since the V buffer will not contain the last U value. 138 | uBuffer.limit(uBufferLimit - 1); 139 | 140 | // Check that the buffers are equal and have the expected number of elements. 141 | boolean areNV21 = 142 | (vBuffer.remaining() == (2 * imageSize / 4 - 2)) && (vBuffer.compareTo(uBuffer) == 0); 143 | 144 | // Restore buffers to their initial state. 145 | vBuffer.position(vBufferPosition); 146 | uBuffer.limit(uBufferLimit); 147 | 148 | return areNV21; 149 | } 150 | 151 | /** 152 | * Unpack an image plane into a byte array. 153 | * 154 | *

The input plane data will be copied in 'out', starting at 'offset' and every pixel will be 155 | * spaced by 'pixelStride'. Note that there is no row padding on the output. 156 | */ 157 | private static void unpackPlane( 158 | Plane plane, int width, int height, byte[] out, int offset, int pixelStride) { 159 | ByteBuffer buffer = plane.getBuffer(); 160 | buffer.rewind(); 161 | 162 | // Compute the size of the current plane. 163 | // We assume that it has the aspect ratio as the original image. 164 | int numRow = (buffer.limit() + plane.getRowStride() - 1) / plane.getRowStride(); 165 | if (numRow == 0) { 166 | return; 167 | } 168 | int scaleFactor = height / numRow; 169 | int numCol = width / scaleFactor; 170 | 171 | // Extract the data in the output buffer. 172 | int outputPos = offset; 173 | int rowStart = 0; 174 | for (int row = 0; row < numRow; row++) { 175 | int inputPos = rowStart; 176 | for (int col = 0; col < numCol; col++) { 177 | out[outputPos] = buffer.get(inputPos); 178 | outputPos += pixelStride; 179 | inputPos += plane.getPixelStride(); 180 | } 181 | rowStart += plane.getRowStride(); 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/CameraImageGraphic.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | 6 | /** Draw camera image to background. */ 7 | class CameraImageGraphic extends GraphicOverlay.Graphic { 8 | 9 | private final Bitmap bitmap; 10 | 11 | public CameraImageGraphic(GraphicOverlay overlay, Bitmap bitmap) { 12 | super(overlay); 13 | this.bitmap = bitmap; 14 | } 15 | 16 | @Override 17 | public void draw(Canvas canvas) { 18 | canvas.drawBitmap(bitmap, getTransformationMatrix(), null); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/CameraXViewModel.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | import androidx.annotation.NonNull; 7 | import androidx.camera.lifecycle.ProcessCameraProvider; 8 | import androidx.core.content.ContextCompat; 9 | import androidx.lifecycle.AndroidViewModel; 10 | import androidx.lifecycle.LiveData; 11 | import androidx.lifecycle.MutableLiveData; 12 | 13 | import com.google.common.util.concurrent.ListenableFuture; 14 | 15 | import java.util.concurrent.ExecutionException; 16 | 17 | /** View model for interacting with CameraX. */ 18 | public final class CameraXViewModel extends AndroidViewModel { 19 | 20 | private static final String TAG = "CameraXViewModel"; 21 | private MutableLiveData cameraProviderLiveData; 22 | 23 | /** 24 | * Create an instance which interacts with the camera service via the given application context. 25 | */ 26 | public CameraXViewModel(@NonNull Application application) { 27 | super(application); 28 | } 29 | 30 | public LiveData getProcessCameraProvider() { 31 | if (cameraProviderLiveData == null) { 32 | cameraProviderLiveData = new MutableLiveData<>(); 33 | 34 | ListenableFuture cameraProviderFuture = 35 | ProcessCameraProvider.getInstance(getApplication()); 36 | cameraProviderFuture.addListener( 37 | () -> { 38 | try { 39 | cameraProviderLiveData.setValue(cameraProviderFuture.get()); 40 | } catch (ExecutionException | InterruptedException e) { 41 | Log.e(TAG, "Unhandled exception", e); 42 | } 43 | }, 44 | ContextCompat.getMainExecutor(getApplication())); 45 | } 46 | 47 | return cameraProviderLiveData; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/FrameMetadata.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | /** Describing a frame info. */ 4 | class FrameMetadata { 5 | 6 | private final int width; 7 | private final int height; 8 | private final int rotation; 9 | 10 | public int getWidth() { 11 | return width; 12 | } 13 | 14 | public int getHeight() { 15 | return height; 16 | } 17 | 18 | public int getRotation() { 19 | return rotation; 20 | } 21 | 22 | private FrameMetadata(int width, int height, int rotation) { 23 | this.width = width; 24 | this.height = height; 25 | this.rotation = rotation; 26 | } 27 | 28 | /** Builder of {@link FrameMetadata}. */ 29 | public static class Builder { 30 | 31 | private int width; 32 | private int height; 33 | private int rotation; 34 | 35 | public Builder setWidth(int width) { 36 | this.width = width; 37 | return this; 38 | } 39 | 40 | public Builder setHeight(int height) { 41 | this.height = height; 42 | return this; 43 | } 44 | 45 | public Builder setRotation(int rotation) { 46 | this.rotation = rotation; 47 | return this; 48 | } 49 | 50 | public FrameMetadata build() { 51 | return new FrameMetadata(width, height, rotation); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/GraphicOverlay.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Matrix; 6 | import android.graphics.Paint; 7 | import android.util.AttributeSet; 8 | import android.view.View; 9 | 10 | import com.google.android.gms.common.internal.Preconditions; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * A view which renders a series of custom graphics to be overlayed on top of an associated preview 17 | * (i.e., the camera preview). The creator can add graphics objects, update the objects, and remove 18 | * them, triggering the appropriate drawing and invalidation within the view. 19 | * 20 | *

Supports scaling and mirroring of the graphics relative the camera's preview properties. The 21 | * idea is that detection items are expressed in terms of an image size, but need to be scaled up 22 | * to the full view size, and also mirrored in the case of the front-facing camera. 23 | * 24 | *

Associated {@link Graphic} items should use the following methods to convert to view 25 | * coordinates for the graphics that are drawn: 26 | * 27 | *

    28 | *
  1. {@link Graphic#scale(float)} adjusts the size of the supplied value from the image scale 29 | * to the view scale. 30 | *
  2. {@link Graphic#translateX(float)} and {@link Graphic#translateY(float)} adjust the 31 | * coordinate from the image's coordinate system to the view coordinate system. 32 | *
33 | */ 34 | public class GraphicOverlay extends View { 35 | private final Object lock = new Object(); 36 | private final List graphics = new ArrayList<>(); 37 | // Matrix for transforming from image coordinates to overlay view coordinates. 38 | private final Matrix transformationMatrix = new Matrix(); 39 | 40 | private int imageWidth; 41 | private int imageHeight; 42 | // The factor of overlay View size to image size. Anything in the image coordinates need to be 43 | // scaled by this amount to fit with the area of overlay View. 44 | private float scaleFactor = 1.0f; 45 | // The number of horizontal pixels needed to be cropped on each side to fit the image with the 46 | // area of overlay View after scaling. 47 | private float postScaleWidthOffset; 48 | // The number of vertical pixels needed to be cropped on each side to fit the image with the 49 | // area of overlay View after scaling. 50 | private float postScaleHeightOffset; 51 | private boolean isImageFlipped; 52 | private boolean needUpdateTransformation = true; 53 | 54 | /** 55 | * Base class for a custom graphics object to be rendered within the graphic overlay. Subclass 56 | * this and implement the {@link Graphic#draw(Canvas)} method to define the graphics element. Add 57 | * instances to the overlay using {@link GraphicOverlay#add(Graphic)}. 58 | */ 59 | public abstract static class Graphic { 60 | private GraphicOverlay overlay; 61 | 62 | public Graphic(GraphicOverlay overlay) { 63 | this.overlay = overlay; 64 | } 65 | 66 | /** 67 | * Draw the graphic on the supplied canvas. Drawing should use the following methods to convert 68 | * to view coordinates for the graphics that are drawn: 69 | * 70 | *
    71 | *
  1. {@link Graphic#scale(float)} adjusts the size of the supplied value from the image 72 | * scale to the view scale. 73 | *
  2. {@link Graphic#translateX(float)} and {@link Graphic#translateY(float)} adjust the 74 | * coordinate from the image's coordinate system to the view coordinate system. 75 | *
76 | * 77 | * @param canvas drawing canvas 78 | */ 79 | public abstract void draw(Canvas canvas); 80 | 81 | protected void drawRect( 82 | Canvas canvas, float left, float top, float right, float bottom, Paint paint) { 83 | canvas.drawRect(left, top, right, bottom, paint); 84 | } 85 | 86 | protected void drawText(Canvas canvas, String text, float x, float y, Paint paint) { 87 | canvas.drawText(text, x, y, paint); 88 | } 89 | 90 | /** Adjusts the supplied value from the image scale to the view scale. */ 91 | public float scale(float imagePixel) { 92 | return imagePixel * overlay.scaleFactor; 93 | } 94 | 95 | /** Returns the application context of the app. */ 96 | public Context getApplicationContext() { 97 | return overlay.getContext().getApplicationContext(); 98 | } 99 | 100 | public boolean isImageFlipped() { 101 | return overlay.isImageFlipped; 102 | } 103 | 104 | /** 105 | * Adjusts the x coordinate from the image's coordinate system to the view coordinate system. 106 | */ 107 | public float translateX(float x) { 108 | if (overlay.isImageFlipped) { 109 | return overlay.getWidth() - (scale(x) - overlay.postScaleWidthOffset); 110 | } else { 111 | return scale(x) - overlay.postScaleWidthOffset; 112 | } 113 | } 114 | 115 | /** 116 | * Adjusts the y coordinate from the image's coordinate system to the view coordinate system. 117 | */ 118 | public float translateY(float y) { 119 | return scale(y) - overlay.postScaleHeightOffset; 120 | } 121 | 122 | /** 123 | * Returns a {@link Matrix} for transforming from image coordinates to overlay view coordinates. 124 | */ 125 | public Matrix getTransformationMatrix() { 126 | return overlay.transformationMatrix; 127 | } 128 | 129 | public void postInvalidate() { 130 | overlay.postInvalidate(); 131 | } 132 | } 133 | 134 | public GraphicOverlay(Context context, AttributeSet attrs) { 135 | super(context, attrs); 136 | addOnLayoutChangeListener( 137 | (view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> 138 | needUpdateTransformation = true); 139 | } 140 | 141 | /** Removes all graphics from the overlay. */ 142 | public void clear() { 143 | synchronized (lock) { 144 | graphics.clear(); 145 | } 146 | postInvalidate(); 147 | } 148 | 149 | /** Adds a graphic to the overlay. */ 150 | public void add(Graphic graphic) { 151 | synchronized (lock) { 152 | graphics.add(graphic); 153 | } 154 | } 155 | 156 | /** Removes a graphic from the overlay. */ 157 | public void remove(Graphic graphic) { 158 | synchronized (lock) { 159 | graphics.remove(graphic); 160 | } 161 | postInvalidate(); 162 | } 163 | 164 | /** 165 | * Sets the source information of the image being processed by detectors, including size and 166 | * whether it is flipped, which informs how to transform image coordinates later. 167 | * 168 | * @param imageWidth the width of the image sent to ML Kit detectors 169 | * @param imageHeight the height of the image sent to ML Kit detectors 170 | * @param isFlipped whether the image is flipped. Should set it to true when the image is from the 171 | * front camera. 172 | */ 173 | public void setImageSourceInfo(int imageWidth, int imageHeight, boolean isFlipped) { 174 | Preconditions.checkState(imageWidth > 0, "image width must be positive"); 175 | Preconditions.checkState(imageHeight > 0, "image height must be positive"); 176 | synchronized (lock) { 177 | this.imageWidth = imageWidth; 178 | this.imageHeight = imageHeight; 179 | this.isImageFlipped = isFlipped; 180 | needUpdateTransformation = true; 181 | } 182 | postInvalidate(); 183 | } 184 | 185 | public int getImageWidth() { 186 | return imageWidth; 187 | } 188 | 189 | public int getImageHeight() { 190 | return imageHeight; 191 | } 192 | 193 | private void updateTransformationIfNeeded() { 194 | if (!needUpdateTransformation || imageWidth <= 0 || imageHeight <= 0) { 195 | return; 196 | } 197 | float viewAspectRatio = (float) getWidth() / getHeight(); 198 | float imageAspectRatio = (float) imageWidth / imageHeight; 199 | postScaleWidthOffset = 0; 200 | postScaleHeightOffset = 0; 201 | if (viewAspectRatio > imageAspectRatio) { 202 | // The image needs to be vertically cropped to be displayed in this view. 203 | scaleFactor = (float) getWidth() / imageWidth; 204 | postScaleHeightOffset = ((float) getWidth() / imageAspectRatio - getHeight()) / 2; 205 | } else { 206 | // The image needs to be horizontally cropped to be displayed in this view. 207 | scaleFactor = (float) getHeight() / imageHeight; 208 | postScaleWidthOffset = ((float) getHeight() * imageAspectRatio - getWidth()) / 2; 209 | } 210 | 211 | transformationMatrix.reset(); 212 | transformationMatrix.setScale(scaleFactor, scaleFactor); 213 | transformationMatrix.postTranslate(-postScaleWidthOffset, -postScaleHeightOffset); 214 | 215 | if (isImageFlipped) { 216 | transformationMatrix.postScale(-1f, 1f, getWidth() / 2f, getHeight() / 2f); 217 | } 218 | 219 | needUpdateTransformation = false; 220 | } 221 | 222 | /** Draws the overlay with its associated graphic objects. */ 223 | @Override 224 | protected void onDraw(Canvas canvas) { 225 | super.onDraw(canvas); 226 | 227 | synchronized (lock) { 228 | updateTransformationIfNeeded(); 229 | 230 | for (Graphic graphic : graphics) { 231 | graphic.draw(canvas); 232 | } 233 | } 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/MLBarcodeScanner.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import android.annotation.SuppressLint 4 | import android.content.Context 5 | import android.util.Log 6 | import android.util.Size 7 | import androidx.camera.core.CameraSelector 8 | import androidx.camera.core.ImageAnalysis 9 | import androidx.camera.core.ImageProxy 10 | import androidx.camera.core.Preview 11 | import androidx.camera.lifecycle.ProcessCameraProvider 12 | import androidx.camera.view.PreviewView 13 | import androidx.core.content.ContextCompat 14 | import androidx.lifecycle.DefaultLifecycleObserver 15 | import androidx.lifecycle.LifecycleOwner 16 | import androidx.lifecycle.ViewModelProvider 17 | import androidx.lifecycle.ViewModelStoreOwner 18 | import com.google.mlkit.common.MlKitException 19 | import com.google.mlkit.vision.barcode.common.Barcode 20 | 21 | /** 22 | * Delegate class to wrap all functionalities of barcode scanning and handling it's resources in a lifecycle aware manner 23 | * 24 | * @param callback listen to new scanned barcode 25 | * @param focusBoxSize width/height of the focus box in pixels (box must be square) 26 | * @param graphicOverlay overlay graphic to be drawn on screen to recognize barcode 27 | * @param previewView camera preview object in your view 28 | * @param lifecycleOwner lifecycle owner of your view (viewLifecycleOwner in fragment and this in activity) 29 | * @param context ui context 30 | * @param drawOverlay if set to true, will display a rectangle around detected barcode (default to true) 31 | * @param drawBanner if set to true, will display detected barcode value on top of it's rectangle (default to false) 32 | * @param targetResolution resolution of the camera view (default to 768 * 1024) 33 | * @param supportedBarcodeFormats list of all supported barcode formats (default to all) 34 | */ 35 | class MLBarcodeScanner( 36 | private val callback: MLBarcodeCallback, 37 | private val context: Context, 38 | private val lifecycleOwner: LifecycleOwner, 39 | private val focusBoxSize: Int, 40 | private val graphicOverlay: GraphicOverlay, 41 | private val previewView: PreviewView, 42 | private val drawOverlay: Boolean = true, 43 | private val drawBanner: Boolean = false, 44 | private val targetResolution: Size = Size(768, 1024), 45 | private val supportedBarcodeFormats: List = listOf(Barcode.FORMAT_ALL_FORMATS) 46 | ) : DefaultLifecycleObserver { 47 | 48 | init { 49 | lifecycleOwner.lifecycle.addObserver(this) 50 | } 51 | 52 | private var lensFacing = CameraSelector.LENS_FACING_BACK 53 | private val cameraXViewModel: CameraXViewModel by lazy { 54 | ViewModelProvider(lifecycleOwner as ViewModelStoreOwner)[CameraXViewModel::class.java] 55 | } 56 | private lateinit var cameraSelector: CameraSelector 57 | private var cameraProvider: ProcessCameraProvider? = null 58 | private var previewUseCase: Preview? = null 59 | private var analysisUseCase: ImageAnalysis? = null 60 | private var needUpdateGraphicOverlayImageSourceInfo = false 61 | private var imageProcessor: VisionImageProcessor? = null 62 | 63 | init { 64 | initialize() 65 | } 66 | 67 | /** 68 | * initialize instance members 69 | */ 70 | fun initialize() { 71 | cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build() 72 | cameraXViewModel.processCameraProvider.observe(lifecycleOwner) { provider: ProcessCameraProvider? -> 73 | cameraProvider = provider 74 | bindAllCameraUseCases() 75 | } 76 | } 77 | 78 | /** 79 | * Stop processing images in camera 80 | */ 81 | fun stop() { 82 | imageProcessor?.stop() 83 | } 84 | 85 | private fun bindAllCameraUseCases() { 86 | // As required by CameraX API, unbinds all use cases before trying to re-bind any of them. 87 | cameraProvider?.unbindAll() 88 | bindPreviewUseCase() 89 | bindAnalysisUseCase() 90 | } 91 | 92 | private fun bindPreviewUseCase() { 93 | if (cameraProvider == null) { 94 | return 95 | } 96 | cameraProvider?.unbind(previewUseCase) 97 | val builder = Preview.Builder() 98 | previewUseCase = builder.build() 99 | previewUseCase?.setSurfaceProvider(previewView.surfaceProvider) 100 | cameraProvider?.bindToLifecycle(lifecycleOwner, cameraSelector, previewUseCase) 101 | } 102 | 103 | @SuppressLint("UnsafeOptInUsageError") 104 | private fun bindAnalysisUseCase() { 105 | if (cameraProvider == null) { 106 | return 107 | } 108 | cameraProvider?.unbind(analysisUseCase) 109 | imageProcessor?.stop() 110 | imageProcessor = BarcodeScannerProcessor( 111 | callback, drawOverlay, drawBanner, focusBoxSize, supportedBarcodeFormats 112 | ) 113 | 114 | val builder = ImageAnalysis.Builder() 115 | builder.setTargetResolution(targetResolution) 116 | analysisUseCase = builder.build() 117 | 118 | needUpdateGraphicOverlayImageSourceInfo = true 119 | 120 | analysisUseCase?.setAnalyzer( 121 | // imageProcessor.processImageProxy will use another thread to run the detection underneath, 122 | // thus we can just runs the analyzer itself on main thread. 123 | ContextCompat.getMainExecutor(context) 124 | ) { imageProxy: ImageProxy -> 125 | if (needUpdateGraphicOverlayImageSourceInfo) { 126 | val rotationDegrees = imageProxy.imageInfo.rotationDegrees 127 | if (rotationDegrees == 0 || rotationDegrees == 180) { 128 | graphicOverlay.setImageSourceInfo(imageProxy.width, imageProxy.height, false) 129 | } else { 130 | graphicOverlay.setImageSourceInfo(imageProxy.height, imageProxy.width, false) 131 | } 132 | needUpdateGraphicOverlayImageSourceInfo = false 133 | } 134 | 135 | try { 136 | imageProcessor?.processImageProxy(imageProxy, graphicOverlay) 137 | } catch (e: MlKitException) { 138 | Log.e("TAG", "Failed to process image. Error: " + e.localizedMessage) 139 | } 140 | } 141 | cameraProvider?.bindToLifecycle(lifecycleOwner, cameraSelector, analysisUseCase) 142 | } 143 | 144 | override fun onPause(owner: LifecycleOwner) { 145 | imageProcessor?.stop() 146 | super.onPause(owner) 147 | } 148 | 149 | override fun onResume(owner: LifecycleOwner) { 150 | bindAllCameraUseCases() 151 | super.onResume(owner) 152 | } 153 | 154 | override fun onDestroy(owner: LifecycleOwner) { 155 | imageProcessor?.stop() 156 | lifecycleOwner.lifecycle.removeObserver(this) 157 | super.onDestroy(owner) 158 | } 159 | } 160 | 161 | /** 162 | * Listen to new scanned barcodes 163 | */ 164 | fun interface MLBarcodeCallback { 165 | /** 166 | * @param displayValue Returns barcode value in a user-friendly format. 167 | * This method may omit some of the information encoded in the barcode. For example, if getRawValue() returns 'MEBKM:TITLE:Google;URL://www.google.com;;', the display value might be '//www.google.com'. 168 | * This value may be multiline, for example, when line breaks are encoded into the original TEXT barcode value. May include the supplement value. 169 | * 170 | * @param rawValue Returns barcode value as it was encoded in the barcode. Structured values are not parsed, for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'. 171 | */ 172 | fun onNewBarcodeScanned(displayValue: String, rawValue: String) 173 | } 174 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/ScopedExecutor.java: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner; 2 | 3 | import androidx.annotation.NonNull; 4 | 5 | import java.util.concurrent.Executor; 6 | import java.util.concurrent.atomic.AtomicBoolean; 7 | 8 | /** 9 | * Wraps an existing executor to provide a {@link #shutdown} method that allows subsequent 10 | * cancellation of submitted runnables. 11 | */ 12 | class ScopedExecutor implements Executor { 13 | 14 | private final Executor executor; 15 | private final AtomicBoolean shutdown = new AtomicBoolean(); 16 | 17 | public ScopedExecutor(@NonNull Executor executor) { 18 | this.executor = executor; 19 | } 20 | 21 | @Override 22 | public void execute(@NonNull Runnable command) { 23 | // Return early if this object has been shut down. 24 | if (shutdown.get()) { 25 | return; 26 | } 27 | executor.execute( 28 | () -> { 29 | // Check again in case it has been shut down in the mean time. 30 | if (shutdown.get()) { 31 | return; 32 | } 33 | command.run(); 34 | }); 35 | } 36 | 37 | /** 38 | * After this method is called, no runnables that have been submitted or are subsequently 39 | * submitted will start to execute, turning this executor into a no-op. 40 | * 41 | *

Runnables that have already started to execute will continue. 42 | */ 43 | public void shutdown() { 44 | shutdown.set(true); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/VisionImageProcessor.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import androidx.camera.core.ImageProxy 4 | import com.google.mlkit.common.MlKitException 5 | 6 | /** 7 | * An interface to process the images with different vision detectors and custom image models. 8 | */ 9 | internal interface VisionImageProcessor { 10 | /** 11 | * Processes ImageProxy image data, e.g. used for CameraX live preview case. 12 | */ 13 | @Throws(MlKitException::class) 14 | fun processImageProxy(image: ImageProxy, graphicOverlay: GraphicOverlay) 15 | 16 | /** 17 | * Stops the underlying machine learning model and release resources. 18 | */ 19 | fun stop() 20 | } -------------------------------------------------------------------------------- /lib/src/main/java/com/buildtoapp/mlbarcodescanner/VisionProcessorBase.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import android.content.Context 4 | import android.graphics.Bitmap 5 | import androidx.annotation.GuardedBy 6 | import androidx.camera.core.ExperimentalGetImage 7 | import androidx.camera.core.ImageProxy 8 | import com.google.android.gms.tasks.Task 9 | import com.google.android.gms.tasks.TaskExecutors 10 | import com.google.android.gms.tasks.Tasks 11 | import com.google.android.odml.image.MediaMlImageBuilder 12 | import com.google.android.odml.image.MlImage 13 | import com.google.mlkit.common.MlKitException 14 | import com.google.mlkit.vision.common.InputImage 15 | import java.nio.ByteBuffer 16 | 17 | /** 18 | * Abstract base class for ML Kit frame processors. Subclasses need to implement {@link 19 | * #onSuccess(T, FrameMetadata, GraphicOverlay)} to define what they want to with the detection 20 | * results and {@link #detectInImage(VisionImage)} to specify the detector object. 21 | * 22 | * @param The type of the detected feature. 23 | */ 24 | internal abstract class VisionProcessorBase : VisionImageProcessor { 25 | 26 | private val executor = ScopedExecutor(TaskExecutors.MAIN_THREAD) 27 | 28 | // Whether this processor is already shut down 29 | private var isShutdown = false 30 | 31 | // To keep the latest images and its metadata. 32 | @GuardedBy("this") 33 | private var latestImage: ByteBuffer? = null 34 | @GuardedBy("this") 35 | private var latestImageMetaData: FrameMetadata? = null 36 | 37 | // To keep the images and metadata in process. 38 | @GuardedBy("this") 39 | private var processingImage: ByteBuffer? = null 40 | @GuardedBy("this") 41 | private var processingMetaData: FrameMetadata? = null 42 | 43 | @ExperimentalGetImage 44 | override fun processImageProxy(image: ImageProxy, graphicOverlay: GraphicOverlay) { 45 | if (isShutdown) { 46 | return 47 | } 48 | val bitmap: Bitmap? = BitmapUtils.getBitmap(image) 49 | val mediaImage = image.image ?: error("image should not be null here") 50 | 51 | if (isMlImageEnabled(graphicOverlay.context)) { 52 | val mlImage = 53 | MediaMlImageBuilder(mediaImage).setRotation(image.imageInfo.rotationDegrees).build() 54 | requestDetectInImage( 55 | mlImage, 56 | graphicOverlay, 57 | bitmap 58 | ) 59 | // When the image is from CameraX analysis use case, must call image.close() on received 60 | // images when finished using them. Otherwise, new images may not be received or the camera 61 | // may stall. 62 | // Currently MlImage doesn't support ImageProxy directly, so we still need to call 63 | // ImageProxy.close() here. 64 | .addOnCompleteListener { image.close() } 65 | 66 | return 67 | } 68 | 69 | requestDetectInImage( 70 | InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees), 71 | graphicOverlay, 72 | bitmap 73 | ) 74 | // When the image is from CameraX analysis use case, must call image.close() on received 75 | // images when finished using them. Otherwise, new images may not be received or the camera 76 | // may stall. 77 | .addOnCompleteListener { image.close() } 78 | } 79 | 80 | private fun requestDetectInImage( 81 | image: InputImage, 82 | graphicOverlay: GraphicOverlay, 83 | originalCameraImage: Bitmap? 84 | ): Task { 85 | return setUpListener(detectInImage(image), graphicOverlay, originalCameraImage) 86 | } 87 | 88 | private fun requestDetectInImage( 89 | image: MlImage, 90 | graphicOverlay: GraphicOverlay, 91 | originalCameraImage: Bitmap? 92 | ): Task { 93 | return setUpListener(detectInImage(image), graphicOverlay, originalCameraImage) 94 | } 95 | 96 | private fun setUpListener( 97 | task: Task, 98 | graphicOverlay: GraphicOverlay, 99 | originalCameraImage: Bitmap? 100 | ): Task { 101 | return task 102 | .addOnSuccessListener(executor) { results: T -> 103 | graphicOverlay.clear() 104 | if (originalCameraImage != null) { 105 | graphicOverlay.add(CameraImageGraphic(graphicOverlay, originalCameraImage)) 106 | } 107 | this@VisionProcessorBase.onSuccess(results, graphicOverlay) 108 | graphicOverlay.postInvalidate() 109 | } 110 | .addOnFailureListener(executor) { e: Exception -> 111 | graphicOverlay.clear() 112 | graphicOverlay.postInvalidate() 113 | this@VisionProcessorBase.onFailure(e) 114 | } 115 | } 116 | 117 | override fun stop() { 118 | executor.shutdown() 119 | isShutdown = true 120 | } 121 | 122 | protected abstract fun detectInImage(image: InputImage): Task 123 | 124 | protected open fun detectInImage(image: MlImage): Task { 125 | return Tasks.forException( 126 | MlKitException( 127 | "MlImage is currently not demonstrated for this feature", 128 | MlKitException.INVALID_ARGUMENT 129 | ) 130 | ) 131 | } 132 | 133 | protected abstract fun onSuccess(results: T, graphicOverlay: GraphicOverlay) 134 | 135 | protected abstract fun onFailure(e: Exception) 136 | 137 | protected open fun isMlImageEnabled(context: Context?): Boolean { 138 | return false 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /lib/src/test/java/com/buildtoapp/mlbarcodescanner/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.buildtoapp.mlbarcodescanner 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | } 14 | } 15 | rootProject.name = "MLBarcodeScanner" 16 | include ':app' 17 | include ':lib' 18 | --------------------------------------------------------------------------------