0

I need to build an app which will recognize QR codes without forcing the user to install other apps. In future, I will also need to manipulate the scanned image before recognizing the code (codes scanned by me will have inverted colors).

Trying to follow hints and tutorials from these links:
Integrating the ZXing library directly into my Android application
Embedding ZXing in android app
http://karanbalkar.com/2013/12/tutorial-65-implement-barcode-scanner-using-zxing-in-android/

After creating some basic code and running the app I click my Scan button and get an error that No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) }

What I've done:
Create new project
Copy core-3.2.1.jar to libs/
Add intent calling and result handling

intent/result code added by me:

private Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scan= (Button)findViewById(R.id.btnScan);

    scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            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
            MultiFormatWriter writer = new MultiFormatWriter();


        } else if (resultCode == RESULT_CANCELED) {
            // Handle cancel
            Log.i("App","Scan unsuccessful");
        }
    }
}

How to start the intent? What am I doing wrong?

Community
  • 1
  • 1
Val
  • 1,548
  • 1
  • 20
  • 36

2 Answers2

2

you should launch scan like this:

@Override
    public void onClick(View v) {
      IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
      integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
      //in case you want to customize a bit.
      integrator.setPrompt("Scan a QR/Bar code");
      integrator.setCameraId(0);
      integrator.setBeepEnabled(false);
      integrator.initiateScan();
    }

Receive results like this:

@Override
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);
                String data = scanResult.getContents();
                // use this data
            } else {
                // error
            }
            break;
        }
    }
}

Edit 1:

Add this to build.gradle of your app as dependencies:

compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
compile 'com.google.zxing:core:3.2.0'
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • where should I take the `IntentIntegrator` class from? is it in the zxing library? – Val Mar 31 '16 at 13:33
  • yes, `import com.google.zxing.integration.android.IntentIntegrator;` – Rohit Arya Mar 31 '16 at 13:34
  • I compiled these libs: `compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar' compile 'com.google.zxing:core:3.2.0'` – Rohit Arya Mar 31 '16 at 13:35
  • @ValCool, used [this library](https://github.com/journeyapps/zxing-android-embedded) – Rohit Arya Mar 31 '16 at 13:36
  • the studio says it cannot resolve symbol `integration` (in `import com.google.zxing.integration.android.IntentIntegrator`) – Val Mar 31 '16 at 13:40
  • Post your `build.gradle` file. – Rohit Arya Mar 31 '16 at 13:47
  • i've copy-pasted your onClick code and also the import for IntentIntegrator but AS still says it cannot resolve symbol 'integration' – Val Mar 31 '16 at 13:54
  • do i need to actually compile the zxing library before using it in my app? i thought it was enough to just copy the provided .jar file to the libs/ folder – Val Mar 31 '16 at 13:56
  • here's my MainActivity.java http://paste.ofcode.org/dhkWhsgMGr5vp5tqmXUgxB btw, i'm also using the original ZXing project (you seem to be using the 'embedded' fork of it) – Val Mar 31 '16 at 13:59
  • it should be `MainActivity.this` since your `activity`'s name is `MainActivity`. – Rohit Arya Mar 31 '16 at 14:21
  • `IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);` – Rohit Arya Mar 31 '16 at 14:47
  • Read it again. It should be MAIN not MY. – Rohit Arya Mar 31 '16 at 15:04
  • ok, the gradle dependencies are working. after deleting the jar from libs/ it now compiles fine – Val Apr 01 '16 at 07:33
0

Try the below link it worked for me:

https://github.com/dlazaro66/QRCodeReaderView

Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23