获得QR扫描通过ZXing结果集成到您的应用程序 [英] Obtaining QR scan results via ZXing that is integrated into your app

查看:126
本文介绍了获得QR扫描通过ZXing结果集成到您的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个答案以创建一个独立的Andr​​oid库项目已在它的ZXing源$ C ​​$ C( ZXing V2.1)。它编译罚款,如果我跑CaptureActivity,我可以读取QR code符合市场预期。

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.

我有,我想拉这个库中的另一个Android项目。我已正确设置该库的关系了。

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

我有是,我怎么启动ZXing扫描我的本地副本通过IntentIntegrator问题(提及的这里)。

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

我试图修改IntentIntegrator.initiateScan()方法使用CaptureActivity我的本地副本,并加载了QR扫描仪正常。 然而,一旦QR code扫描的二维信息显示在屏幕上,而不是将结果发送回在onActivityResult我的呼唤活动。

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.

我怎样才能使它发QR扫描结果对我的呼唤活动onActivityResult?

有关引用,这里是我改变了IntentIntegrator.initiateScan()方法:

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.

解决方案:

下面就是结束了工作。我仍然调用它以同样的方式有:

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

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

但initiateScan方法现在看起来是这样的:

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

重要的事情是确保BS_PACKAGE指向CaptureActivity包,你所说的act.startActivityForResult ......,而不仅仅是startActivityForResult ......那你叫setSingleTargetApplication与应用程序的包名即会在调用扫描仪。

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.

推荐答案

尝试改变行 startActivityForResult(intentScan,REQUEST_ code);

act.startActivityForResult(intentScan,REQUEST_ code);

您不必注释掉code包含 findTargetAppPackage ,只是通过调用 setSingleTargetApplication()(如果你使用这个库的唯一应用程序)

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)

这篇关于获得QR扫描通过ZXing结果集成到您的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