Skip to content

πŸ“·πŸ” Android QR code Generating and scanning Application

Notifications You must be signed in to change notification settings

th3kumar/QRather_base

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

QRather_base

Pixel True Mockup high

https://youtu.be/7GtfUSF7Mgg

Note: At Google I/O 2022 the Google code scanner was announced. You should consider using it instead of qrather. If you want to support devices without Play Services or like to ship the latest ML Kit model - use qrather.

Quick Start

View-based

To use the QR scanner simply register the ScanQRCode() ActivityResultContract together with a callback during init or onCreate() lifecycle of your Activity/Fragment and use the returned ActivityResultLauncher to launch the QR scanner Activity.

val scanQrCodeLauncher = registerForActivityResult(ScanQRCode()) { result ->
    // handle QRResult
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    …
    binding.button.setOnClickListener { scanQrCodeLauncher.launch(null) }
}

Check out the sample inside this repo or visit the official Activity Result API documentation for more information.

Jetpack Compose

Use the rememberLauncherForActivityResult() API to register the ScanQRCode() ActivityResultContract together with a callback in your composable:

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

Check out the official Compose Activity Result documentation for more information.

Responses

The activity result is a subclass of the sealed QRResult class:

  1. QRSuccess when ML Kit successfully detected a QR code
    • wraps a QRContent object
  2. QRUserCanceled when the Activity got canceled by the user
  3. QRMissingPermission when the user didn't accept the camera permission
  4. QRError when CameraX or ML Kit threw an exception
    • wraps the exception

Content

The content type of the QR code detected by ML Kit is wrapped inside a subclass of the sealed QRContent class which always provides a rawValue.

Currently, supported subtypes are: Plain, Wifi, Url, Sms, GeoPoint, Email, Phone, ContactInfo, CalendarEvent

See the ML Kit Barcode documentation for further details.

Customization

Use the ScanCustomCode() ActivityResultContract to create a configurable barcode scan. When launching the ActivityResultLauncher pass in a ScannerConfig object:

BarcodeFormat options
BarcodeFormat.FORMAT_ALL_FORMATS
BarcodeFormat.FORMAT_CODE_128
BarcodeFormat.FORMAT_CODE_39
BarcodeFormat.FORMAT_CODE_93
BarcodeFormat.FORMAT_CODABAR
BarcodeFormat.FORMAT_DATA_MATRIX
BarcodeFormat.FORMAT_EAN_13
BarcodeFormat.FORMAT_EAN_8
BarcodeFormat.FORMAT_ITF
BarcodeFormat.FORMAT_QR_CODE
BarcodeFormat.FORMAT_UPC_A
BarcodeFormat.FORMAT_UPC_E
BarcodeFormat.FORMAT_PDF417
BarcodeFormat.FORMAT_AZTEC
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
          setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
          setUseFrontCamera(true) // use the front camera
        }
      )
    }
}

fun handleResult(result: QRResult) {
    …

πŸ’‘ You can optionally pass in an ActivityOptionsCompat object when launching the ActivityResultLauncher to control the scanner launch animation.

Screenshots / Sample App

image

Libraries Used

. BarCodeScanner

. Room Database

. Google Material Design

. Stetho