Integrating the Zxing Library Directly into My Android Application

Integrating the ZXing library directly into my Android application

UPDATE! - SOLVED + GUIDE

I've managed to figure it out :) And down below you can read step-by-step guide so it hopefully can help others with the same problem as I had ;)

  1. Install Apache Ant - (See this YouTube video for config help)
  2. Download the ZXing source from ZXing homepage and extract it
  3. With the use of Windows Commandline (Run->CMD) navigate to the root directory of the downloaded zxing src.
  4. In the commandline window - Type ant -f core/build.xml press enter and let Apache work it's magic [having issues?]
  5. Enter Eclipse -> new Android Project, based on the android folder in the directory you just extracted
  6. Right-click project folder -> Properties -> Java Build Path -> Library -> Add External JARs...
  7. Navigate to the newly extracted folder and open the core directory and select core.jar ... hit enter!

Now you just have to correct a few errors in the translations and the AndroidManifest.xml file :) Now you can happily compile, and you will now have a working standalone barcode scanner app, based on the ZXing source ;)

Happy coding guys - I hope it can help others :)

Integration ZXing library into my Android application

Go to folder which is root folder for zxing project ( may be "zxing" is the name of root folder) from cmd prompt.

Make sure you have ant installed.(Check this)

run cmd :

ant -f core/build.xml

If error related to tools.jar comes, copy tools.jar to desired location.

Integrating ZXing in my application

you should launch scan like this:

@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
//in case you want to customize a bit.
integrator.setPrompt("Scan a QR/Bar code");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.initiateScan();
}

Receive results like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String data = scanResult.getContents();
// use this data
} else {
// error
}
break;
}
}
}

Edit 1:

Add this to build.gradle of your app as dependencies:

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'

Integrate ZXing in Android Studio

I was integrating ZXING into an Android application and there were no good sources for the input all over, I will give you a hint on what worked for me - because it turned out to be very easy.

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

  • https://github.com/journeyapps/zxing-android-embedded

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

repositories {
jcenter()
}

dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.2.0'
}

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:
From the Activity:

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

From a Fragment:

IntentIntegrator.forFragment(this).initiateScan(); // `this` is the current Fragment
// If you're using the support library, use IntentIntegrator.forSupportFragment(this) instead.

There are several customizing options:

IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan a barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();

They have a sample-project and are providing several integration examples:

  • AnyOrientationCaptureActivity
  • ContinuousCaptureActivity
  • CustomScannerActivity
  • ToolbarCaptureActivity

If you already visited the link you going to see that I just copy&pasted the code from the git README. If not, go there to get some more insight and code examples.

How to integrate ZXing Library to Android Studio for Barcode Scanning?

You need add the following to your build.gradle file:

repositories {
mavenCentral()

maven {
url "http://dl.bintray.com/journeyapps/maven"
}
}

dependencies {
// Supports Android 4.0.3 and later (API level 15)
compile 'com.journeyapps:zxing-android-embedded:2.0.1@aar'

// Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.
// If you only plan on supporting Android 4.0.3 and up, you don't need to include this.
compile 'com.journeyapps:zxing-android-legacy:2.0.1@aar'

// Convenience library to launch the scanning and encoding Activities.
// It automatically picks the best scanning library from the above two, depending on the
// Android version and what is available.
compile 'com.journeyapps:zxing-android-integration:2.0.1@aar'

// Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.
// This mostly affects encoding, but you should test if you plan to support these versions.
// Older versions e.g. 2.2 may also work if you need support for older Android versions.
compile 'com.google.zxing:core:3.0.1'
}

My build.gradle file like this:

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.myapplication2"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

repositories {
mavenCentral()

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

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// Supports Android 4.0.3 and later (API level 15)
compile 'com.embarkmobile:zxing-android-minimal:2.0.0@aar'

// Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.
// If you only plan on supporting Android 4.0.3 and up, you don't need to include this.
compile 'com.embarkmobile:zxing-android-legacy:2.0.0@aar'

// Convenience library to launch the scanning and encoding Activities.
// It automatically picks the best scanning library from the above two, depending on the
// Android version and what is available.
compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'

// Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.
// This mostly affects encoding, but you should test if you plan to support these versions.
// Older versions e.g. 2.2 may also work if you need support for older Android versions.
compile 'com.google.zxing:core:3.0.1'
}

The code is here.

Also, for how to use it, please refer my answer on the Stackoverflow here

It contains method and also simple code that I have tested.

how to import ZXING library in android application?

http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/

Thats a step by step guide to natively integrating. It integrates the zxing project into yours as a backup of sorts. You will send out an intent request, but your application is registered as a receiver of that request. If the user doesn't have a different scanner, yours will be the only option. If they have other scanners it will allow the user to choose. If you want your app to always be chosen without another option, the comments in the tutorial detail how to change the intent filter to do it.

Keep in mind this is the quick and dirty way to do it. As others have mentioned, ZXing provides a great tutorial on how to send a barcode intent out and point them to the market if they don't have a scanner.



Related Topics



Leave a reply



Submit