Using ZXing to create an Android barcode scanning app [duplicate]
I've been searching for how to add a barcode scanner to my app. Are there any examples or how can I do this easily?
The ZXing project provides a standalone barcode reader application which — via Android's intent mechanism — can be called by other applications who wish to integrate barcode scanning.
The easiest way to do this is to call the ZXing SCAN
Intent
from your application, like this:
public Button.OnClickListener mScan = new Button.OnClickListener() {
public void onClick(View v) {
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) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
Pressing the button linked to mScan
would launch directly into the ZXing barcode scanner screen (or crash if ZXing isn't installed). Once a barcode has been recognised, you'll receive the result in your Activity
, here in the contents
variable.
To avoid the crashing and simplify things for you, ZXing have provided a utility class which you could integrate into your application to make the installation of ZXing smoother, by redirecting the user to the Android Market if they don't have it installed already.
Finally, if you want to integrate barcode scanning directly into your application without relying on having the separate ZXing application installed, well then it's an open source project and you can do so! :)
Edit: Somebody edited this guide into this answer (it sounds a bit odd, I can't vouch as to its accuracy, and I'm not sure why they're using Eclipse in 2015):
Step by step to setup zxing 3.2.1 in eclipse
- Download zxing-master.zip from "https://github.com/zxing/zxing"
- Unzip zxing-master.zip, Use eclipse to import "android" project in zxing-master
- Download core-3.2.1.jar from "http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/"
- Create "libs" folder in "android" project and paste cor-3.2.1.jar into the libs folder
- 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
- 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.
- Clean and build project. If your project show error about "switch - case", you should change them to "if - else".
- Completed. Clean and build project. You can click on "Proprties" > "Android" > click on "Is Libraries" to use for your project.
I had a problem with implementing the code until I found some website (I can't find it again right now) that explained that you need to include the package name in the name of the intent.putExtra.
It would pull up the application, but it wouldn't recognize any barcodes, and when I changed it from.
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
to
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
It worked great. Just a tip for any other novice Android programmers.
Using the provided IntentInegrator is better. It allows you to prompt your user to install the barcode scanner if they do not have it. It also allows you to customize the messages. The IntentIntegrator.REQUEST_CODE constant holds the value of the request code for the onActivityResult to check for in the above if block.
IntentIntegrator intentIntegrator = new IntentIntegrator(this); // where this is activity
intentIntegrator.initiateScan(IntentIntegrator.ALL_CODE_TYPES); // or QR_CODE_TYPES if you need to scan QR only
IntentIntegrator.java
Using Zxing this way requires a user to also install the barcode scanner app, which isn't ideal. What you probably want is to bundle Zxing into your app directly.
I highly recommend using this library: https://github.com/dm77/barcodescanner
It takes all the crazy build issues you're going to run into trying to integrate Xzing or Zbar directly. It uses those libraries under the covers, but wraps them in a very simple to use API.
If you want to include into your code and not use the IntentIntegrator that the ZXing library recommend, you can use some of these ports:
http://code.google.com/p/android-zxinglib/
http://code.google.com/p/android-quick-response-code/
I use the first, and it works perfectly! It has a sample project to try it on.
Barcode Detection is now available in Google Play services. Code lab of the setup process, here are the api docs, and a sample project.
You can use this quick start guide http://shyyko.wordpress.com/2013/07/30/zxing-with-android-quick-start/ with simple example project to build android app without IntentIntegrator.
精彩评论