The aim of this text is to elucidate how we will combine code scanning operate into our Android apps utilizing Google’s Code Scanner API.
Scan outcomes To be give again simply to software by way of Google Play companies
which all code scanning work.
minSdkVersion
worth of ≥ 21
Add dependencies
dependencies {
implementation 'com.google.android.gms:play-services-code-scanner:16.0.0'
}
Add metadata to routinely obtain scanner modules
...
android:title="com.google.mlkit.imaginative and prescient.DEPENDENCIES"
android:worth="barcode_ui"/>
...
we will use ModuleInstallClient API ARRIVE clear
verify the scanner module availability and request obtain
as a result ofGoogle Play companies
-
Configure barcode scanner choices —
GmsBarcodeScannerOptions
[Optional]
val choices = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC)
.construct()
-
Take an instance of
GmsBarcodeScanner
val scanner = GmsBarcodeScanning.getClient(this)
// Or with a configured choices
val choices = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_AZTEC)
.construct()
val scanner = GmsBarcodeScanning.getClient(this, choices)
-
Begin scanning
║▌║█║▌│║▌║▌█
non-public enjoyable initiateScanner(
gmsBarcodeScanner: GmsBarcodeScanner,
onSuccess: (Barcode) -> Unit,
onCancel: () -> Unit,
onFailure: (Exception) -> Unit
) {
gmsBarcodeScanner.startScan()
.addOnSuccessListener { barcode ->
// Activity accomplished efficiently
val end result = barcode.rawValue
Log.d(TAG, "initiateScanner: $end result")
// verify the worth - URL, TEXT, and so forth.
when (barcode.valueType) {
Barcode.TYPE_URL -> {
Log.d(TAG, "initiateScanner: ${barcode.valueType}")
}
else -> {
Log.d(TAG, "initiateScanner: ${barcode.valueType}")
}
}
// Show valu
Log.d(TAG, "initiateScanner: Show worth: ${barcode.displayValue}")
// Formate - FORMAT_AZTEC, and so forth.
Log.d(TAG, "initiateScanner: Format: ${barcode.format}")
onSuccess(barcode)
}
.addOnCanceledListener {
// Activity canceled by the consumer
onCancel()
}
.addOnFailureListener { e ->
// Activity failed with an exception
onFailure(e)
}
}