Calling barcode scanner on a button click in android application

Solution 1:

I think that "copying" Barcode Scanner and include it in your app might be overloading your projects. You should certainly use the Intent from the Scanner:

From here: http://code.google.com/p/zxing/wiki/ScanningViaIntent

If the Barcode Scanner is installed on your Android device, you can have it scan for you and return the result, just by sending it an Intent. For example, you can hook up a button to scan a QR code 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.setPackage("com.google.zxing.client.android");
        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
        }
    }
}

For more options, like scanning a product barcode, or asking Barcode Scanner to encode and display a barcode for you, see this source file:

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

And here's some source from our test app which shows how to use them:

http://code.google.com/p/zxing/source/browse/trunk/androidtest/src/com/google/zxing/client/androidtest/ZXingTestActivity.java

IntentIntegrator

We have also begun creating a small library of classes that encapsulate some of the details above. See IntentIntegrator for a possibly easier way to integrate. In particular this will handle the case where Barcode Scanner is not yet installed.

http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java

Via URL As of Barcode Scanner v2.6, you can also launch the app from a URL in the Browser. Simple create a hyperlink to http://zxing.appspot.com/scan and Barcode Scanner will offer to launch to handle it. Users can also choose to always have Barcode Scanner open automatically.

NOTE: This URL is not meant to serve an actual web page in a browser, it's just a hook to launch a native app.

Known Issues User jamesikanos reports the following 'gotcha':

Create a TabHost activity with launchMode "singleInstance" Create a child activity with a "Start scan" button (launch zxing using the IntentIntegrator from this button) onActivityResult in your child activity will return immediately as "cancelled" onActivityResult is never called subsequently