QR code

As we have started adopting digitalization, many of us might have realized its importance. One of the common aspects in it is QR code. QR code i.e. Quick Response Code, in layman terms, is a type of code that contains information not readable by humans.

So, how to read a QR code ???

Well, a QR code can be read by an imaging device such as a camera. Several mobile applications have made use of QR codes to pay or receive money in a secured way.

If you are thinking of making such an app, well you have come to the right place !!!!

In this blog post, we will look into the process of scanning a QR code using a port of the Zxing library that is a barcode scanning library.

Get Started

Let’s start by adding the required dependencies in the app level build.gradle file as follows :

compile ‘com.journeyapps:zxing-android-embedded:3.5.0’

compile ‘com.android.support:appcompat-v7:25.3.1’

IntentIntegrator in Activity

We need to use IntentIntegrator in order to initiate the activity which will scan the QR code as follows :

IntentIntegrator intentIntegrator = new IntentIntegrator(this);

intentIntegrator.initiateScan();

IntentIntegrator in Fragment

We need to use IntentIntegrator in order to initiate the fragment which will scan the QR code as follows :

IntentIntegrator.forFragment(this).initiateScan();

Get the results of the scan

@Override

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

       IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

       if (result != null) {

           if (result.getContents() == null) {

               Log.d(“scan”, “cancelled”);

           } else {

               Toast.makeText(this, “Scanned: ” + result.getContents(), Toast.LENGTH_LONG).show();

           }

       } else {

           super.onActivityResult(requestCode, resultCode, data);

       }

   }

Customized Scan

// for scanning QR code

               intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);

               intentIntegrator.setPrompt(“Start scanning”);

               intentIntegrator.setOrientationLocked(false);

   intentIntegrator.setBeepEnabled(false);

               intentIntegrator.setCameraId(0);

You can use barcode formats like QR_CODE_TYPES to scan only QR codes or ALL_CODE_TYPES to scan any type of barcode.

The setPrompt() method can be used to show a prompt message when a user is scanning the code.

The setBeep() method can be used to turn on/off the beep when a user is scanning the code.

Orientation changes

To change the orientation, you can specify the orientation in your AndroidManifest.xml file as follows :

<activity

           android:name=”com.example.zxingdemo.MainActivity”

           android:screenOrientation=”portrait”

           tools:replace=”screenOrientation” />

In order to use your own orientation, use the following :

intentIntegrator.setOrientationLocked(false);

There are a lot of other options which you can explore in IntentIntegrator.

That’s it !!!!

Well, I think after looking at the steps to integrate, you will definitely find it very easy to implement Zxing in your app.

Also, you can take a look at my demo for scanning QR code using Zxing.

Well, in case I have missed out anything, you can refer Zxing in Android for more details.

Suraj Makhija

Software Engineer(Android) at LetsNurture Infotech, a Passionate Android Explorer and a CR7 Fan

Want to work with us? We're hiring!