android: zxing barcode scan successful but not returning from activity

后端 未结 5 546
悲哀的现实
悲哀的现实 2021-01-04 06:23

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

相关标签:
5条回答
  • 2021-01-04 06:30

    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.

    0 讨论(0)
  • 2021-01-04 06:42

    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);
    
            }
        }
    
    0 讨论(0)
  • 2021-01-04 06:46

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

    0 讨论(0)
  • 2021-01-04 06:49

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

    0 讨论(0)
  • 2021-01-04 06:50

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

    0 讨论(0)
提交回复
热议问题