3 Steps to integrate barcode scanning in your Android app

    

Whilst barcode scanning is a pretty complex and non-trivial task in itself, it could be overwhelming sometimes with android programming. Lucky for us, there exists an opensource project called ZXing (pronounced Zebra-crossing) that solves this problem for us.

Barcode{.wp-image-152 .alignnone width=”108” height=”77”}

The ZXing project has already done the heavy lifting by programming the core java components required to scan a 1d/2d barcode or even a PR-code in the form of a Google-play app. All you have to do is send an intent to this app and receive the scanned results that you may use in your app.

The ZXing project is Apache licensed, so it is free to use without any kind of restrictions. Follow these steps to integrate ZXing with your app:

Step 1: Download the source for IntentIntegrator.java and IntentResult.java from here and add the files to your android project sources.

Step 2: Start an intent in that part of your code where you would like to initiate the barcode scanning (such as a menu handler):

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

Step 3: All that remains now is to handle the result of this activity in your onActivityResult() handler. This is how I did it in my code:

if (requestCode==IntentIntegrator.REQUEST_CODE)
{
	IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
	if (scanResult != null)
	{
		// handle scan result
		//MessageBox.run(this, "", "toString() returns: " + scanResult.toString());
	}
	else
	{
		// else continue with any other code you need in the method
		MessageBox.run(this, "", "scanResult is null.");
	}
}

That`s all! The above code not only returns the barcode scanning result to your app, but even prompts the user to install a barcode scanner app in case one isn’t there. This is the easiest and recommended way of integrating barcode scanning in your android app.

In case you want to embed the entire ZXing component in your app and don’t want to install an app separately for it, refer to the relevant links in the references section. However, this method is not recommended as your app won’t get the updates from ZXing.

References:

[ android  java  ]