The simplest way to create a barcode scanner in Android is possibly with the Google Code Scanner API https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner
However, this doesn't seem to work for me. My code in MainActivity.kt file looks like this:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val options = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC)
.build()
val scanner = GmsBarcodeScanning.getClient(this, options)
scanner.startScan()
.addOnSuccessListener { barcode ->
val rawValue: String? = barcode.rawValue
}
.addOnCanceledListener {
// Task canceled
}
.addOnFailureListener { e ->
// Task failed with an exception
}
}
}
The settings.gradle
file has the following code:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
My build.gradle
file has the following dependency:
implementation 'com.google.android.gms:play-services-code-scanner:16.0.0'
I have added the following to my AndroidManifest.xml
file:
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode_ui"/>
However, I only get a black screen while running the app. The Google QR code scanner UI is showing up but the camera doesn't seem to work. The entire screen is black.
Update:
The app works on one device but not the other. What could be wrong here?
Thanks.