Android Development 103: Barcode Scanner!

Ok let’s finally do something useful! Of course this has been done a million times, but what the heck, let’s make a barcode scanner.

First off, we are going to build it with Google’s ZXing (zebra crossing) library. This allows us to open the camera, use the app they already built and capture the data without us having to write our own barcode library. Here it goes:

Let’s make our layout file (under App -> res -> layout -> activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
	android:orientation="vertical" >

    <Button android:id="@+id/button_scan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_scan" />

    <TextView
	    android:id="@+id/scan_format"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:textIsSelectable="true" />

	<TextView
	    android:id="@+id/scan_content"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:textIsSelectable="true" />

</LinearLayout>

Then we need to populate some strings for those files (App -> res -> strings.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Barcode</string>
    <string name="action_settings">Settings</string>
    <string name="button_scan">Scan</string>
    <string name="scan_format">Scan Format</string>
    <string name="scan_content">Scan Content</string>

</resources>

Now we need to write out app. We are going to include the google Zxing library. To do that right click the “src” directory. Then select “New” -> “Package”. Enter “com.google.zxing.integration.android” for the “Name”

Now we need to add some classes. Download the latest zxing library (https://code.google.com/p/zxing/downloads/list) and unzip it. Navigate to the IntentIntegrator.java file (zxing-2.2 -> android-integration -> src -> com -> google -> zxing -> integration -> android -> IntentIntegrator.java) and open the file in your favorite text editor. Copy the file contents.

Create a new class in your newly created package (right click your com.google.zxing.integration.android package and select “New” -> “Class”). Name your file “IntentIntegrator.java” and paste in the contents of the library file.

Create another new class named “IntentResult.java” and paste the contents of IntentResult.java in the downloaded library file.

Now in MainActivity (App -> src -> com.example.barcode -> MainActivity.java):

package com.example.barcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class MainActivity extends Activity {

	private Button buttonScan;
	private TextView textFormat, textContent;

	private static String logtag = "barcode";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buttonScan = (Button)findViewById(R.id.button_scan);
		textFormat = (TextView)findViewById(R.id.scan_format);
		textContent = (TextView)findViewById(R.id.scan_content);

		buttonScan.setOnClickListener(scanListener);
	}

	private OnClickListener scanListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.getId() == R.id.button_scan) {
				// we know we are talking about the button
				IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
				scanIntegrator.initiateScan();
			}
		}
	};

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

		Log.d(logtag, "able to set the text");

		if(scanResult != null) {
			Log.d(logtag, "able to set the text");

			String scanContent = "Scan Content: ";
			String scanFormat = "Scan Format: ";

			scanContent += scanResult.getContents();
			scanFormat += scanResult.getFormatName();

			Log.d(logtag, "able to set the text");

			// put the results to the text
			textContent.setText(scanContent);
			textFormat.setText(scanFormat);

		} else {
			Toast.makeText(MainActivity.this, "we didn't get anythign back from the scan", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

This will open the camera, scan for a barcode, and return the information about the barcode to you. I’ll post a video of how to implement this in a second.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.