logo

In this tutorial we will learn how to integrate Zxing 2.0 library into your Android app natively using Eclipse.

Step by pay for homework step tutorial given below:

1. First go to the ZXing project from here http://code.google.com/p/zxing/downloads/list, and download projects file ZXing-2.0.zip.

2. From downloaded archive take the android project “android” and jar file “core.jar” from core directory, and create new android project from it.

packages


You must include core.jar into the build path, and you should see all zxing client packages like here. 🙂

3. Then mark this project as Android Library project
(Right click on project > Properties > Android > mark “is library”)

Properties

4. Now create your new project, I call it “QR Code Scanner” here, and make sure that it is using previous library project (Right click on project > Properties > Android > Add)

Add Zxing library

5. Also include core.jar into the build path.

6. Your main activity will look like:
i.e.

package com.mobisys.android.qrscanner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class ScannerActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        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
           }
        }
     }
}

7. Also in AndroidManifest.xml, you must include following permission

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

8. Finally, you must include “CaptureActivity” in android manifest because your Android app has no

idea how to handle the scan intent.

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

That’s it, run the application and enjoy scanning QR code. 😀

AUTHOR: Vikas Hiran
6 Comments
  • Stickytroll

    There is no core.jar file in that .zip.

    May 22, 2013
    • core.jar is part of zxing library & you can get core.jar when you will download zxing library. This blog is meant for how to use zxing library..

      May 22, 2013
  • falahgs

    great …. thanks for good tutrioal

    thanks again

    December 22, 2013
  • Jyotsna

    What is the role of : String format = intent.getStringExtra(“SCAN_RESULT_FORMAT”); ?
    You have not used it anywhere.

    And your code is not scanning QR codes for me only scanning barcodes..
    Why is it so?

    May 12, 2014
  • eshu

    there is no project and jar file in this link http://code.google.com/p/zxing/downloads/list, so sand me exact link to download

    April 10, 2015
  • Swastik

    Sir there is no project file here so can you send me that file or the exact link.

    June 25, 2015

Leave a Comment

Your email address will not be published.