Android Scan QRCode Libraries
- https://github.com/dm77/barcodescanner - last release on Aug 19, 2017
- https://github.com/journeyapps/zxing-android-embedded/ - last release on Mar 4, 2018
- Google Mobile Vision API - Barcode API, now migrated to ML Kit Vision - Barcode Scanning
NOTE: I am picking zxing-android-embedded
because my circumstances doesn't allow me to use Firebase/ML Kit. Be aware that zxing-android-embedded
has 156 issues and there is no new release for almost a year. It works on my project which is still using targetSdkVersion 27
using support libraries 27.1.1
.
Depedencies
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
implementation 'com.google.zxing:core:3.4.0'
}
NOTE: This works on SDK 19
and above. For SDK 14
support, refer to this.
Hardware Acceration
The github docs mentioned the need to enable hardware acceleration via AndroidManifect.xml
file.
NOTE: I believe this option is optional.
<application android:hardwareAccelerated="true" ... >
Code
Initialize QR code scanner.
- The default orientation is landscape. I change it to potrait.
The following code will launch activity for barcode scanning.
IntentIntegrator integrator = new IntentIntegrator(this);integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE); // optionalintegrator.setOrientationLocked(false); // allow barcode scanner in potrait modeintegrator.initiateScan();
The following code will receive QR code result.
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { String value = result.getContents(); if (value != null) { Timber.d("qrcode=%s", value); // do something } else { Timber.d("QR code capture canceled or failed"); } } else { super.onActivityResult(requestCode, resultCode, data); }}
Edit AndroidManifest.xml
.
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application ...>
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
</application>
</manifest>
Camera permission (<uses-permission android:name="android.permission.CAMERA" />
) is automatically added by the to. On Android 6+, runtime request permission is automatically handled by IntentIntegrator/CaptureActivity
.
References: