This library allows you to easily add CameraX Preview with attached ML Kit to detect and display Qr codes and Barcodes. It provides QrXScanner
layout component with all underlying logic attached.
allprojects {
repositories {
maven(url = uri("https://jitpack.io"))
}
}
dependencies{
implementation("com.github.Luteoos:qrx:1.0.1")
}
For snapshots and non-released version see guide on Jitpack.io
Becasue QrX exposes via listener com.google.mlkit.vision.barcode.common.Barcode
I've decided to import Google ML Kit as an api
dependency. Be aware of this as it might cause problems if you use this dependency on its own in your project.
api("com.google.mlkit:barcode-scanning:17.0.2")
$ git clone https://github.com/Luteoos/qrx.git
$ cd qrx/
$ ./gradlew build
<layout>
<!-- ... -->
<io.github.luteoos.qrx.QrXScanner
android:id="@+id/qrxPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- ... -->
</layout>
private val permissionRequestCamera = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
binding.cameraBarcode.onPermission(it)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.qrxPreview.initialize(
this,
{ // onBarcodeClickListener
},{ //onPermissionRetryListener
})
permissionRequestCamera.launch(Manifest.permission.CAMERA)
}
There is possibility to set listener on every detection result:
binding.qrxPreview.onBarcodeScannedListener = {
// List<Barcode>
}
It will be invoked every ImageAnalysis.Analyzer
cycle, no user interaction required
In runtime you can access and modify View components via
binding.qrxPreview.findViewWithTag<TYPE>(QrXScanner.COMPONENT_TAG)
COMPONENT_TAG | TYPE |
---|---|
CAMERA_PREVIEW | androidx.camera.view.PreviewView |
CAMERA_OVERLAY | ImageView |
CAMERA_TORCH_BUTTON | ToggleButton |
PERMISSION_DENIED_LAYOUT | LinearLayout |
PERMISSION_DENIED_TEXT | TextView |
PERMISSION_DENIED_BUTTON | Button |
MIT License
Copyright (c) 2022 Mateusz Lutecki
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.