Generate scannable barcodes in Android apps by using ZXing

Learn how to enable Android apps to present scannable barcodes for POS systems like the one used in the Starbucks app rewards program.



Mobile payments have been a hard nut to crack, but slowly businesses are getting the hang of it. I still believe paying for goods and services with your phone will become commonplace at your favorite retailers and restaurants in the not too distant future. In 2013, Starbucks generated over $1 billion in mobile transactions. That's billion with a "B," folks.

Ironically, where many companies have attempted high-tech hardware/software solutions like the Verizon-Isis Mobile Wallet, Starbucks instead chose to go the route of a rather low-fi solution and concentrate instead on enticing customers to use its app with loyalty rewards—and it's a strategy that seems to be paying off. In June 2013, Starbucks announced that more than 10% of its sales were done using a mobile phone inside the store, making it the worldwide leader in mobile payment adoption.

So how does its app work? Well, instead of relying on NFC-enabled devices and specially developed POS scanners, Starbucks opted to display a scannable barcode within its app and allow the user to hold his or her phone up to the existing scanners at the counter—exactly as you might do with a plastic loyalty or gift card. Even though the Starbucks app has some drawbacks, the bottom line is it's dead simple to use, and it works on just about any smartphone cranked out in the last three years.

As an application developer, if you have not had to work with barcodes in your app, assume that at some point down the road you will. You'll do more than just read barcodes—you'll also present them on the LCD in a format that can be easily scanned by other devices. Fortunately for Android developers, there is already a very large open source library that is actively maintained and handles most of the heavy lifting for you.

This tutorial demonstrates how to generate a QR code from within your app using intents exposed by the freely downloadable Zebra Crossing (ZXing) barcode scanner. Follow along or download the entire project and import it directly into Eclipse.

1. Create a new Android project in Eclipse. Target Android 2.3 or better.

2. Open the /src/ file and replace the on create with the following:


package com.authorwjf.barcodegen;

import android.os.Bundle;
import android.content.ActivityNotFoundException;
import android.content.Intent;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		Intent intent = new Intent("");
		intent.putExtra("ENCODE_FORMAT", "QR_CODE");
		intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
		intent.putExtra("ENCODE_DATA", "");

		try {
		} catch (ActivityNotFoundException e) {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + "")));


Believe it or not, you're done. The first time you run the app it will take you to Google Play, where you can download the free ZXing scanner. After that, running the app will generate a QR code that directs users to the TechRepublic site.


You may want to display and read barcodes without sending the user to download another application. With ZXing and its Apache license, this can be accomplished; in fact, I did it recently for a client. This requires downloading the source, compiling the ANT script, and copying the resulting core.jar into your project directly. It's beyond the scope of this introduction, but most developers should be able to find everything they need to know in the official ZXing project repository.