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.
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.
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.
The activity result is a subclass of the sealed QRResult
class:
QRSuccess
when ML Kit successfully detected a QR code- wraps a
QRContent
object
- wraps a
QRUserCanceled
when the Activity got canceled by the userQRMissingPermission
when the user didn't accept the camera permissionQRError
when CameraX or ML Kit threw an exception- wraps the
exception
- wraps the
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.
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.
. Stetho