Integrate zxing barcode scanner into your Android app natively using Eclipse

June 13, 2011

Edit: Sean Owen, one of the developers for ZXing has posted a comment to this blog warning of the pitfalls of integrating ZXing into your own app; doing so just to avoid having your users take that extra step of installing from the market is not a good reason. I completely agree with this. There are many advantages to using the intent based approach as outlined in his comments. My motivation is for an enterprise app that does not have access to the Android market and involves my client installing zxing manually on thousands of devices before they are able to distribute to its business customers.

So to be clear, do not use this method unless it is absolutely necessary, and if you do have to – make sure that override your intent filters so that other apps that want to use zxing do not end up calling your modified version. Also, if zxing is already installed, then you should use that by default instead of your modified version.

ZXing is one of the most popular barcode scanning applications on the market. They make it very easy for you to integrate into your application via an intent but this means that your users must manually install the application from the market.

Fortunately, the app is also open source so I will show you how to cleanly build this capability into your project.

Please note that the awesome developers of this product have released the src under the Apache v2.o license so please be sure to adhere to the terms of this license and give them full credit for their work. http://www.apache.org/licenses/LICENSE-2.0

Step One: Obtain the zxing src code
The src can be found at http://code.google.com/p/zxing/source/browse/trunk. Specifically you only need the android/ and the core/ projects. Use svn to checkout these to your local hard-drive.

Step Two: Build zxing core using Apache Ant
You will need to build the core project into a jar file using apache ant (download from here http://ant.apache.org/ivy/download.cgi). Using a shell or cmd prompt navigate to the root directory of the downloaded zxing src and execute ”ant -f core/build.xml”. This will produce a file core/core.jar which we will use in the next step.

Step Three: Build ZXing Android using Eclipse
Create a New Android Project (File –> New –> Android Project).
Set the project name to ZXing (or similar).
Select the “Create project from existing source” radio button
Click “Browse” and navigate to the android project that you downloaded from zxing and click “OK”
Select “Finish”

The project will not currently build. We need to add the core.jar file (that we produced in the previous step) into our project. Right-click on ZXing project –> properties –> Java Build Path –> Add External Jars –> Navigate to and select core.jar –> Open –> OK.

Actually, while we’re here we should do one more very important thing! Right-click on ZXing project –> properties –> Android –> Scroll down and check/tick the “Is Library” checkbox –> OK.

Step 4: Include ZXing Android into your project.
Within Eclipse,  Right-click on YOURPROJECTNAMEHERE project –> properties –>Android –> Scroll down to Libraries section –> Click Add –> Select ZXing (which should appear as an option as a result of completing previous step).

Next, in some trigger function e.g. button press within your code you should add:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

In the same activity you’ll need the following to retrieve the results:

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
      }
   }
}

Almost there! One of the current limitations of Android Library projects is that it will not pull anything from AndroidManifest.xml into your project.
So if we try to invoke the above code we will receive a runtime exception because your Android app has no idea how to handle the scan intent.
To fix this you just need to copy the following into your AndroidManifest.xml:

<activity android:name="com.google.zxing.client.android.CaptureActivity"
   android:screenOrientation="landscape"
   android:configChanges="orientation|keyboardHidden"
   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
   android:windowSoftInputMode="stateAlwaysHidden">
   <intent-filter>
      <action android:name="android.intent.action.MAIN"/>
      <category android:name="android.intent.category.DEFAULT"/>
   </intent-filter>
   <intent-filter>
      <action android:name="com.google.zxing.client.android.SCAN"/>
      <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

And as Columbo would say, “Just one more thing!”. Add this permission to the top of your AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA"/>

EDIT: You need to do yet one more thing! You need to add the core.jar (produced in Step two) to your new project (Right-click your project –> Properties –> Java Build Path –> Add External JARS… –> Select core.jar –> OK). Thanks to Marco and Markosys in the comments for spotting and pointing out the omission!

Advertisement

