Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: zxing barcode scan successful but not returning from activity

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
                }
            }
        }
    }
like image 232
Barry Avatar asked Jun 13 '12 13:06

Barry


3 Answers

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

like image 159
Barry Avatar answered Oct 23 '22 03:10

Barry


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

like image 5
Sean Owen Avatar answered Oct 23 '22 04:10

Sean Owen


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

like image 1
Alfred Avatar answered Oct 23 '22 04:10

Alfred