Android Scan QRCode Library: zxing-android-embedded

Jun 29, 2019

Android Scan QRCode Libraries

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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.