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.

6 thoughts on “Tutorial: Scan barcodes with ZXing library (Intent integration)”

  1. Hi Tonia,
    Nice Tutorial I need some help in my barcode app.
    I want some information regarding barcode like suppose my product is Tyre and barcode is on Tyre is of particular type or it will be vary like some of UPC or some of EAN?
    How we will get to know product name using barcode number? as per my knowledge UPC barcode type of 12 digit and first 6 digit is represent product name.
    I want to decode barcode number to get this information but i didn’t get any solution for this.
    Currently i am getting barcode number and it’s type using zxing library and i ma saving this barcode number into Sqlite. but now i want to check scanned barcode is of “Particular type” which i want.
    need help.. I just started working on Android.

      1. Hi Antonina,
        thanks for reply. I have seen this link. I don’t want to get product name by google search. Is there any other way? Can we have any opensource database for Barcode? and Is it must to have product database in my case?

        1. I see. Well, if you’re interested only in some products, you can keep them in database (codes and their corresponding names and types) and then – check the code after scan. I don’t know any opensource databases. There is something like https://www.outpan.com/developers.php , try searching for others on Google, maybe there’s something better 😉

Leave a Reply

Your email address will not be published.