ZXing

In this tutorial, we will learn to integrate ZXing lib into an Android app. Let us first get some idea about ZXing.

ZXing

ZXing (“zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. ZXing is also hosted under Google Open Source Project.

The latest version up to this date is 3.3.3. Modules in this release we need to focus on are –

Modules
Description
core
The core image decoding library, and test code (Java lib)
android
Android app also available on Play Store Barcode Scanner
android-core
Android-related code shared among android, other Android apps
android-integration
Supports integration with Barcode Scanner via Intent


All the modules are available on Maven Central –

core
android-core
android-integration

Integration

Let’s say we want to integrate ZXing in our app called QR Code Scanner Android. Now the problem is that android module is actually an Android application and not an Android library. So to use the ZXing lib as advocated by ZXing themselves, we have the following options –

Obviously, both these options depend upon the Barcode Scanner app. So to overcome this dependency JourneyApps has come up with ZXing Android Embedded which is loosely based on the ZXing Android Barcode Scanner application but an with the official ZXing project.

ZXing Android Embedded is available on bintray –

ZXing Android Embedded

So this tutorial is based on integrating the ZXing Android Embedded library in our QR Code Scanner app. You can find the source code of this tutorial on QR Code Scanner. Let’s start the integration. From version 3.6.0, only Android SDK 19+ is supported by default.

Dependency

repositories {  
   jcenter() 
}

dependencies {  
   implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}

For Android 14+ support, see this link.

Code

MainActivity.kt

class MainActivity : AppCompatActivity() {     
   override fun onCreate(savedInstanceState: Bundle?) {        
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
         button_scan_qr_code.setOnClickListener {
            IntentIntegrator(this).initiateScan()
        }
    }
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            if (result.contents == null) {
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
            }
        }
    }
 }

On click of button_scan_qr_code, CaptureActivity will start scanning using default camera. Once it scans any QR code, it sends back the result to onActivityResult the MainActivity.

ZXing also provides online QR Code Generator. Enter the required fields, generate and scan it to get the results.

ZXing Android Embedded lib also provides various customization –

For more advanced options do have a look at their Sample application.

That’s it for this tutorial. Thanks to the ZXing authors for Java library and ZXing Android Embedded to make this integration easy. Enjoy scanning the QR code. ????

Do you have any product idea or business need?

Apples In-App Purchase

Hands-On Implementation of Apple’s In-App Purchases On Ruby Based Server

Information about Languages & Tools, Server Configuration and Endpoint & API Setup and many more.

Multi User Chat

Features of Multi User Chat (Group Chat) in Android

Features allows users to visible multiple chatrooms on one or more screens.

android Logs

Simple way to write android Logs

Complete process of writing logs in a simplified way and how to effectively get rid of logs during app release.