Blog Infos
Author
Published
Topics
Author
Published

Major benefits:
  • No permissions required 🎉
  • No dependencies on MLKIt anymore
Requirements

minSdkVersion value of ≥ 21

Implementation

Add dependency

dependencies {
  implementation 'com.google.android.gms:play-services-code-scanner:16.0.0'
}
<application ...>
  ...
  <meta-data
      android:name="com.google.mlkit.vision.DEPENDENCIES"
      android:value="barcode_ui"/>
  ...
</application>
Scan a code
  • Configure barcode scanner options — GmsBarcodeScannerOptions [Optional]
val options = GmsBarcodeScannerOptions.Builder()
    .setBarcodeFormats(
        Barcode.FORMAT_QR_CODE,
        Barcode.FORMAT_AZTEC)
    .build()
val scanner = GmsBarcodeScanning.getClient(this)


// Or with a configured options

val options = GmsBarcodeScannerOptions.Builder()
    .setBarcodeFormats(
        Barcode.FORMAT_QR_CODE,
        Barcode.FORMAT_AZTEC)
    .build()

val scanner = GmsBarcodeScanning.getClient(this, options)
private fun initiateScanner(
    gmsBarcodeScanner: GmsBarcodeScanner,
    onSuccess: (Barcode) -> Unit,
    onCancel: () -> Unit,
    onFailure: (Exception) -> Unit
) {
    gmsBarcodeScanner.startScan()
        .addOnSuccessListener { barcode ->
            // Task completed successfully
            val result = barcode.rawValue
            Log.d(TAG, "initiateScanner: $result")
            // check the value - URL, TEXT, etc.
            when (barcode.valueType) {
                Barcode.TYPE_URL -> {
                    Log.d(TAG, "initiateScanner: ${barcode.valueType}")
                }
                else -> {
                    Log.d(TAG, "initiateScanner: ${barcode.valueType}")
                }
            }
            // Display valu
            Log.d(TAG, "initiateScanner: Display value: ${barcode.displayValue}")
            // Formate - FORMAT_AZTEC, etc.
            Log.d(TAG, "initiateScanner: Format: ${barcode.format}")
            onSuccess(barcode)
        }
        .addOnCanceledListener {
            // Task canceled by the user
            onCancel()
        }
        .addOnFailureListener { e ->
            // Task failed with an exception
            onFailure(e)
        }
}

 

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Scanning a QR or Barcode is one of the most common use cases across…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu