1

我使用这个答案创建了一个独立的 Android 库项目,其中包含 ZXing 源代码(ZXing v2.1)。它编译得很好,如果我运行 CaptureActivity,我可以按预期读取 QR 码。

我有另一个 Android 项目,我想从中提取这个库。我已经正确设置了该库关系。

我遇到的问题是,如何通过 IntentIntegrator(在此处提到)启动 ZXing 扫描仪的本地副本。

我尝试修改 IntentIntegrator.initiateScan() 方法以使用 CaptureActivity 的本地副本,并正确加载 QR 扫描仪。 但是,一旦扫描了二维码,二维码信息就会显示在屏幕上,而不是在 onActivityResult 中将结果发送回我的调用活动。

如何让它将 QR 扫描结果发送到我的呼叫活动的 onActivityResult?

作为参考,我将 IntentIntegrator.initiateScan() 方法更改为:

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

我正在像这样开始扫描:

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

我觉得我在这里错过了一些简单的东西,任何朝着正确方向的推动都会很棒。

解决方案

这就是最终的工作。我仍然以相同的方式调用它:

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

但是initialScan 方法现在看起来像这样:

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

重要的事情是确保 BS_PACKAGE 指向 CaptureActivity 包,您调用“act.startActivityForResult...”而不是“startActivityForResult...”,并且您调用 setSingleTargetApplication 的应用程序的包名称将调用扫描器。

4

1 回答 1

1

尝试换行startActivityForResult(intentScan, REQUEST_CODE);

act.startActivityForResult(intentScan, REQUEST_CODE);

您不需要注释包含的代码findTargetAppPackage,只需通过调用设置目标应用程序的包setSingleTargetApplication()(如果您是唯一使用此库的应用程序)

于 2013-02-17T21:10:23.020 回答