Using ZXing to create an Android barcode scanning app

235,371

Solution 1

The ZXing project provides a standalone barcode reader application which — via Android's intent mechanism — can be called by other applications who wish to integrate barcode scanning.

The easiest way to do this is to call the ZXing SCAN Intent from your application, like this:

public Button.OnClickListener mScan = new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }
};

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
        }
    }
}

Pressing the button linked to mScan would launch directly into the ZXing barcode scanner screen (or crash if ZXing isn't installed). Once a barcode has been recognised, you'll receive the result in your Activity, here in the contents variable.

To avoid the crashing and simplify things for you, ZXing have provided a utility class which you could integrate into your application to make the installation of ZXing smoother, by redirecting the user to the Android Market if they don't have it installed already.

Finally, if you want to integrate barcode scanning directly into your application without relying on having the separate ZXing application installed, well then it's an open source project and you can do so! :)


Edit: Somebody edited this guide into this answer (it sounds a bit odd, I can't vouch as to its accuracy, and I'm not sure why they're using Eclipse in 2015):

Step by step to setup zxing 3.2.1 in eclipse

  1. Download zxing-master.zip from "https://github.com/zxing/zxing"
  2. Unzip zxing-master.zip, Use eclipse to import "android" project in zxing-master
  3. Download core-3.2.1.jar from "http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/"
  4. Create "libs" folder in "android" project and paste cor-3.2.1.jar into the libs folder
  5. Click on project: choose "properties" -> "Java Compiler" to change level to 1.7. Then click on "Android" change "Project build target" to android 4.4.2+, because using 1.7 requires compiling with Android 4.4
  6. If "CameraConfigurationUtils.java" don't exist in "zxing-master/android/app/src/main/java/com/google/zxing/client/android/camera/". You can copy it from "zxing-master/android-core/src/main/java/com/google/zxing/client/android/camera/" and paste to your project.
  7. Clean and build project. If your project show error about "switch - case", you should change them to "if - else".
  8. Completed. Clean and build project. You can click on "Proprties" > "Android" > click on "Is Libraries" to use for your project.

Solution 2

I had a problem with implementing the code until I found some website (I can't find it again right now) that explained that you need to include the package name in the name of the intent.putExtra.

It would pull up the application, but it wouldn't recognize any barcodes, and when I changed it from.

intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

to

intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");

It worked great. Just a tip for any other novice Android programmers.

Solution 3

Using the provided IntentInegrator is better. It allows you to prompt your user to install the barcode scanner if they do not have it. It also allows you to customize the messages. The IntentIntegrator.REQUEST_CODE constant holds the value of the request code for the onActivityResult to check for in the above if block.

IntentIntegrator intentIntegrator = new IntentIntegrator(this); // where this is activity 
intentIntegrator.initiateScan(IntentIntegrator.ALL_CODE_TYPES); // or QR_CODE_TYPES if you need to scan QR only

IntentIntegrator.java

Solution 4

Using Zxing this way requires a user to also install the barcode scanner app, which isn't ideal. What you probably want is to bundle Zxing into your app directly.

I highly recommend using this library: https://github.com/dm77/barcodescanner

It takes all the crazy build issues you're going to run into trying to integrate Xzing or Zbar directly. It uses those libraries under the covers, but wraps them in a very simple to use API.

Solution 5

If you want to include into your code and not use the IntentIntegrator that the ZXing library recommend, you can use some of these ports:

I use the first, and it works perfectly! It has a sample project to try it on.

Share:
235,371

Related videos on Youtube

wajiw
Author by

wajiw

I'm a LAMP/Flex/Java engineer out to learn how to make the best web apps possible.

Updated on April 26, 2020

Comments

  • wajiw
    wajiw about 4 years

    I've been searching for how to add a barcode scanner to my app. Are there any examples or how can I do this easily?

    • Alexander Farber
      Alexander Farber almost 9 years
      Check my detailed answer with screenshots and sample Android app.
    • Dan Dascalescu
      Dan Dascalescu almost 8 years
      ZXing is not the only way to read a barcode. As of 2016, it's much easier to use the Android Barcode API.
    • Pramesh Bhalala
      Pramesh Bhalala over 4 years
      I made code for barcode generate and scan barcode. You can follow this to get the Step By Step Code. stackoverflow.com/a/58742737/11613683
  • wajiw
    wajiw over 14 years
    Thanks guys! I'm a newbie android dev and really just wanted to start figuring out what it would take to get a bar code scanner to work. I still need to figure out how to even add com.google.zxing to my project. Is that as easy as just using com.google.zxing in my code or do I have to download the source and import it to my manifest file?
  • Sean Owen
    Sean Owen over 14 years
    (I'm the project dev BTW -- we can continue at groups.google.com/group/zxing) Christopher is right. By using code like that you don't need to import any project code at all. You are calling out to the Barcode Scanner app via Intent; no barcode scanning code in your app.
  • Sean Owen
    Sean Owen over 14 years
    The really nice way to do it involves a little more code, which will make sure the user is cleanly prompted to install Barcode Scanner if necessary. That's the other link he referred to. Copy the class at code.google.com/p/zxing/source/browse/trunk/android-integrat‌​ion/… and use that. Again no other code needed. If you want you can go all the way and embed the scanning code, but without a hard reason to do it, it's only harder for you.
  • agentofuser
    agentofuser about 14 years
    So to use this IntentIntegrator I have to copy it into my project (in that case svn:externals might be a good idea to avoid keeping a stale version)?
  • Christopher Orr
    Christopher Orr about 14 years
    Yes, you'd need to copy it (taking note of the Apache Licence requirements). Though it's so simple, I wouldn't bother with keeping up-to-date via svn:externals or anything.
  • Seshu Vinay
    Seshu Vinay over 12 years
    Using this, if i scan a barcode, it adds the scanned content to barcode scanner app, can i disable that?
  • user1213202
    user1213202 almost 12 years
    i want to scan the barcode without installing the app.if we want to directly scan the code u r saying copy the source code to the project.can anybody tell clearly what code has to be added.in that link many folders are there right?code.google.com/p/zxing/source/browse/… in this many are there.so anybody tell clearly what are the folders to be added.please give the solution to this.
  • Ashish Mishra
    Ashish Mishra about 11 years
    can i get image from barcode scanner?because content provide only name of barcode.I need image please help.
  • Jan Hudec
    Jan Hudec about 11 years
    @AshishMishra: Image of what? The information encoded in the bar code is textual (or numerical) and that's what you get. It may encode where to get an image (URL to download it from or product id to look up in some catalogue or such), but that depends on the specific kind of code you are scanning.
  • Victor Sergienko
    Victor Sergienko over 10 years
    The first one only supports scanning. The second is a good example of encoding a QR code, worked for me.
  • Lucas Jota
    Lucas Jota over 10 years
    I've tried your example project and I can't make it to scan a QRCode, in logcat I'm getting com.google.zxing.NotFoundException and Decode Fail... at DecodeAsyncTask (Inner class of PreviewCallback), the result received by onPostExecute is always null, even if the QRCode is inside the framing rectangle... also I don't see neither the "laser", nor the yellow candidate points that appear at original zxing app (not sure if you've implemented that...)
  • Lucas Jota
    Lucas Jota over 10 years
    found it! it's only scanning in landscape mode... any idea how do solve this?
  • shyyko.serhiy
    shyyko.serhiy over 10 years
    There is no "laser", nor the yellow candidate points, because it's just an example of how ZXing can be integrated i case you want to use different layout and features than with Intent approach. If using layout of Intent is ok for you, you would better stick with approach proposed by Christopher Orr.
  • Dan Dascalescu
    Dan Dascalescu almost 8 years
    This is easier than using Zxing.
  • Peter Mortensen
    Peter Mortensen about 5 years
    Both links are broken (Google Code was shut down). May be you can replace them (e.g. these projects may now be on GitHub)?
  • Pramesh Bhalala
    Pramesh Bhalala over 4 years
    I made code for barcode generate and scan barcode. You can follow this to get the Step By Step Code. stackoverflow.com/a/58742737/11613683