How to use Zxing in android

RAAAAM picture RAAAAM · Jan 3, 2012 · Viewed 40.5k times · Source

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.

Answer

Seshu Vinay picture Seshu Vinay · Jan 3, 2012

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:

http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/Intents.java