• Home
  • Articles
  • Use the ZXing Barcode Scanner in an Android App
 
95
0
0

Use the ZXing Barcode Scanner in an Android App

Tek Eyeviatekeye.biz
January 31, 2013

QR Code for tekeye.bizBarcodes and Quick Response (QR) codes are a useful medium for storing short sequences of data that can be recognised by a variety of machines and devices. They are particularly good for stock control hence the universal use on packaging and for logistics. QR codes are useful for communicating web addresses and contact details. Adding support for barcodes and QR codes in an App opens up new interface, communication and feature possibilities.

One of the great features of Android is how easy it is to tap into existing functionality. Scanning barcodes and Quick Response (QR) codes is a good example. Google have a free scanning App that can be accessed via an Intent. This tutorial is an example of how to access the Google Barcode Scanner. For the following example code to work the scanner needs to be installed. Search for Barcode Scanner in the Android Market and install it. It’s published by the Google ZXing team, this link takes you to their market page. ZXing stands for Zebra Crossing (a.k.a. pedestrian crosswalk) which viewed painted on the ground sort of look like barcodes.

In the following layout there are three buttons to choose to scan either a QR code, product barcode or something else. There are two TextViews to display the type of barcode scanned and the data it contains. Open a new Android project in Eclipse using the File menu, select New then Android Project. Fill in the Project Name, here Scan Barcode is used. Click Next and select the build target from the installed APIs, Android 2.1 or higher. Click Next and enter the Package Name in the required format, for example biz.tekeye.scanbarcode, leave Create Activity checked and enter Main as the Activity name, click Finish. In the new project replace the code in res/layout/main.xml with this code (see Copying Code from the Articles tips).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/butQR"
      android:text="QR Code"
      android:textSize="18sp"/>
    <Button android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/butProd"
      android:text="Product"
      android:textSize="18sp"/>
   <Button android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/butOther"
      android:text="Other"
      android:textSize="18sp"/>
  </LinearLayout>
  <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvStatus"
    android:text="Press a button to start a scan."
    android:textSize="18sp" />
  <TextView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tvResult"
    android:text="Ready"
    android:textSize="18sp"
    android:background="@android:color/white"
    android:textColor="@android:color/black"/>
</LinearLayout>

Depending upon which button is pressed the program puts the relevant parameters into an Intent before starting the ZXing Activity and waiting for the result. Replace the Main class in Main.java under the src folder with this code, the imports for OnClickListener, View, Intent and TextView will be required:

public class Main extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    HandleClick hc = new HandleClick();
    findViewById(R.id.butQR).setOnClickListener(hc);
    findViewById(R.id.butProd).setOnClickListener(hc);
    findViewById(R.id.butOther).setOnClickListener(hc);
  }
  private class HandleClick implements OnClickListener{
    public void onClick(View arg0) {
      Intent intent = new Intent("com.google.zxing.client.android.SCAN");
      switch(arg0.getId()){
        case R.id.butQR:
          intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        break;
        case R.id.butProd:
          intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        break;
        case R.id.butOther:
          intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
        break;
      }
      startActivityForResult(intent, 0);    //Barcode Scanner to scan for us
    }
  }
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
      TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
      TextView tvResult=(TextView)findViewById(R.id.tvResult);
      if (resultCode == RESULT_OK) {
        tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
        tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
      } else if (resultCode == RESULT_CANCELED) {
        tvStatus.setText("Press a button to start a scan.");
        tvResult.setText("Scan cancelled.");
      }
    }
  }
}

Notice how it is possbile to scan for a family of barcodes (using SCAN_MODE) or a specific type of barcode (using SCAN_FORMATS). If you know what type barcode is being decoded than setting a scan format to that single particular type may result in faster decoding (it will not be trying to run through all the barcode decoding algorithms), as in intent.putExtra("SCAN_FORMATS", "CODE_39"). For mutliple SCAN_FORMATS pass a comma separated list, see the example program above.

SCAN_MODE SCAN_FORMATS
QR_CODE_MODE QR_CODE
PRODUCT_MODE EAN_13
EAN_8
RSS_14
UPC_A
UPC_E
ONE_D_MODE As for product mode plus…
CODE_39
CODE_93
CODE_128
ITF
CODABAR
DATA_MATRIX_MODE DATA_MATRIX

The ZXing team expand the supported formats in new versions, the SCAN_FORMATS of CODABAR was added in version 2.0, RSS_EXPANDED, AZTEC and PDF_417 formats may be added in future releases.

Android Barcode Scanning

Now go and make that scanning inventory control or grocery list App you’ve been thinking about!

Download some code that covers this article ready for importing into an Android project. The code can also be accessed via the Android Example Projects page. See the article Move Android Code Between PCs Running Eclipse on how to import example code into an Eclipse project. A version of this article was produced for the Android Cookbook.

Discussion

-