Obtaining QR scan results via ZXing that is integr

2019-04-16 11:56发布

问题:

I used this answer to create a standalone Android library project that has the ZXing source code in it (ZXing v2.1). It compiles fine and if I run CaptureActivity, I can read a QR code as expected.

I have another Android project from which I want to pull in this library. I have set that library relationship up correctly.

The issue I am having is, how do I launch my local copy of the ZXing scanner via IntentIntegrator (mentioned here).

I tried modifying the IntentIntegrator.initiateScan() method to use my local copy of CaptureActivity, and that loads the QR scanner properly. However, once the QR code is scanned the QR information is displayed on-screen instead of sending the result back to my calling activity in onActivityResult.

How can I make it send the QR scan results to onActivityResult of my calling activity?

For reference, here is what I changed the IntentIntegrator.initiateScan() method to:

  public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats) {       

  //Hardcoding name of activity to call --> is this where I've gone wrong?
    Intent intentScan = new Intent(act, CaptureActivity.class);

    intentScan.addCategory(Intent.CATEGORY_DEFAULT);

    // check which types of codes to scan for
    if (desiredBarcodeFormats != null) {
      // set the desired barcode types
      StringBuilder joinedByComma = new StringBuilder();
      for (String format : desiredBarcodeFormats) {
        if (joinedByComma.length() > 0) {
          joinedByComma.append(',');
        }
        joinedByComma.append(format);
      }
      intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
    }


//Commented this out because it didn't seem to find my class...

//    String targetAppPackage = findTargetAppPackage(intentScan);
//    if (targetAppPackage == null) {
//      return showDownloadDialog();
//    }
//    
//    
//    intentScan.setPackage(targetAppPackage);
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    attachMoreExtras(intentScan);
    startActivityForResult(intentScan, REQUEST_CODE);
    return null;
  }

And I'm initiating the scan like this:

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

I feel like I'm missing something easy here, any push in the right direction would be great.

SOLUTION

Here's what ended up working. I still invoke it the same way with:

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

But the initiateScan method now looks like this:

  public AlertDialog initiateScan(Activity act, Collection<String> desiredBarcodeFormats) 
  {

    Intent intentScan = new Intent(BS_PACKAGE + ".SCAN");

    intentScan.addCategory(Intent.CATEGORY_DEFAULT);

    // check which types of codes to scan for
    if (desiredBarcodeFormats != null) {
      // set the desired barcode types
      StringBuilder joinedByComma = new StringBuilder();
      for (String format : desiredBarcodeFormats) {
        if (joinedByComma.length() > 0) {
          joinedByComma.append(',');
        }
        joinedByComma.append(format);
      }
      intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
    }

    //THIS WAS THE KEY
    setSingleTargetApplication(act.getPackageName());

    String targetAppPackage = findTargetAppPackage(intentScan);
    if (targetAppPackage == null) {
      return showDownloadDialog();
    }

    intentScan.setPackage(targetAppPackage);
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    attachMoreExtras(intentScan);
    act.startActivityForResult(intentScan, REQUEST_CODE);
    return null;
  }

Important things are make sure that BS_PACKAGE points to the CaptureActivity package, that you call "act.startActivityForResult..." instead of just "startActivityForResult..." and that you call setSingleTargetApplication with the package name of the application that will be calling the scanner.

回答1:

Try to change the line startActivityForResult(intentScan, REQUEST_CODE);

to act.startActivityForResult(intentScan, REQUEST_CODE);

You do not need to comment the code that contains findTargetAppPackage, just set your target application's package by calling setSingleTargetApplication() (if you are the only application using this library)