quickie is a Quick Response (QR) Code scanning library for Android that is based on CameraX and ML Kit on-device barcode detection. It's an alternative to ZXing based libraries and written in Kotlin. quickie features:
- Easy API for launching the QR scanner and receiving results by using the new Activity Result API
- Modern design, edge-to-edge scanning view with multilingual user hint
- Android Jetpack CameraX for communicating with the camera and showing the preview
- ML Kit Vision API for best, fully on-device barcode recognition and decoding
There are two different flavors available on mavenCentral()
:
Bundled | Unbundled |
---|---|
ML Kit model is bundled inside app (independent of Google Services) | ML Kit model will be automatically downloaded via Play Services (once after app install) |
additional 1.1 MB per ABI (you should use App Bundle or ABI splitting) | smaller app size |
V2 model is used (possibly faster, more accurate) | currently V1 model will be downloaded |
// bundled:
implementation("io.github.g00fy2.quickie:quickie-bundled:1.1.1")
// unbundled:
implementation("io.github.g00fy2.quickie:quickie-unbundled:1.1.1")
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 scanQrCode = registerForActivityResult(ScanQRCode(), ::handleResult)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
…
binding.button.setOnClickListener { scanQrCode.launch(null) }
}
fun handleResult(result: QRResult) {
…
Check out the sample inside this repo or visit the official Activity Result API documentation for more information.
The callback you add to the registerForActivityResult
will receive 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. You can set the supported BarcodeFormat
list, overlayStringRes
and overlayDrawableRes
resource ID.
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))
setOverlayStringRes(R.string.scan_barcode)
setOverlayDrawableRes(R.drawable.ic_scan_barcode)
}
)
}
}
fun handleResult(result: QRResult) {
…
💡 You can optionally pass in an ActivityOptionsCompat object when launching the ActivityResultLauncher to control the scanner launch animation.
You can find the sample app APKs inside the release assets.
- AndroidX
- Min SDK 21+ (required by CameraX)
- (Google Play Services available on the end device if using
quickie-unbundled
)
The MIT License (MIT)
Copyright (C) 2021 Thomas Wirth
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.