zXing into Android for Barcode reading functionality (original project 100Mb, imported 3Mb)

The zXing documentation is rather complex. A simple task of integrating the zXing library can be painful. Here I am trying to document the steps needed for including zXing as part of your android application.

We have 2 choices. Either to include all the source as part of the android application project itself, or create a new project and import relevant files into this new project. Obviously if you create a new project we need to add the zxing project as a dependent project to your android app project.

This is what I did and things worked for me. I was able to only include 3Mb worth files, instead of the entire 105 Mb.
– checkout the sources from (using svn) http://code.google.com/p/zxing/source/checkout
– Use svn export on the checkedout project, so that you dont get the .svn folders (if u miss this step, you will not be able to import this project into your svn server)
– Create a new java project
– Right click on src directory and hit import
– Select only the “src” sub directory of the following zXing project directories
* core
* javase
* android
* androidtest
* android-integration
– Do not import the test directories from core/javase/android/androidtest/android-integration at first. Do the src imports, get things to work and then play around with test (the same way you did the src).

In your AndroidManifest.xml

In your button click

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

In your response listener (source of below code snippet, http://stackoverflow.com/questions/2050263/using-zxing-to-create-an-android-barcode-scanning-app)

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

Q : Getting multiple errors in the src directory of my new project. The import changed the package name to something like “android.src.com.google……”.
A1 : While you import you need to “right click” on the “src” folder of your new project. And hit import. You may have selected import on your project instead of the “src” folder.
A2 : While importing you may have selected the folder “android”, instead of “android/src”.


17 thoughts on “zXing into Android for Barcode reading functionality (original project 100Mb, imported 3Mb)

  1. hi, i tried to follow your procedure but wasnt able to get the desire results. I am using eclipse and i tried using both java projects and android projects. what could i be missing….

      • hi, I wasnt able to import the packages succefully, in the src folders they all have their names as “android.src.com.google……” which is different from what is actualling the the files. I beleive that is what is causing alot of errors.
        The procedure I followed
        1. I downloaded zxing.1.6
        2. extracted it to my system
        3. created an empty java project(later on an android project)
        4. created an src folder if absent
        5. right clicked on the src folder and imported the src folder of the 6 colders you mentioned

        Everytime the import is done there is always either core.src or android.src infront of the names of the packages. I am guessing probably its because i didnt do the 1st part ie svn, but am using windows 7 and not sure how to go about it.

      • I have tried the steps just now. Seems to work for me. I’ll try something different and report back. Send me some screen snap shots. That may help understand further.

  2. nowoxi :
    hi, i tried to follow your procedure but wasnt able to get the desire results. I am using eclipse and i tried using both java projects and android projects. what could i be missing….

    I kept on getting package name to something like “android.src.com.google……”

  3. Hello – i just stumbled upon your article, and i’m just about to start on a project similar to what you’ve described above. i really hope what you’ve described above works – and i’ll be posting in to let you know.

    I just wanted to clarify 3 things –
    1. Since the zxing code-base has activities under a package “com.google…”, did you make any changes to the package name? or are simply not using any of the zxing activities?
    2. did you re-use any of the layout XMLs to recreate a UI similar to the zxing app?
    3. in your article where you’re mentioning changes to be made, you’ve got a section “In you AndroidManifest.xml” – under this you’ve not added anything? is there something you’ve missed out on?

    • Anirvan,
      On 1. No, I created a different new project and added to my app project as a dependent.
      On 2. No, I just called the intent and waited for the response.
      On 3. Yes, I need to update the blog for this manifest file entries.

  4. Hello, I am new to Android and ZXing and I have a few questions to ask.
    1) what do you mean by using svn? I have never use it before
    2) I did what you said by creating a Java project and import the stuffs (JavaSE, android, androidtest, etc), but I got errors. All are due to the absence of R file. Should I use Android project instead of Java project?

    Thanks in advance!

  5. Hi,

    I followed the steps outlined in your tutorial. But I’m getting multiple errors in the src directory of my new project.

    I will summarize the problems:

    1) i tried using both java projects and android projects. In both case I’m facing missing dependencies.

    2) For each import, I right clicked on the src directory of my new project & selected the “src” subfolders like android/src, core/src etc.

    3) the package names are like “com.google……” but still getting error in most of the files for e.g com/google/zxing/client/android/BeepManager.java I
    ‘m getting the following error:

    R cannot be resolved to a variable

    Plese tell me where I am going wrong.

    Thanks in advance

  6. hey, i followed your tutorial and it worked,but i have to install zXing application to make it work. Is there any way in which i can add zXing application inside my application so that i can only install one app.

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 )

Google+ photo

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

Connecting to %s