quickie Android QR Code Scanning Library

repository·main·Indexed 19 days ago

https://github.com/g00fy2/quickie

A modern Android QR code scanning library built with Kotlin, CameraX, and ML Kit. It provides an easy-to-use API via the Activity Result API for both simple one-tap scanning and customizable barcode detection. Available in bundled and unbundled flavors, it supports various content subtypes including Wifi, Url, and ContactInfo, and offers a ScannerConfig DSL for advanced configuration of barcode formats, overlays, and camera settings.

Tokens
1.7K
Snippets
4
Records
7
Agent score
17%

What's inside quickie

  1. Access QR code data with QRContent

    main

    When a scan is successful (QRSuccess), you receive a QRContent object. It provides:

    • rawBytes: The raw byte array of the content.
    • rawValue: The string representation of the content (returns null for non-UTF8 barcodes).

    Supported content subtypes include:

    • Plain, Wifi, Url, Sms, GeoPoint, Email, Phone, ContactInfo, CalendarEvent.
  2. Quick Start: Launch QR scanner in View-based Android apps

    main

    To use the QR scanner in an Activity or Fragment, register the ScanQRCode() ActivityResultContract during the init or onCreate() lifecycle. Use the returned ActivityResultLauncher to launch the scanner by calling .launch(null).

    val scanQrCodeLauncher = registerForActivityResult(ScanQRCode()) { result ->
        // handle QRResult
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        …
        binding.button.setOnClickListener { scanQrCodeLauncher.launch(null) }
    }
  3. Install quickie via Maven Central

    main

    Choose between two flavors of quickie depending on your app size and ML Kit requirements:

    • Bundled: The ML Kit model is included in your app. This makes the app independent of Google Services but increases the app size by approximately 2.5 MB per ABI. It uses the faster and more accurate V3 model.
    • Unbundled: The ML Kit model is downloaded via Play Services. This results in a smaller app size increase (approx. 550 KB) but requires Google Play Services on the device and currently uses the V1 model.

    It is recommended to use Android App Bundles or ABI splitting when using the bundled version to manage size increases.

    // bundled:  
    implementation("io.github.g00fy2.quickie:quickie-bundled:1.12.0")
    
    // unbundled:
    implementation("io.github.g00fy2.quickie:quickie-unbundled:1.12.0")
  4. Quick Start: Launch QR scanner in Jetpack Compose

    main

    In a Composable function, use rememberLauncherForActivityResult() to register the ScanQRCode() ActivityResultContract. Use the returned launcher to start the scan.

    @Composable
    fun GetQRCodeExample() {
        val scanQrCodeLauncher = rememberLauncherForActivityResult(ScanQRCode()) { result ->
            // handle QRResult
        }
        
        Button(onClick = { scanQrCodeLauncher.launch(null) }) {
            // ...
        }
    }
  5. Configure ScannerConfig options

    main

    The ScannerConfig builder allows you to customize the scanning experience. Key options include:

    • setBarcodeFormats(List<BarcodeFormat>): Restrict scanning to specific formats.
    • setOverlayStringRes(Int): Set the string resource for the scanner overlay.
    • setOverlayDrawableRes(Int): Set the drawable resource for the scanner overlay.
    • setHapticSuccessFeedback(Boolean): Enable or disable haptic feedback on success.
    • setShowTorchToggle(Boolean): Show or hide the flashlight toggle.
    • setShowCloseButton(Boolean): Show or hide the close button.
    • setHorizontalFrameRatio(Float): Set the horizontal overlay ratio (default is 1 / square frame).
    • setUseFrontCamera(Boolean): Use the front-facing camera.
    • setKeepScreenOn(Boolean): Keep the device screen on during scanning.
  6. Customize barcode scanning with ScanCustomCode()

    main

    For advanced configurations, use the ScanCustomCode() ActivityResultContract. Instead of calling .launch(null), pass a ScannerConfig object built via the ScannerConfig.build { ... } DSL to control scanner behavior.

    val scanCustomCode = registerForActivityResult(ScanCustomCode(), ::handleResult)
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        …
        binding.button.setOnClickListener {
          scanCustomCode.launch(
            ScannerConfig.build {
              setBarcodeFormats(listOf(BarcodeFormat.FORMAT_CODE_128)) // set interested barcode formats
              setOverlayStringRes(R.string.scan_barcode) // string resource used for the scanner overlay
              setOverlayDrawableRes(R.drawable.ic_scan_barcode) // drawable resource used for the scanner overlay
              setHapticSuccessFeedback(false) // enable (default) or disable haptic feedback when a barcode was detected
              setShowTorchToggle(true) // show or hide (default) torch/flashlight toggle button
              setShowCloseButton(true) // show or hide (default) close button
              setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
              setUseFrontCamera(true) // use the front camera
              setKeepScreenOn(true) // keep the device's screen turned on
            }
          )
        }
    }
    
    fun handleResult(result: QRResult) {
        …
    }
  7. Handle QR scan results with QRResult

    main

    The scanner returns a QRResult object, which is a sealed class representing the outcome of the scan. Handle the following subtypes:

    • QRSuccess: Successfully detected a QR code. Contains a QRContent object.
    • QRUserCanceled: The user canceled the scanning activity.
    • QRMissingPermission: The user denied camera permissions.
    • QRError: An exception occurred within CameraX or ML Kit. Contains the exception object.