165

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
wajiw
  • 11,780
  • 17
  • 50
  • 73
  • 1
    Check [my detailed answer](http://stackoverflow.com/a/30572168/165071) with screenshots and sample Android app. – Alexander Farber Jun 02 '15 at 16:48
  • ZXing is not the only way to read a barcode. As of 2016, it's much easier to use the [Android Barcode API](http://stackoverflow.com/questions/6327483/implement-bar-code-scanner-in-android/38881708#38881708). – Dan Dascalescu Aug 10 '16 at 22:17
  • I made code for barcode generate and scan barcode. You can follow this to get the Step By Step Code. https://stackoverflow.com/a/58742737/11613683 – Pramesh Bhalala Nov 07 '19 at 06:19

7 Answers7

210

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.
Christopher Orr
  • 106,059
  • 26
  • 192
  • 189
  • 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? – wajiw Jan 13 '10 at 16:54
  • 5
    (I'm the project dev BTW -- we can continue at http://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 Jan 13 '10 at 18:46
  • 14
    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 http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java 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. – Sean Owen Jan 13 '10 at 18:47
  • 1
    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)? – agentofuser Apr 11 '10 at 18:05
  • 2
    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. – Christopher Orr Apr 11 '10 at 21:55
  • 1
    Using this, if i scan a barcode, it adds the scanned content to barcode scanner app, can i disable that? – Seshu Vinay Dec 14 '11 at 10:05
  • 1
    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?http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fandroid%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fandroid in this many are there.so anybody tell clearly what are the folders to be added.please give the solution to this. – user1213202 Jun 14 '12 at 13:10
  • can i get image from barcode scanner?because content provide only name of barcode.I need image please help. – Ashish Mishra Mar 28 '13 at 09:49
  • @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. – Jan Hudec Apr 24 '13 at 13:07
  • I made code for barcode generate and scan barcode. You can follow this to get the Step By Step Code. https://stackoverflow.com/a/58742737/11613683 – Pramesh Bhalala Nov 07 '19 at 06:18
41

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user496827
  • 411
  • 4
  • 2
23

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

IntentIntegrator.java

Serg
  • 3,054
  • 1
  • 31
  • 45
Yack
  • 1,380
  • 1
  • 10
  • 13
11

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Aracem
  • 6,928
  • 3
  • 35
  • 71
11

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.

Michael Peterson
  • 9,046
  • 3
  • 51
  • 48
6

Barcode Detection is now available in Google Play services. Code lab of the setup process, here are the api docs, and a sample project.

Jim Baca
  • 5,684
  • 2
  • 20
  • 30
3

You can use this quick start guide http://shyyko.wordpress.com/2013/07/30/zxing-with-android-quick-start/ with simple example project to build android app without IntentIntegrator.

shyyko.serhiy
  • 478
  • 2
  • 9
  • 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 Jan 28 '14 at 15:49
  • found it! it's only scanning in landscape mode... any idea how do solve this? – Lucas Jota Jan 28 '14 at 16:06
  • 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. – shyyko.serhiy Jan 29 '14 at 10:33