This question already has an answer here:
I am new to Zxing. I am doing barcode conversion using zxing in my android application. Can anyone guide me how to include zxing to android device.
If the zxing barcode scanner is installed in the mobile, its very easy:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");//for Qr code, its "QR_CODE_MODE" instead of "PRODUCT_MODE"
intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history
startActivityForResult(intent, 0);
and in OnActivityResult
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT"); //this is the result
} else
if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
If its not installed: u can put this code in try-catch block and catching the exception, u can do this:
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
startActivity(marketIntent);
So it redirects the app to android market
and ur app continues running once if the barcode scanner
is installed.
If u dont want to use the other app in ur app, U have to download zxing library and try using the classes from core.jar file(it is created using apache ant). Follow this tutorial to do that: https://github.com/zxing/zxing/wiki/Getting-Started-Developing
All Intent options can be found here:
You need to download the Zing's .Jar file & add in to your application folder. Then you can call classes & methods of it.
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
来源:https://stackoverflow.com/questions/8708705/how-to-use-zxing-in-android