1

I've been searching on how to add a barcode scanner to my app just to decode barcodes. I found a really good application I could intergrate to do that called "Zxing" but the only problem is, it has encoding and decoding but what i really want is only decoding so that I could limit what I use from the Zxing open source file.

Ive searched plenty of places couldnt find just decoding with zxing?

So my question is how could I use Zxing only to decode and not to encode aswell? Example codes and step by step instructions will be really appreciated.

Thanks in Advanced!

1
  • 1
    In your research, did you come across this question? Jan 25, 2012 at 17:13

1 Answer 1

12

The simplest way to do it is scan via Intent.

here is some sample code:

//This intent will ask the Barcode Scanner app to scan a code and give us the result
Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 

/* you can optionally add an extra to the intent that tells it what type of code its looking for. Like this:
 * 
 * intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
 * 
 * If you don't put that in it will scan all types.
 */

 startActivityForResult(intent, 0);

Then in your onActivityResult() you can get the scanned data like this:

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            // contents contains whatever was encoded
            String contents = intent.getStringExtra("SCAN_RESULT");

            // Format contains the type of code i.e. UPC, EAN, QRCode etc...
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");


        }
    }

}

EDIT:

The Intent model is built in to the very core idea of android. And it was put there for good reason. By having only 1 application that handles the scanning of barcodes and just returns the result to any other application that wants to make use of it, we get less wasted space.

For instance, if a user has 5 apps that all scan different barcodes and do various things with them if all 5 of those apps include their own barcode decoding within their own app, the user now has 5 copies of the barcode reading functionality on their device wasting space.

Another upside to the Intent model(specifically with barcode decoding) is that it allows your application to require fewer permissions. Because you no longer need access to the camera.

It also makes everything much simpler for the developer. As you can see above the amount of effort it takes to integrate with zxing via intent is minimal. You'll find that it is possible to re-use some portions of the Zxing project and include them within your application. But that it is much more difficult to get up and running.

The ZXing project is open sourced so you are of course welcome to start picking through the source to figure out which classes you'll need to manually copy to your project. I suggest that if you intend to go this route that on your first attempt you do not try to remove the encoding functions. Get the entire thing up and working, and then start removing stuff. It is likely that if you try to take only some subset of the project some things won't function properly even though they don't seem like they should be affected by what you've left out.

One last bit of advice, I totally understand why you want to include this functionality within your own app(I've been there myself). But don't make this decision lightly and do take some time to consider things like this: Lets say you do include this functionality within your own application. All goes well for a while. But after some time goes by you start to see strange errors popping up on some devices that cause them to be unable to use the scanning functionality of your app. Now you will be in a situation where you are going to have to try to debug and fix something that you did not create, and likely do not completely understand all of what is going on under the hood of. Whereas integrating with Intents makes it so that this debugging and fixing is done by the people who actually know every bit of how it works and what problems need addressed, and they work on their own update schedule. So this fixes will get out to the masses much quicker than you could probably get them out.

2
  • But the problem is scanning through intent will mean the user will have to download Zxing seperatly but I want to intergrate it together and only decode
    – Sri
    Jan 25, 2012 at 17:19
  • I do not view them having to download Barcode Scanner separately as a "problem". This model keeps apps focused on what it is that they do best, and leaves the more complicated tasks(scanning barcode, choosing audio file, choosing gallery image etc) to the things that were designed specifically to do that exact task. Intents and this model are very fundamental to the idea of android. This is how the system was intended to work =). See my edit for more info.
    – FoamyGuy
    Jan 25, 2012 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.