14

I am successfully using zxing to scan codes, by calling the installed barcode reader's intent, but when it beeps and indicates a good scan I expect the zxing activity would return control so I can process the result, but it sits there and tries to scan again. I have to press the back button and then it returns and I can do the next step. Is there some obvious flag I'm missing when I call the scanner?

Any advice gratefully received. Many thanks.

Here's my code:

public boolean onTouchEvent(final MotionEvent event) {

    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
    startActivityForResult(intent, 0);

    return true;
    }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

            // Handle successful scan

            String s = "http://www.google.com/search?q=";
            s += contents;
            Intent myIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
            startActivity(myIntent1);
            }
        else 
            if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                }
            }
        }
    }
Barry
  • 1,258
  • 3
  • 13
  • 27

5 Answers5

10

Here's the full answer to my own question, hope this helps someone:

Go here and copy the whole IntentIntegrator class, add it to your app; also go here and copy the IntentResult class to your app. Now add this to your activity (or trigger the scan by a button / whatever):

public boolean onTouchEvent(final MotionEvent event) {

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

    return true;
    }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
        // handle scan result
          String s = "http://www.google.com/search?q=";
            s += scanResult.getContents();

            Intent myIntent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
            startActivity(myIntent1);
      }
      // else continue with any other code you need in the method
      //...
    }

It would have been great to just call the services provided by the Barcode scanner app and not copy and paste chunks of code into your own app but this seems to be the recommended way :(

Barry
  • 1,258
  • 3
  • 13
  • 27
  • I'm not sure I understand your last comment. This is the minimum amount of code you can copy and paste since you are using the provided integration library. Sorry, it can't be 0 lines of course. You can not use the integration library. But then you will re-write its code, which is maybe tens of lines, sure. But that's more. Sure, do that if you like -- what's the difference between reinventing the needed code, and copying the provided needed code? – Sean Owen Jun 13 '12 at 19:28
  • Sorry Sean, I didn't mean to whine (tho I probably did - it was a long day, I apologise). My original stab, copied from an earlier answer here, was a few lines which used the intent offered by your app, which would have been perfect. You recommended I use IntentIntegrator which is a different route, and of course it works perfectly. My first hack was *so close* - if I could have got your intent to release after a successful scan I would have got the functionality in 10 lines, rather than adding 500 lines and two classes. Cheers, B. – Barry Jun 14 '12 at 07:57
  • can you please check my [question](https://stackoverflow.com/q/64784906/8868582) related to it? – Faisal Qayyum Nov 11 '20 at 10:49
5

Why not use the provided IntentIntegrator class? This is the only approach mentioned in the project docs, did you have a look at those? https://github.com/zxing/zxing/wiki/Scanning-Via-Intent

I created it to wrap up these details of sending and parsing the Intent, so you don't make typos. For example, there's no such thing as extra "com.google.zxing.client.android.SCAN.SCAN_MODE".

PhilLab
  • 4,777
  • 1
  • 25
  • 77
Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Sorry Sean, I'm noob: import com.google.zxing.integration.android.IntentIntegrator; is failing as com.google can't be resolved. Am I missing a link somewhere? Many thanks. – Barry Jun 13 '12 at 16:36
  • OK, it all works! Sorry, I didn't realise you meant add two whole classes to my app (IntentIntegrator and IntentResult). The original plan, to use about 10 lines of code to call the installed scanner was ideal if I could have found a way to get it working. That way updates to the scanner = updates to my app too, this way I have to keep an eye on your changes manually :( The thing you said doesn't exist came from [link](http://stackoverflow.com/questions/2050263/using-zxing-to-create-an-android-barcode-scanning-app) - a conversation you had been involved in last year. Thanks for the help. – Barry Jun 13 '12 at 17:12
1

Add finishActivity(requestCode); at the end of onActivityResult() method.

Try this: Replace your first 2 lines in onTouch with the below code. It seems the issue is while scanning codes other than QR. Please remove the scan filter and check once.

Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

Alfred
  • 331
  • 4
  • 9
  • Sorry Alfi, that's *after* the problem. Execution never reaches '//handle successful scan' until I hit the back key. It seems to be looping on the scan itself and never passing control back to my code at all. – Barry Jun 13 '12 at 15:08
  • Are you scanning only QR Codes, as your code shows? If you try and scan 1D and matrix it keeps showing the green dots without actually scanning anything. – Alfred Jun 13 '12 at 15:24
  • With the code above it successfully scans QR and EAN13 etc, but this is my first dip into zxing so any other pointers are welcome, but my main problem is getting zxing to let go when it gets a good result. – Barry Jun 13 '12 at 15:33
  • Sorry, no change! Operates exactly the same - a successful scan; scanner beeps; carries on looking for a code and if I loiter over the barcode already captured it scans it and beeps again; I press back - twice - and the scanner activity closes and the web browser goes off and googles the (correctly scanned) result : – Barry Jun 13 '12 at 15:54
1

I was having the same issue so I tried using the IntentIntegrator class as recommended by Sean Owen. I still had the problem until I realized this was only happening when trying to scan a barcode in portrait (most frequently on phones). It turns out that the orientation change from portrait to landscape causes the double scan. I resolved this by adding android:configChanges="orientation|keyboardHidden|screenSize" to the activity in my manifest. You probably only need the orientation one, but that is untested.

For any users experiencing this issue when creating an Adobe AIR Native Extension, be sure to add that line not only to your android project manifest, but also to your activity tag in your android manifest additions in your app.xml.

Colin
  • 650
  • 7
  • 15
  • It helps if the activity from which you start the CaptureActivity, is in landscape mode. I added `android:screenOrientation="landscape"` to that activity in the Manifest. Which worked for me. – Christine Jan 16 '17 at 10:48
0

Here is the solution that I am using. It is working fine for me.

Intent intent = new Intent(SelectOptionActivity.this, CaptureActivity.class);
                intent.putExtra("SCAN_MODE", "ONE_D_MODE");
                intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A,QR_CODE");
                intent.setAction(Intents.Scan.ACTION);
                startActivityForResult(intent, 1);


public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            final String contents = intent.getStringExtra(Intents.Scan.RESULT);
            final String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);

        }
    }
SANJAY GUPTA
  • 1,564
  • 13
  • 21