Nowadays, along with the popularity of QR codes, there are many libraries that support us decode into text in our app.
Some of them are Mobile Vision (using Google services and now it is a part of ML Kit), XZing and XZingScanner.
Unfortunately, Mobile Vision only support detecting QR code from Bitmap, XZing is too hard to implement and XZingScanner sometimes occurs "black camera" issue after back from another activity.
So, I decide to make this library as a replacement. I'm using CameraX and XZing because some of resons below:
- CameraX is a powerful Jetpack support library and is the easiest way to add camera capabilities to our apps. It's a perfect choose instead of Camera2 API
- According to testing, XZing seems stronger than Mobile Vision about accuracy and performance.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency
implementation 'com.github.thaitv21:QRCodeScanner:1.0.0'
Create a class named MainApplication
public class MainApplication extends Application implements CameraXConfig.Provider {
@Override
public CameraXConfig getCameraXConfig() {
return Camera2Config.defaultConfig()
}
}Register your application in Manifest
<application
...
android:name=".MainApplication"
...>
...
</application>Declare CAMERA permission for your app
<uses-permission android:name="android.permission.CAMERA"/>
In your layout, add the view
<com.nullexcom.qrscanner.QRScannerView
android:id="@+id/qrScannerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>QRScannerView provides some methods, which helps you scan QR code:
-
startCamera(LifecycleOwner)
Start the rear camera and scan QR code.
LifecycleOwner may be Activity or Fragment -
stopCamera()
Deactive scannable camera -
setCallback(long delay, Result result)
Register callback, which invokes when any QR founded.
delay: timeout between two reported events
result: QR result
- On Android 6 or higher, you need request runtime permission for CAMERA
- startCamera should called in onResume()
- stopCamera should called in onPause()
- If you are getting any error related to library's dependencies, add the following dependencies:
implementation 'androidx.camera:camera-core:1.0.0-beta02'
implementation 'androidx.camera:camera-camera2:1.0.0-beta02'
implementation 'com.google.zxing:core:3.3.3'