26

I am trying to Integrate ZXing QR Code into my android app without installing BarCode Scanner app, I have followed the steps as:

1) Firstly I have downloaded ZXing.zip file and extract it

2)open the ZXing project as an android existing project and then go to android folder and open the android folder and also include core.jar file into the ZXing project named CaptureActivity.

3)I have used the CaptureActivity project as a library in my project named 'QRCodeSample'. (Problem in including CaptureActivity as a library)

4)My code is as below3:

   public class QRCodeSampleActivity extends Activity {
Button b1;
static String contents;

public static final int REQUEST_CODE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            /*Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
                    "QR_CODE_MODE");
            startActivityForResult(intent, 0);*/
            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) {
            contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Log.i("Barcode Result", contents);
            Intent i1 = new Intent(QRCodeSampleActivity.this, webclass.class);
            startActivity(i1);
            // Handle successful scan
        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
            Log.i("Barcode Result","Result canceled");
        }
    }
}

 }

The manifest file is :

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

     <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>
    <activity
        android:label="@string/app_name"
        android:name=".QRCodeSampleActivity" >
       <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>


    <activity  android:name=".webclass"></activity>

</application>

</manifest>

and its not adding library also

LibraryInformation

When I am trying to run my project,the error msg is:

Unable to instantiate activity ComponentInfo{com.qr.code/com.qr.code}: java.lang.ClassNotFoundException: com.qr.code in loader dalvik.system.PathClassLoader[/data/app/com.qr.code-1.apk]

6
  • To include ZXing is possible I made it. I think you use some other classes. I cannot remember that zxing uses the namespace com.qr.code
    – rekire
    Dec 1, 2011 at 11:57
  • Why are you using library project. You can build the downloaded zxing library to a jar file using ant and include that jar file into your project. Here is the link for the instructions code.google.com/p/zxing/wiki/GettingStarted
    – blessanm86
    Dec 1, 2011 at 11:59
  • Are you sure that this works for Android too? I made my own lib with some custom extensions.
    – rekire
    Dec 1, 2011 at 12:08
  • @blessenm: When I had extracted the ZXing file, then I converted the core folder into it as core.jar file and add it to my project
    – Kanika
    Dec 1, 2011 at 13:03
  • you have put the project and the library at the same disk partition Jun 8, 2014 at 0:13

7 Answers 7

23

Finally I got the answer,

As of ADT 14,the resource fields(such as R.id.decode) are no longer constants when defined in library projects

So in the ZXing library->android->com.google.zxing.client.android.CaptureActivityHandler.java and DecodeHandler.java

Replace both of these classes switch case statements with if-else,and then import this ZXing library into your project..

Rest of the coding of my own project is same...just the problem with the library classes as these are not updated as according to ADT 14..

Kanika

2
  • 21
    Please do not integrate our code as a library project. Please write your own scanner app and reuse pieces if you need to. We did not open source it for you to clone this way.
    – Sean Owen
    Dec 2, 2011 at 11:43
  • I made everything as featured but still got that exception. Why I cant reach those classes? I added it as a lib and also added core.jar as external jar. AndroidRuntime(6316): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sonqr/com.google.zxing.client.android.CaptureActivity}: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity May 5, 2012 at 16:01
17

For all those Android Studio/Gradle users out there

Okay guys, as my task today was to integrate ZXING into an Android application and there were no good sources for input all over, I will give you a hint what made my be successful - cause it turned out to be very easy (on version 2.*).

There is a real handy git repository that provides the zxing android library project as an AAR archive.

All you have to do is add this to your build.gradle

repositories {
    mavenCentral()

    maven {
        url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
    }
}

dependencies {
    compile 'com.google.zxing:core:2.2'
    compile 'com.embarkmobile:zxing-android-minimal:1.2.1@aar'
}

and Gradle does all the magic to compile the code and makes it accessible in your app.

To start the Scanner afterwards, use this class/method:

IntentIntegrator.initiateScan(this);    // `this` is the current Activity

If you already visited the link you gonna see that i just copy&pasted the code from there the git readme. If not go there to get some more insight and code examples!

Hope to be helpful for future readers. Peace :)

4
  • This looks to me like it's only really useful for barcodes. I need to use it QR codes. Am I wrong? Feb 8, 2015 at 20:39
  • Hi, I downloaded sources from the link You provided. Is it possible to compile it to jar file and then import it to my android studio project? If yes, can You provide some link/instructions on how to build it? I would be most happy. Thanks in advance.
    – greenskin
    Mar 24, 2015 at 21:30
  • Hey greenskin, at the github page of xzing there is some more input (first section) github.com/zxing/zxing/wiki/Getting-Started-Developing Mar 25, 2015 at 14:41
  • @LangustenGustel I have one question. I integrated zxing like you said. I need to customize the camera preview? As in add a button to turn on/off the flashlight and a toolbar at the top? Is it possible to do this when you start it via an Intent?
    – Isuru
    Feb 18, 2016 at 20:29
3

I had the same Problem and after hours struggling with it I finally managed to solve it like this. as Rubiraj is pointing

*Right Click on your project > Properties > Android > (under tab library) Add the Zxing project *

Assuming you were able to correctly create Zxing project as a Library as it is explained here : http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/

3

You need to follow step as given by the link

http://www.androidaz.com/development/zxing-qr-reader-direct-integration

you can download core.jar from

http://repo1.maven.org/maven2/com/google/zxing/core/2.2/

The above is working for me, if your program still just put the core-2.2.jar in libs and clean your project

2
  • is this a standard fix? You have posted the same answer to several questions Sep 28, 2013 at 11:41
  • It long time I have posted this but see this link if it help androiddevelopersolutions.com/2012/06/… Let me know so that I will update my comment
    – Piyush
    Oct 2, 2015 at 18:35
2

MaterialBarcodeScanner: Easy to use barcode reader for your Android Project (Uses Google Mobile Vision API).

  1. Provide gradle dependency

    compile 'com.edwardvanraak:MaterialBarcodeScanner:0.0.6-ALPHA'

  2. Build a MaterialBarcodeScanner

    private void startScan() {
        /**
         * Build a new MaterialBarcodeScanner
         */
        final MaterialBarcodeScanner mBarcodeScanner 
                 = new MaterialBarcodeScannerBuilder()
                .withActivity(MainActivity.this)
                .withEnableAutoFocus(true)
                .withBleepEnabled(true)
                .withBackfacingCamera()
                .withText("Scanning...")
                .withResultListener(new MaterialBarcodeScanner.OnResultListener() {
                    @Override
                    public void onResult(Barcode barcode) {
                        barcodeResult = barcode;
                        result.setText(barcode.rawValue);
                    }
                })
                .build();
        mBarcodeScanner.startScan();
    }
    
  3. Hook it up to a button

    fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
     startScan();
    }
    });
    
  4. Start scanning!

1
  • How can I use this library for continuous scanning (e.g.: without having to touch the button)?
    – LRMAAX
    May 5, 2017 at 14:06
1

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
0
  1. After importing Zxing as existing project, Properties > Java Buildpath > Check "is library" (check button) and then try to add Zxing as library.

  2. Make sure webclass.class exist in your QRcodesampleActivity.java

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.