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.
Anybody facing the same issues, follow the simple steps:
Import the project android
from downloaded zxing-master
zip file using option Import project (Eclipse ADT, Gradle, etc.)
and add the dollowing 2 lines of codes in your app level build.gradle
file and and you are ready to run.
So simple, yahh...
dependencies {
// https://mvnrepository.com/artifact/com.google.zxing/core
compile group: 'com.google.zxing', name: 'core', version: '3.2.1'
// https://mvnrepository.com/artifact/com.google.zxing/android-core
compile group: 'com.google.zxing', name: 'android-core', version: '3.2.0'
}
You can always find latest version core
and android core
from below links:
https://mvnrepository.com/artifact/com.google.zxing/core/3.2.1 https://mvnrepository.com/artifact/com.google.zxing/android-core/3.2.0
UPDATE (29.05.2019)
Add these dependencies instead:
dependencies {
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.google.zxing:android-core:3.3.0'
}