QR Code Scanner Application using Zxing

Its been a few weeks since I created this blog but could not decide what to post first. There was so much to share, too many options. Finally, here’s my first blog post. I hope you find it useful.

A few days back I was trying to Integrate ZXing QR Code  into my android app without having to install BarCode Scanner app. A lot of tutorials I found on the internet went with the installation of BarCode Scanner app which opened BarCode Scanner whenever a scan was required.

However, a much elegant way is to implement a built-in scanner. The easiest way according to my experience is using Zxing Android Embedded. The method requires nothing extra-complicated and it worked for me perfectly. Here’s the link to the complete working project on github.

This tutorial guides you through the steps to create a basic QR scanning app on Android Studio, which displays the scanned result in a dialog box. There are 7 simple steps. Lets get started.

Step 1

Create a new project with Blank Activity.

1

Step 2

We start by setting up our environment.  First step, adding Zxing library as a dependency of our project. Assuming we do not know the package name we require right now, we can search for it. Go to File > Project Structure > Dependencies > + > Library Depedency.

2.1

2.2

Search for “zxing” and select the zxing-parent library. Enter, and you should be able to see it added in your project.

2.3

2.4

“Apply” the changes and close the window. To verify the dependency has been added, open the “build.gradle” file.

2.5

A similar and simpler way to add a dependency is to add the highlighted line in “build.gradle(app)” file.

Step 3

Now to add the Zxing Android Embedded library to our project, we add the following:


compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'

This is what the gradle file finally looks like:

3.1

Step 4

For the final step in setting up the dependencies, download core.jar and add it in the “libs” folder of the project.

4.1

Step 5

The setup is now complete. All that remains now is the code. Add a button to the xml UI file. We’ll use this button to start the scanning activity.

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Scan Code" android:id="@+id/button_scan" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:onClick="startScan"/> 

5.1

Step 6

Now we come towards editing the MainActivity.java to add the event handler of button click.


public void startScan(View sender) {

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();

}

For more information on IntentIntegrator and other Zxing components, visit this.

I decided to display my result in a dialog box. To decide what we do with the result, we implement the “onActivityResult” method.


public void onActivityResult(int request, int result, Intent i) {

IntentResult scan = IntentIntegrator.parseActivityResult(request, result, i);

Context context = getApplicationContext();

if (scan != null) {

if (result == RESULT_OK) {

String contents = i.getStringExtra("SCAN_RESULT"); //this is the result of scan

AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);

dlgAlert.setMessage(contents);

dlgAlert.setTitle("Code Scanned");

dlgAlert.setPositiveButton("Ok",

new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog here
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();

} else if (result == RESULT_CANCELED) {

Toast toast = Toast.makeText(context, "No result", Toast.LENGTH_LONG);
toast.show();

}

}
}

Step 7 (optional)

I recommend cleaning the project once after adding external dependencies to avoid any chances of time wastage. The app is now ready.

7.1

Result

11949518_1702944449936969_1059588020_n

11951066_1702944406603640_1441681826_n

11950881_1702944446603636_1417959735_n

11940235_1702944443270303_1354104846_n

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s