167 Responses to “Integrate zxing barcode scanner into your Android app natively using Eclipse”

  1. Lorenz Stierlin Says:

    Thanks for this post. It was really helpfull. But i have the problem that the retangle with the red laser line is in the right corner and not in the middle of the screen… i use a android 2.2 htc desire HD for testing. curiously on a virtual device it works correctly. maybe you can help me? thanks a alot

  2. Oskar NRK Says:

    Hi! I tried the integration and it works, thanks :)

    But there’s a problem: “Barcode Scanner” takes control of application name: the application name displayed is “Barcode Scanner”, not the one I provided in my application manifest, I can’t fix it. Any suggestion?


    • Doesn’t the barcode scanner go full screen without titlebar?
      In any case, if you want to customise, you can go into the src of the Android ZXing app, change stuff there and recompile. I did this to disable many of the menus.

      • Oskar NRK Says:

        Yes it does. The problem that I launch Barcode Scanner with a button in my application but including ZXing as library the main app I’m developing gets “Barcode Scanner” as application name. It seems like Barcode Scanner overrides my manifest settings.
        Now I try to edit Barcode Scanner manifest a bit. Thanks :)

      • Oskar NRK Says:

        Found the problem: the “values-” folders. I’m Italian and my test device language is Italian.

        By default the main app’s app_name overrides the library’s app_name but using locales and not in the main app doesn’t work. I deleted all the “values-” folders in the library and now all works fine.

        Now I’m trying to disable menu in the capture view.

    • Carlos Says:

      Good question men… You help me a lot!!! Thanks Oskar

  3. Drew Says:

    Hi, I am an absolute noob here. But someone help me out here?

    On Step 4, it says I am suppose “Select Zxing” in the library. But when I click on “Add”, there is no option at all, not to mentioned Zxing. Did I do anything wrong? Please help!

    Thanks!
    Andrew

    Thanks!

  4. G.Letellier Says:

    Thank’s for your tutorial !! Works Great !!!

  5. emna Says:

    hi! help me please, i did all the three first steps but on step 4 there is no options, what should i do?

  6. Kevin Says:

    emna, I believe Step 4 is worded incorrectly

    The original:

    Step 4: Include ZXing Android into your project.
    Within Eclipse, Right-click on ZXing project –> properties –>Android –> Scroll down to Libraries section –> Click Add –> Select ZXing (which should appear as an option as a result of completing previous step).

    Should read:

    Step 4: Include ZXing Android into your project.
    Within Eclipse, Right-click on YOURPROJECTNAMEHERE project –> properties –>Android –> Scroll down to Libraries section –> Click Add –> Select ZXing (which should appear as an option as a result of completing previous step).

    If you followed the original wording you would be adding library project to itself–which is senseless. Other than that, pretty nicely done… thanks for the help.

  7. Darshan Says:

    HI !

    Please check this application on github called QRC

    https://github.com/egure/diordna/tree/master/QRC

    This has the integrated QRC source , you can build application based on this.

    Thanks,
    Darshan

  8. emna Says:

    another question please! when i push the button to start scanning i only got the retangle with the red laser line without a result, i waited… but still nothing. what could be the problem please!


    • The scan mode in the post is set to QR code. With this setting it will only scan 2d barcodes


      • I have the same problem. I installed the Zxing app (that is being built from the downloaded source codes) into my device. I get the rectangle with the red line, but it just keeps on trying to read the barcode. No matter if it is a 1D or 2D barcode.
        Although the Zxing app that is available in android market works perfectly.
        Could the android target platform be an issue? I have tried android 2.2 and 2.1 to build this project.
        Great tutorial btw.

      • Sayali Says:

        I have same issue. Does it have other modes? What are they & how to use them. Can you please provide any tutorial link if any?

  9. Brandon Says:

    First off this is a brilliant article. I have been searching over the net for help with fully integrating the zxing and was able to solve my issues through this.

    Now to my wonderful new hurdle:
    How to get the product details with the successful scan. I have been looking through the zxing source code and have been trying to impelement the ProductResultHandler ( http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/result/ProductResultHandler.java ) without success in getting the product info. Any assistance would be greatly appreciated.

  10. Marco Says:

    great tutorial.
    I’m going to link it on my blog, I hope you could be happy about that ;)


  11. [...] zxing barcode scanner in your Android application Hi there, let me highlight this good tutorial of Damian Flannery about adding barcode scanner capabilities to your Android [...]

  12. Aditya Says:

    Hi,I have copied and paste in android manifest file…but still getting
    run time exception…Please help…

    Thanks,
    Aditya…

  13. Manish Says:

    thanks a lot..this code works wonders..!!!

  14. Ted Says:

    Thx for this tutorial, but everytime I build the android Project from existing source (step 3), src and res are full of unresolved variables. Can anyone help?


  15. Hi all, I have a problem. I did everything as in tutorial, but still have a problem. I think ZXing project witch I convert to library can’t correctly included to my project. First time when I add ZXing library to my project I see green icon, I ‘ve built project, run on emulator and got “runtime exception”. After I go to properties an see red icon on Properties -> Android -> Library (earlier included)…
    Maybe someone can help me ?

    This is screenshot of my error – http://imageshack.us/f/19/unledqpk.png/

  16. Aditya Says:

    Hi,
    I have follwoed step by step.But getting Run time Error.My manifest is like below….

    Please Help…..want urgently.

  17. Aditya Says:

    Hi,
    I have follwoed step by step.But getting Run time Error.My manifest is like below….

    Please Help…..want urgently.

  18. Jacob Says:

    When I add ZXing as a library I get like 400 errors.
    Lots of things which cannot be resolved to a type like AddressBookParsedResult BarcodeFormat. Hope someone can point me to what im doing wrong.

  19. david Says:

    Great article, thanks!

    I’m having a little trouble following, though. So I have the whole zxing project, and I’ve been able to build the core.jar using ant.

    In Eclipse, when I create the new project, it wants to default to android 2.3.1. I want it to use 2.2, since my phone doesn’t have 2.3 yet and I’d like to try this out on it. I can import the core.jar and have it build with 2.3.1, but if I try to change the project to 2.2, I get an error in my manifest with it not liking the xLargeScreens attribute, then all kinds of other errors pop up and it all goes to hell.

    Is it because the core is built for 2.3.1 as well? How do I make that build for 2.2?

    Also – if I create my own project and try to add in my CaptureActivity project (using 2.3.1 – just trying to see if i can get a full build going), it adds the CaptureActivity_src and then I get all these errors unless I also add core.jar to my project.

    I’m doing this on a mac with eclipse 3.6 and all the up to date android stuff.

    Thanks!

  20. Quang Anh Says:

    Thank you so much,this tutor help me a lot .

  21. r4ul90 Says:

    I’m trying to follow your tutorial. Currently I can add the ZXing project as a library to another project, when I make it the first time I see a tick icon under the reference column, but after I close this window and check properties again I see a red X, I don’t understand what can be wrong, perhaps Eclipse? I’m using Eclipse 3.6.2 and I can build the app but when I try to actually scan it crashes I believe it has something to do with this reference, I would appreciate any help you can provide me with, thanks in advance and great tutorial.

  22. r4ul90 Says:

    Are you using Linux or Mac? I can’t find a way to make this work finally I solved the red X issue, now I always see a green tick the reason was that the projects were located in different partitions.

    Now the problem I have is the ‘library project’ I’ve added the core.jar file (I’ve built it lots of times always getting a success message), but somehow it doesn’t work I get errors (tons of them) in the source files, I suppose those errors should disappear when the file (core.jar) is added to the project, but for me it doesn’t work (it’s frustrating) I’ve already added it many times as an external jar.

    Hope you can help me I’m using Win 7 x64 and Eclipse 3.7.0. Thanks in advance for your help.

    • r4ul90 Says:

      I solved the issue I set the library project profile to android 2.2 and that made it work. Of course the core.jar file was added as an external jar.

      • Rahul Popuri Says:

        Hi,
        I tried what you said, and it makes the compiling errors in the library go away, but when I launch the activity it gives me a message: “Sorry the Android camera encountered a problem. You may need to restart the device.”

  23. Alejandro Says:

    I followed all the steps and finally I can use the Barcode Scanner with my application but just if the Barcode Scanner is installed or if I used the debbuger of Eclipse (which installs the Barcode Scanner App from the library created on the step 3). My question is: How can I fully integrate the Barcode Scanner code into my application without install it and with no trace of the original Barcode Scanner? I think is weird for the user that my app installs another app in the device, and I’m not sure if I’m doing something wrong.
    Thanks.

  24. Marco Says:

    Amazing tutorial, works perfectly!

    I have only one question, if I press the “CAMERA SCAN” button my phone (that has Barcode Scanner APP installed) asks me:
    “complete action using”
    - BARCODE SCANNER
    - MY APP

    Is there a way to specify in the intent not to show the “complete action” dialog? and just start MY APP as default?

    thanks :)


    • Thanks.

      You need to change the action intent (in the zxing that’s included in your app) to something custom.

      e.g. Change public static final String ACTION = “com.google.zxing.client.android.SCAN”; to String ACTION = “com.yourco.yourproject.android.SCAN”; in com.google.zxing.client.android.Intents.java (in ZXing project)
      and change to in the AndroidManifest for the ZXing project.

      Then Recompile. That should do it.

      • Marco Says:

        Thank you works great :)

      • Tom Says:

        Would you be able to post what your two manifests look like for this? I cannot for the life of me get this to work. I keep getting

        android.content.ActivityNotFoundException: No Activity found to handle Intent { act=net.blueearth.clowns.android.SCAN (has extras) }

        Thanks!

  25. JayaVenkat Says:

    Hi,
    Actually I followed this tutorial line by line but integrated zxing to my own project but when button clicked the scanning is not performed please correct my problem

  26. Venkat Says:

    Hi,
    Thanks for providing this tutorial, I have followed this tutorial line by line everything works fine .But while integrating with my app I found problem. Scanning is not done when button is clicked

  27. ajay Says:

    I tried to import this project. After successfully creating core.jar and loading the android project – i get tons of errors related to strings.xml.
    Use %s as a placeholder for the product ID, and %f for format

    I get

    error: Multiple substitutions specified in non-positional format; did you mean to add the formatted=”false” attribute? strings.xml /qrcodeprj/res/values line 96 Android AAPT Problem

    • Aditi Says:

      I received a similar error too. Downloaded fresh copy of the android project from zxing project and the errors were resolved.

  28. RacZo Says:

    Hey guys!

    I did everything (get the source, compile with Ant, get the core.jar to my project and include the zxing project as an Android Library on my App) and… sweet! Got the scanner to work (even without asking me what App to use) and after scanning, it returns to my App.

    BUT… it gets back to my own Activity and I try to get the results on the onActivityResult method, resultCode is always RESULT_CANCELED and the intent is null, so no results! :(

    I call the CaptureActivity (on my manifest) with:

    Intent intent = new Intent(“com.myapp.SCAN”);
    intent.setPackage(“com.myapp”);
    intent.putExtra(“SCAN_MODE”, “PRODUCT_MODE”);
    intent.putExtra(“SCAN_WIDTH”, 800);
    intent.putExtra(“SCAN_HEIGHT”, 200);
    startActivityForResult(intent, 0);

    In my Manifest I added the CaptureActivity like this:

    This is how I’m trying to read the results:

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    Log.d(LOG_TAG, “onActivityResult was reached!”);
    if (requestCode == 0) {
    if (resultCode == RESULT_OK) {
    // Successful scan
    Log.d(LOG_TAG, “Barcode Scanner successful.”); } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    Log.d(LOG_TAG, “Barcode Scanner canceled.”);
    }
    }
    }

    I’ll appreciate a lot if you give me some light about this guys!

    Thanks in advance!

  29. Marco Says:

    I all, it works perfectly thanks!
    I also had to add the core.jar to my project (not just to the ZXing project). Hope this will save some time to somebody!

    Thanks again :)

  30. jaav Says:

    Hi Damian,
    This is a great post, it helped me a lot in getting a scanning app up and running.
    Thank you.
    Grtz

  31. Markosys Says:

    Many thanks for pulling this together. It’s a great jumpstart into integrating the scanning activity into my app.

    I also needed to add the core.jar (see @marco) to my new project to eliminate errors resulting from adding my Zxing project library. Does this mean that something in my build process or path could be incorrect?

    For those attempting to run on 2.2SDK / API 8:

    Building on Android SDK 2.2 / API 8 required modification of the AndroidManifest.xml file, removing:
    ‘android:xlargeScreens=”true” ‘ from .

    The issue was discussed on the Zxing project site (read to the bottom): http://code.google.com/p/zxing/issues/detail?id=864.

    Thanks,
    Mark

  32. Markosys Says:

    Regarding comment from markosys:
    ‘android:xlargeScreens=”true” ‘ from .
    should read
    ‘android:xlargeScreens=”true” ‘ from .

    regrets for the typo/omission.

  33. Ajeet Says:

    I did everything (get the source, compile with Ant, get the core.jar to my project and include the zxing project as an Android Library on my App) Got the scanner to work (even without asking me what App to use) and after scanning, it returns to my App…Help needed ..whats going wrong ..can you tell me.

  34. AndroidDev Says:

    I did everything as you have shown, when I call the barcode intent, the scanning screen appears but instead of the camera screen it appears white background in the scanning box(which has a red line), but it scans the barcode when i point my phone to a code.

  35. J.d. Ballard Says:

    Sorry, but this is not “Native”, this is using intents. Natively would be using the core directly in your code.


    • Well if you followed the article then you just compiled the src and linked it to your project; so you can either hook into your own view or call the scan function via an intent.
      The term native in this context is used to illustrate that you don’t need to install the barcode scanning app separately.

  36. David Says:

    It still requires the barcode app, isnt there a way to fully integrate the code without having to rely on the user to install the Barcode scanner app?


    • Um, it doesn’t require you to install the app separately. That’s the whole point. You must be doing something wrong.

      • David Says:

        That is very strange, there are two “errors” I get, one just loops me back when I press the button, it sends me back to the classical layout, and sometimes it crashes because apparently “No activity found to handle intent”, and when I install the scanner it suddenly works. Do you have a .zip maybe of the project? I really dont know what Ive been doing wrong..I followed the tutorial to the letter (also tried bringing in the source into the project but I still get that “no activity found to handle intent”.

        thanks

  37. Shane Says:

    Thank you so much for this awesome post! I have searched for hours for the answers. After reading through your post, I realize that I was only hitting pieces of steps previously, but now the whole picture makes sense. Thanks again!

  38. V Says:

    Great article!
    Thanks from Mexico!

    For those having problems…remember that, when you create your Android project, you must specify a platform version – according to this version some AndroidManifest.xml tags will change. So, until you fix your AndroidManifest.xml you will see a lot of errors about the R class.

  39. bidu Says:

    Hey Damian nice tutorial, but i guess i have a problem i follow everything that you order nevertheless i have a problem in the last step that says: “EDIT: You need to do yet one more thing! You need to add the core.jar (produced in Step two) to your new project (Right-click your project –> Properties –> Java Build Path –> Add JARS… –> Select core.jar –> OK). Thanks to Marco and Markosys in the comments for spotting and pointing out the omission! ”

    but i cant find a way to find the core what i just see are the projects, and the project that i import the core.jar dont have this file to select.

    Here is a printscreen maybe this help you to understand what im trying to say ;)

    http://img703.imageshack.us/img703/7075/errorpus.png

    Sorry for poor english but im brazillian and my english isnt too good.

  40. kshitij vyas Says:

    Hi…thnx up till now…but m stuck in different kind of problem…actually m following all the steps..But when i build Core folder and look into it firstl i didnot find core.java.then is run command again.this time there was core.java but there is no manifest file.So,eclipe is not allowing to built project without manifest.But should i do?

  41. kshitij vyas Says:

    m sorry for the poor english.It was actually core.jar..not core.java.

  42. Sean Owen Says:

    Hi all, I’m one of the developers of Barcode Scanner. This is no doubt helpful, and the spirit is in the right place.

    However I must say we discourage people from copying-and-pasting Barcode Scanner in this way. You’re encouraging people to create apps that make people think they’re using our app, when they’re not.

    I gets worse when people do this, then modify the code in ways that break it. Guess who gets the e-mail for support? We also see plenty of people just do this to add ads or affiliate links.

    The motivation seems to just be “because I don’t want to make people install someone else’s app”, which is a flimsy reason. It may increase profits for the app maker, but to the extent it does, it subtracts a little from the open-source community. It also harms the user experience IMHO; the user is not getting fixes and improvements of the real Barcode Scanner app this way. Finally, you can see how much programmer time it’s wasting to try to get the integration working, when Intents takes 5 minutes.

    I won’t suggest you should remove this post of course; I would request that you consider the downsides of this approach, the negative externalities on the project, and perhaps emphasize that in the text.


    • Thanks for your comment Sean, I completely agree with everything you outlined. I’ve updated the text at the top of the article; hopefully to your satisfaction. I’d like to leave the article up because I think it has saved people a lot of time. However, if you feel it’s being overwhelmingly used for the wrong reasons and is harming the open-source project then I’ll take it down because that’s the last thing I want to do.

      Best regards,
      Damian

      • Sean Owen Says:

        Thanks Damian, that’s a great reply. Let’s leave it there. I’d rather people have all the info they could want, and also all the issues to consider, and this helps that goal. Thanks.

      • Iulia Ion Says:

        Then why not have the BarcodeScanner app installed by default on your phone, with the Android OS distribution? If it is not used by other apps, and it is not there by default, I do not see a reason why we should ask users to install “yet another app”. This is really poor user experience, and it will just make people walk away from such apps.

      • Sean Owen Says:

        Why not have it baked into Android itself? Well, I’m afraid I’m not that decision-maker! What do you mean? (As it happens — this was developed at Google and was almost a part of Android 1.0, but wasn’t, and probably should not be. But, this could easily be an app that’s nothing to do with Google.)

        It *is* used by other apps. It is preinstalled on some phones, but not most. Again — hardly something the app controls.

        I don’t think it’s such a big deal to install another app. I don’t argue with people who think it’s a bad experience. I’ve open sourced all this code to help you build it into your app. I’m merely emphasizing the “you build” and “your app” — I’m speaking out against pure copy-and-paste jobs out there which have little added value.

        Sure, it’s easier for them. Sure integrating makes the “experience better”. It may make them more money. These are still no justification for directly ripping off someone’s work.

      • Iulia Ion Says:

        Sean, I understand your point, am very grateful for very good library you implemented and by no means do I want to rip it off. I am, however, going to say that to the non-technical user it makes a great difference if he is asked to install another app or not. Apart from that, there are other reasons why I need to integrate the ZXing library directly in my app. I want to use ZXing to transfer sensitive bits of data, such as public/private keys, which I really do not want to leave my app (especially not to an app that has Internet permissions and can be updated remotely). Also, for the sake of good user experience, I would like to change the screen that displays the QR code to give the user customized instructions, instead of displaying the content of the barcode (which is all a big junk of gibberish that makes no sense to the user).

        That being said, one think I am totally missing out on by basically ripping your code apart, is the great updates you will be releasing to improve the performance of the decoder. For this purpose, do you think you could release a distribution that would be easy to incorporate in an app (for example a jar file with the library for Android).

        Thank you very much. I really appreciate your work and your help.

        Best,
        Iulia


    • Hi Sean!

      Indeed, your words are very true.

      I have just convinced my boss to let users download Zxing, Barcode Scanner; and through the Android Intent Integrator, the whole picture looks much more stable and trustful.

      I hope other CEO’s are able to be convinced.
      :)

    • Richard Says:

      Hi Sean, thanks for the great code but I found this page when trying to find how to integrate zxing with my app. You have said that using Intents takes 5 minutes to integrate but I can’t find a clear explanation of how to do this.
      It would be helpful to me (and I’m sure others) if you could provide a link to the ‘right’ way to integrate using intents.
      Thank you


  43. [...] /android project as a library in your own project. Damian Flannery has put together a very nice step-by-step blog post for doing [...]

  44. Iulia Ion Says:

    Thank you very much for the explanation on how to get this done. This was very useful. Especially since the process is so counter-intuitive. One thing I totally disagree with you on: It is a really BIG deal asking users to install another application just so that they can install yours. People are lazy, things are too complicated, and they cannot spend their time installing apps, so easily integrating the barcode scanner into your app, is totally worth it.


  45. Hey Damian,

    I actually set up the same project structure independently of your post and I’m seeing an odd issue when I install my application on my phone. I end up getting two icons, one launches my app and the other launches the original barcode scanner. Have you ever seen anything like that in your experience?

    Thanks…

    Simon

    • Sean Owen Says:

      It means you haven’t taken out our CaptureActivity from your copy of the manifest. You should probably start with your own manifest. If you copy ours (aside from issues here) you’re going to get settings and such that don’t apply to you.

  46. Anthony B Says:

    Hmm well I seem to have got it working… but for some reason the mask outside of the border is red rather than just a transparent black (making it look like half of the screen is just red). Any idea why this might be happening? Thanks.

  47. Bob Says:

    Thanks for the great instructions. However, I am having an issue using ZXing as a library. Every time I attach it in Eclipse it looks fine (little green tick). However, when I then check that it is still included, it has changed to a red cross and the Project name is simply listed as a question mark.

    I have tried many different API levels, rebuilding etc, but nothing seems to work.

    Any suggestions about how to fix this would be much appreciated.

    • Bob Says:

      I managed to get past this problem by deleting the minimum SDK levels listed in the ZXing manifest.

      Now I get a fatal error running it which states:
      android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }

      I can remove this by separately installing Barcode Scanner, but as soon as I uninstall it, the error returns. Obviously it isn’t actually loading the ZXing or Core jar…. even though both are listed properly under ‘Library Projects’ and ‘Referenced Libraries’.

      Any ideas would be welcome because this is driving me nuts now! Thanks.

      • Jakob Says:

        Had the same problem. My problem was in my manifest. Where I forgot to put the inside the tags
        From there, it worked like a charm.

        Else ensure that you have copied it, excatly as in the post.

        Try renameing it to your own:
        something.someting.android.SCAN (in Activity)
        and similar in the Manifest
        and in the class Intents.java in ZXing project


      • Jakob, I’m having this problem as well. Can you explain your comment about forgetting to put something inside the tags?

  48. monta Says:

    hi this is wonderful ,
    but i want to impliment in my application a sample scan code bar.
    that is to say, win i want i scan a code a bar, it will store in imageview (same application fidme)
    please can you help me.

  49. Jakob Says:

    Everything works! Except I have problems when I access the menu in Barcode Scanner. Menu shows, but when I select a menu item (Help) I get:

    java.lang.NoSuchFieldError: com.google.zxing.client.android.R$id.help_contents

    I also have problems when clicking on preferences (but found a work-around for that)

    Help

  50. Prashant wankhede Says:

    I have problem with creating the core.jar ,plase he;p me.thank in advance

  51. coderealm Says:

    Dear damian flannery,
    Please help me with the basics. I installed ant. My zxing is in c:\zxing. When I open command prompt on windows 7,
    I type in:

    >C:\zxing
    >’C:\zxing is not recognized as internal or external command,operable program or batch file.

    So I don’t even get past the second step. can you please help me with the command line statements


  52. i am facing an error java.lang.VerifyError in while starting CaptureActivity

  53. sdcool Says:

    I’ll try this out. Thanks a lot for this valuable write up.
    I have 2 queries:
    1. Do I have to write the camera code for Zxing to be used? Or will it automatically invoke the camera functionality?
    2. If I want the scanned result to be from my sqlite db how can I achieve it?


  54. Can you please give the core.jar, I couldn’t able to make it .. Thnaks


  55. Note that ADT v.14 no longer allows resource fields (R. etc.) to be constants in library projects. This means they can’t be used in case statements. So once you clean up the manifest (xlargeScreen item noted above) you will have to go through and find the 3-4 case statements (highlighted by errors) and convert them. The fix suggestion in the gutter of the code indicated how to use Eclipse’s QuickFix feature to auto-convert these Switch statements to if-else.

  56. Satyaseshu Says:

    Hi, Thanks for nice tutorial. I implemented this Zxing code scanner in my application. everything working fine. but i got extra icons i.e, 3 icons in left top, right top and bottom right. what’s these icons? is there any help and i would like keep my own button i.e., close button. if i clicked on close button the current activity will close and goes to previous activity. can any one tell how to achieve this problem?? this is my mail is s.seshu143attherategmaildotcom..
    Thanks in advance………………

  57. Justin Says:

    I’ve modified the ZXing source code to make encoding/decoding a bit easier. The project is located at:
    http://code.google.com/p/android-quick-response-code/

    The new source allows direct integration of encoding and decoding without having to install ZXing or use Intents.

    • Sean Owen Says:

      I think this is overall a positive step — solving several of the issues that I perceive that rampant copying-and-pasting of Barcode Scanner has caused. Justin I will follow up with you directly with one small request.

  58. quangho Says:

    i do step by step but it do not work, an excception was found in logcat: unable to start com.google.zxing.client.android.CaptureActivity
    please hepl me !!!

  59. Asep rohimat Says:

    Hi all, i tried with this procedure and always get error. finally i found tutorial with call zxing via intent with barcode scanner app must install in phone. i can call barcode intent but my phone always get error after scan the barcode, what procedure or what kind off setup must i do. this is my code :

    public class zxing_act extends Activity {
    /** Called when the activity is first created. */
    private EditText edittext1, editText2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText edittext1 = (EditText) findViewById(R.id.edittext1);
    EditText editText2 = (EditText) findViewById(R.id.editText2);

    Button next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new View.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”);
    edittext1.setText(contents);
    String format = intent.getStringExtra(“SCAN_RESULT_FORMAT”);
    editText2.setText(format);

    } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    }
    }
    }

    sory my english is very poor.

  60. syed Says:

    Can some one upload the core.jar file pls.

  61. preethan Says:

    very useful , covered evry detail thanku

  62. preethan Says:

    One important point , the core file has to be added to library of the new project as well.

  63. crv Says:

    I am able to launch ZXing App from my native App,thats fine,when I launch,it is able to scan but not Showing any details of it like Url,name or address..Plz suggest me with some solutions

  64. maliks Says:

    please some one help me out via this … i tried each and every thing to make my app run but it shows lot’s of error’s … someone plz help me out … plz drop email at sudhirmalik2011@gmail.com if you can solve my prob … i need to scan qr code with my app only

  65. Tony John... Says:

    Thanks a lot Damian, it really helped.. had to jump through a few hoops.. one thing I would like to mention is that.. the ZXing project and the project that includes it as a library should be in the same drive and accessible via logical path ..

    Sean Owen.. I understand your argument but one of the challenges I see while making an application is that users would frown on the need to install another application to make a main application work.. for people who have a scanner installed well and good.. but others would not freely go ahead and install it. I sure do understand the cost component and also understand the work that you and your team have put it.. it would be awesome if there was some standard way to integrate ZXing directly without having BarcodeScanner installed as an application which would help you and also adhere to the means you expected the product to be used..

    FYI I am not a Android JAVA guru.. I understand and have worked in many programming languages and starting to wade into the Android Space..

  66. Jakob Says:

    I have a major problem when using the integration on 3.0 and 3.1 (But not 3.2)
    In 3.0 and 3.1 the search rectangle is moved to the lover left corner and scanning is really bad! I found out that the barcode still gets recognized if it is in the center of the viewfinderview. (But the user experience is really bad)

    Have any one else experienced this problem? Help!

  67. Nikhil Says:

    HI All,

    I followed all the steps as mentioned but I am getting the following exception at run time

    01-11 11:21:48.411: E/AndroidRuntime(18807): FATAL EXCEPTION: main
    01-11 11:21:48.411: E/AndroidRuntime(18807): java.lang.ExceptionInInitializerError
    01-11 11:21:48.411: E/AndroidRuntime(18807): at java.lang.Class.newInstanceImpl(Native Method)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at java.lang.Class.newInstance(Class.java:1429)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.os.Handler.dispatchMessage(Handler.java:99)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.os.Looper.loop(Looper.java:123)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at java.lang.reflect.Method.invokeNative(Native Method)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at java.lang.reflect.Method.invoke(Method.java:521)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    01-11 11:21:48.411: E/AndroidRuntime(18807): at dalvik.system.NativeStart.main(Native Method)
    01-11 11:21:48.411: E/AndroidRuntime(18807): Caused by: java.lang.NoClassDefFoundError: com.google.zxing.ResultMetadataType
    01-11 11:21:48.411: E/AndroidRuntime(18807): at com.google.zxing.client.android.CaptureActivity.(CaptureActivity.java:106)
    01-11 11:21:48.411: E/AndroidRuntime(18807): … 15 more

    Can any please help me I am really new to this and android as well..

    Nikhil

    • Ajay Says:

      Hey Nikhil,
      I am having the same problem. Can you help me out if you figured out whats wrong??

      • Jan Says:

        Happened to me while using eclipse.
        I fixed this problem by compiling the core.jar library using ant and including it into the client project. Setting up project dependancy does not work.

  68. Cady Says:

    I am having issues with these steps. I cannot even get past step 2. what file do I actually download? There are three options…how do I install it?

  69. ghouse Says:

    hi thanks for the post i have one doubt what if want to use this library in my app i have to pay money for them

  70. Ali Says:

    I am novice here please help me, I have done all steps and code compiles well but when I press a button to start the SCAN activity nothing happens, my current activity just reloads (showing me the same button). the code defines intent and starts activity but
    it never goes to “onActivityResult” method. Does anyone have a solution for this?


  71. [...] I completely agree with this. There are many advantages to using the intent based approach as outlined in his comments. My motivation is for an enterprise app that does not have access to the Android market and involves my client installing zxing manually on thousands of devices before they are able to distribute to its business customers. Integrate zxing barcode scanner into your Android app natively using Eclipse « Damian Flannery's Bl… [...]

  72. lindo Says:

    If you want to achieve TRUE NATIVE integration of zxing (i.e. not using Intents) then you need only import the core.jar file as a Library into your project and then start using it. To cover a LOT of ground faster you can bring the ZXing App’s code (i.e. zxing/android/src/) but you will need to copy some of the resources from ZXing App’s res/ to your own project’s /res. Then you can go about cutting out the crap you don’t need such as, in my case: Book reader, Menus, History Manager, Google Shopping, Intent handling, most of the *ResultHandlers (AddressBook, Email, Geo, ISBN, Calendar, SMS, Tel, Wifi, perhaps more). And then go on to customise where you need.

    • Sean Owen Says:

      Please don’t encourage people to copy android/. We have enough problems with total copy and paste jobs already. This is officially quite discouraged.

      • lindo Says:

        Is it not well written code? Is it not open-sourced under the Apache License 2.0? Would you have me do something else with the code, other than use it as a baseline to enhance, to somehow fulfil my needs? I don’t understand your angle on this, other than the potential to profit via sales of your paid App (which is unreasonable, given the code is open-sourced). Talk about subtracting from the open-source community ;)

      • Sean Owen Says:

        This has been covered many times. How would Firefox devs feel if you pasted their code into your app and called it “LindoBrowser”? Not illegal, but does not seem ethical.

        I’m sure you’re one of the good guys, and not one of the many people out there who publish clones on Market, so that they can add their brand name, or add money-making ads and affiliate links, or worse. That’s really what I’m speaking out about.

        I do think it subtracts from the community when you tell people to not use the existing app, and instead copy and paste the code so that you don’t drive any users to the existing app. That’s what you are helping people do here, but I think reasonable people can disagree about whether that’s so bad. To be sure, a lot of people are doing this from companies who want to copy and paste into their app to increase their own profits.

        I’d also ask you to ask yourself who you think gets the support e-mail when something that looks a load like Barcode Scanner, but isn’t, has an error? Again: coming out of the community’s “pocket”, which is mostly mine.

        If you are encouraging innovation based on this code, I’m all for that. But I doubt that starts with copying all the code. I’d sure like to see someone build substantially their own, better app.

        I can’t understand your point about profit. I don’t sell an SDK. Maybe you don’t know that I am the primary developer? I have given four years of time to the open source project, for free. App revenue pays for even more work on the project now, but I still ‘lose money’. Have a look at what I put in here: http://code.google.com/p/zxing/source/list

        If I wanted more money, I’d do contract work or something and not spend any more time on the project. Now, what exactly have you given to the people here?

  73. lindo Says:

    I guess, like you, I’ve provided a solution of sorts and now I’m here, like you, rambling on :)

    You make gross misjudments about what I’ve done, what I’m doing, and why I’m doing it. Understandable, since you don’t know. But it’d be a shame if you act this way towards everyone that seeks to utilise the core natively.

    If I can provide some constructive feedback that may alleviate the need to base off /android/ App’s code: the project lacks documentation and examples (apart from the /android/ App) as to how to utilise the core at all, let alone best practices. Hence why I ended up at this thread, only to find its just using the Intent method (the method described in this thread is more ‘bundled’ than ‘native’).

    Bait: Is Indian Giving “ethical”? Is complaining about not making a profit from your contribution to open-source work a very strange thing to do?

    Your time and effort is appreciated, at least, by me.

    • Sean Owen Says:

      I don’t know what you’re doing and don’t judge it. I bet you are a force for good. I just said I don’t want to encourage people to go this road; this advice can be used for good, but judging from questions on StackOverflow and the mailing list, 80+% of people just want a quick, free solution for their own company, and don’t want to give back or have any connection to the open source project. So most consumers of this advice are just being helped to do something I don’t support, so I think it’s OK to say I don’t want it supported.

      android/ is not an API or library and so is not documented to be used like that. That is why there aren’t “examples”. It’s not meant to be obtuse, and I think it’s clean and reasonably documented. More docs are welcome.

      Where I am complaining about not profiting? I was just defending against the implication that I am motivated by money now, by pointing out that I hardly profit.

      We were paid handsomely by Google for the initial work. After Google, I continued to support it gratis, with what little time I could sparse. The paid app is an effort to justify sparing a bit more time, since there is much that could be done; improvements are back-ported regularly, not kept to myself. (Really: I was interested to see how a paid app / open source commercialization works on a small scale.)

      Listen if you want to get indignant about somebody here, I’d point you to Big In Japan / ShopSavvy. It’s a lovely app and service. But they sell an “SDK” which is mostly zxing inside. They modified the scanner to send back parts of everything you scan to their servers. They track your location and are most certainly out for profit. They had to be told several times to get the open source licensing right. They have never contributed anything to the project. The most ‘thanks’ the project got were bizarre legal threats (http://osdir.com/ml/zxing/2010-10/msg00196.html) which didn’t stop until Google legal got involved.

      Really. I am not the guy you want to come after.

      • lindo Says:

        Don’t worry, your code is in good hands (in this case!).

        Really stinks what Big In japan / ShopSavvy did, vultures :/ All the more reason to outdo them via open-source, eh?

  74. Piotr Says:

    Friends… what could be wrong when I get errors like this

    Description Resource Path Location Type
    The method call() of type KillerCallable must override a superclass method KillerCallable.java /ZXing/src/com/google/zxing/client/android/result/supplement line 38 Java Problem
    ?

  75. Sri Says:

    I have followed the exact steps and I get a lot of errors but they all have the same concept which is R cannot be resolved to a variable.

    How could I fix this, any help would be appreciated

    Thanks in Advance

  76. Vyacheslav Says:

    thanks man! You’ve just saved my life!

  77. Alex Says:

    Please, I trying and don’t have errors, but the app doesn’t works, it just stay the same, can someone, please send me a example project sucessfull working ? A simple project with a button calling the zxing, PLEASE !!!!

    Thanks

    • malik Says:

      i have done all the above things and made my own project but when i run my app on phone and scans the QR code it only says “Found URL” and does not redirects me to that URL and takes me back to same page to scan qr code again and again same happens, how can i solve this problem ?

  78. Conor Caslin Says:

    Thanks for posting this. It was really helpful. But I’m having a problem when the QR Scanner screen with the red laser line is launched. The scanner appears in the right corner and not in the middle of the screen.any help is appreciated. thanks in advance

  79. kamil Says:

    Hii i was getting error wen i created apk file and tried to install that can u give me code where have successfully done….
    can u just upload ur that project for me
    Thanks in advance..

  80. dada Says:

    hi i have a doubt if i want to display the qrcode image in the same layout where i have entered the string then how can i achieve that one any suggestions will be great help

  81. Disya Says:

    Thanks.. man it worked for me :D

  82. dwking Says:

    Can’t build the zxing successfullly… somebody tip me off..
    When I add the import i get the error “manifest file missing” … should be pretty easy to fix..but… :(
    help..please.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 30 other followers