Mark Comix
Mark Comix

Reputation: 1898

Using "Zxing's barcode scanner" App on Android

I'm modifying an existing app. The app used "Zxing's barcode scanner" through Java class and packages.

My project includes those packages:

com.google.zxing com.google.zxing.integration com.google.zxing.integration.android

I have a class with some code like this:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class QRdecoderActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // temp = this;

        IntentIntegrator.initiateScan(this);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {

            case IntentIntegrator.REQUEST_CODE: {

                if (resultCode != RESULT_CANCELED) {

                    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

                    if (scanResult != null) {
                        String upc = scanResult.getContents();

                        Toast.makeText(this, "Contents : " + upc, Toast.LENGTH_LONG).show();

                    }

                }
                finish();               

                break;
            }
        }
    }   
}

Everything is working fine, but when I start the testing process, I discover that I need to have the " Barcode Scanner" App installed.

It thats right?.

I thought I did not need to have, if it using the Java Class inside my project.

How I can check if the App is installed? and How can I go to the "Google Play" and show it, to the user, from my code for download?

Upvotes: 0

Views: 4129

Answers (1)

Graham Smith
Graham Smith

Reputation: 25757

This has been discussed before and is extremely well documented at the Zxing website. While you can integrate the source into your app, you can also scan via intent.

From what you posted, it looks like the source code has been integrated into the app, therefore you should not need it installed (as all the classes should be there).

If you are being prompted to install the barcode scanner app, it sounds like scanning via intent is being used. The end result is that you have a cocktail of both approaches, where scanning via intent is the method being used.

I, personally, prefer scanning via intent. This is documented here: http://code.google.com/p/zxing/wiki/ScanningViaIntent.

My reasoning is that your app becomes independent of the bar code scanner. Any updates caused by new barcode standards or general bug fixes/improvements are made immediately available to the end user (as an update via Google Play), as they do not have to wait for your app to integrate any updated source code. Also, it is encouraged that you only use the source for Zxing if you plan to add value to it.

How I can check if the App is installed? and How can I go to the "Google Play" and show it, to the user, from my code for download?

Zxing provide the class to gracefully handle the situation where the user makes the intent and the Barcode Scanner app is not installed. It will take the user to the app on Google play directly. You can find it at http://code.google.com/p/zxing/source/browse/trunk/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java.

Once you have the class, you simply need to call the following:

IntentIntegrator integrator = new IntentIntegrator(yourActivity);
integrator.initiateScan();

and then add to your Activity

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
  if (scanResult != null) {
    // handle scan result
  }
  // else continue with any other code you need in the method
  ...
}

Upvotes: 5

Related Questions