How to scan QR Code in Android Apps

If you’re here, you probably need to integrate QR Code functionality into your existing android app. We are doing just that. When it comes to scanning and generating QR Codes into your android apps and for any platform out there for that matter,zxing comes among the libraries that can help. Rightfully so. It is the best in my opinion.

QR Code scanning on android can have a huge amount of details and most of the time the developer just wants to start some activity and get the result. That’s why we created a library to help you do that. No more talking, lets do some work.

The library is located at our git repo if you want to take a look.To use it, follow these steps. We assume you are using Android Studio.

1.Add jitpack dependency to your root gradle file

allprojects {
     repositories {
	...
	maven { url 'https://jitpack.io' }
	}
   }

2. Add the library dependency to your app level build.gradle file

dependencies {
    compile 'com.github.blikoon:QRCodeScanner:0.1.1'
   }

3.Declare the request code for qr scanning in one of your activities in your code

private static final int REQUEST_CODE_QR_SCAN = 101;

4.Start the QR Code scanning process when the user clicks on some button or in response to some other event

public void onClick(View v) {
                //Start the qr scan activity
                Intent i = new Intent(MainActivity.this,QrCodeActivity.class);
                startActivityForResult( i,REQUEST_CODE_QR_SCAN);
}

At this point the library takes care of scanning for you. You can even choose an image from your gallery to be scanned.
5.Get the scan result back

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

        if(resultCode != Activity.RESULT_OK)
        {
            Log.d(LOGTAG,"COULD NOT GET A GOOD RESULT.");
            if(data==null)
                return;
            //Getting the passed result
            String result = data.getStringExtra("com.blikoon.qrcodescanner.error_decoding_image");
            if( result!=null)
            {
                //We fall here if the image the user chose to scan did not contain any QR Code or just failed to be scaned for some other reason
            }
            return;

        }
        if(requestCode == REQUEST_CODE_QR_SCAN)
        {
            if(data==null)
                return;
            //Getting the passed result
            String result = data.getStringExtra("com.blikoon.qrcodescanner.got_qr_scan_relult");
            //Here you have your scan result. Use it in your app
            Log.d(LOGTAG,"Have scan result in your app activity :"+ result);
          
        }
    }

As you can see it is very easy to enrich your app with this library. You can find a full android example app here. If you have any problems using this lib, shoot me in the comments below.

Have fun QR Scanning!

Posted in android, Programming and tagged , , .

Daniel Gakwaya loves computer Hardware/Software.He is a Software Engineer at BLIKOON and lead developer of bliboard-The whiteboard system currently marketed by the company.He is known to hack around on any piece of tech that happens to pick his interest. More on his tech endeavors here
Follow him on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.