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

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:
http://stackoverflow.com/questions/11205183/integrating-zxing-barcode-scanner?rq=1
http://stackoverflow.com/questions/4854442/embed-zxing-library-without-using-barcode-scanner-app/4854637#4854637
http://stackoverflow.com/questions/16433860/how-to-use-zxing-library-wihtout-installing-barcodescanner-app
https://github.com/zxing/zxing
http://code.google.com/p/zxing/wiki/ScanningViaIntent
 

7 comments

  1. Alexander Farber says:

    Check my detailed answer on how to use ZXing in Android app: http://stackoverflow.com/a/

  2. Prahlad Yeri says:

    Great answer, @Alexander Farber! If I were to write an app today and need barcode scanner integration, is passing an intent to ZXing still the way to go? Or is it better to build and embed ZXing itself in my app?

  3. aaa says:

    Test post digimarc

  4. Alexander Farber says:

    Passing intent is the recommended way.

  5. justin Martin says:

    Nice article, Thanks for sharing this information about the barcode app, I am looking for some kind of barcode app for my project and its look perfect for me. So thanks for sharing this info with us. (http://www.primabusinessuk.com)

  6. Monivu says:

    Wonderfull steps.I m very very thankful to u for posted this 3 steps in that blog..I m use these 3 steps barcode scanning in anroid app .Thankyou so much…monivu

  7. Krishna Prasad says:

    I have phone gap based android app. can I integrated Zxing into it.
    are there any references and steps for it?

Leave a Reply

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