Tutorial: Scan barcodes with ZXing library (Intent integration)

Nowadays, more and more apps have either built-in bar code & QR code scanner or represent their products via these codes. So it’s really the matter of time when the Android developer will build the app with the bar code integration.

With the though of this, I made a simple tutorial on how to integrate the most popular open-source barcode library – ZXing. In this tutorial I will describe an intent integration, which means that I will ask the system if there is a Barcode Scan app installed, if yes – the scan will be performed, if not – I will be re-directed to the Play Store where I could easily download Barcode Scan app and after the installation, – perform the scan. This way, we leave all the heavy-lifting for ZXing library.

In order to start, you need to download the ZXing-X.X.zip (where X.X is the current version of ZXing) from http://code.google.com/p/zxing/downloads/list. Unpack the .zip file and navigate to android-integration folder. There you will see a src folder and a few .jar files. Pick the one that suits you (honeycomb if you build Android 3> app or supportv4 for compatibility…) and put it into the /lib folder of your Android project.

In my tutorial project, I’ve changed a bit the main.xml file, so the result intent data would be easy to see:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

    <TextView
       android:id="@+id/main_textview_desc"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="@string/scan_desc_text"/>

    <Button android:id="@+id/main_button_scan"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="@string/scan_text"
       android:layout_gravity="center_horizontal"
       android:padding="10dp"
       android:layout_margin="10dp"/>

    <TextView
       android:id="@+id/main_textview_scan_result_title"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="@string/scan_result"
       android:textStyle="bold"
       android:textSize="18sp"/>

    <TextView
       android:id="@+id/main_textview_scan_result"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

There is a vertical LinearLayout, some description text on top, Scan button and the scan result text below it. All strings are in strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BarcodeScanProject</string>
    <string name="scan_desc_text">Press Scan button in order to scan a barcode or QR code&#8230;</string>
    <string name="scan_text">Scan</string>
    <string name="scan_result">Scan result</string>
</resources>

Lets add some code to our Activity as well:


package com.lomza.tuts.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button scanButton;
    private TextView resultTextView;

    /**
     * Initializes controls, values...
     */
    private void init() {
        scanButton = (Button) findViewById(R.id.main_button_scan);
        scanButton.setOnClickListener(this);
        resultTextView = (TextView) findViewById(R.id.main_textview_scan_result);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(scanResult != null) {
            Log.i("SCAN", "scan result: " + scanResult);
            resultTextView.setText(scanResult.toString());
        } else
            Log.e("SCAN", "Sorry, the scan was unsuccessful...");
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.main_button_scan) {
            // start scanning
            IntentIntegrator intentIntegrator = new IntentIntegrator(this);
            intentIntegrator.initiateScan();
        }
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
    }
}

Here we just set our main layout, then initialize a result TextView and our Scan button. When the Scan button will be pressed, the IntentIntegrator intent will be fired and the scan process will start! What if you don’t have a Barcode Scan app installed? Well, in that case you will see a Dialog window:

barcode scanner installed question

 

If you will confirm the installation of a Barcode Scan app and press Scan button again, you will see this introductory window:

barcode scanner info screen

 

Pressing Done will lead you to the Camera view with the barcode detection. I would like to mention that your camera should have the auto-focus, otherwise you may not succeed with the scanning.

After a successful scan some product’s barcode (beer’s in my case 🙂 ), the information will display in the TextView below:

scanned barcode info

 

The information is taken from IntentResult and contains the format of the bar code, content itself, raw bytes if there are some, orientation of the camera in the moment of scanning, and the Error Correction level.

That’s it!

Like and share:

Published by

Tonia Tkachuk

I’m an Android Developer, writing code for living and for fun. Love beautiful apps with a clean code inside. Enjoy travelling and reading.