android: zxing barcode scan successful but not returning from activity

16,989

Solution 1

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".

Solution 2

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 :(

Solution 3

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);

Solution 4

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.

Share:
16,989

Related videos on Youtube

Barry
Author by

Barry

I'm a technical writer, but I also write Android apps in Java - currently an STL file viewer and a machine vision demo.

Updated on September 16, 2022

Comments

  • Barry
    Barry over 1 year

    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
    Barry almost 12 years
    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.
  • Alfred
    Alfred almost 12 years
    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.
  • Barry
    Barry almost 12 years
    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
    Barry almost 12 years
    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
    Barry almost 12 years
    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
    Barry almost 12 years
    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 - a conversation you had been involved in last year. Thanks for the help.
  • Sean Owen
    Sean Owen almost 12 years
    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?
  • Barry
    Barry almost 12 years
    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.
  • Christine
    Christine over 7 years
    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.
  • Sagar
    Sagar almost 5 years
    how to return result from back to previous form second activity?
  • Faisal Qayyum
    Faisal Qayyum over 3 years
    can you please check my question related to it?