2

我目前在我的应用程序中使用 Zxing 库。例如,在扫描一本书的条形码后,我如何从扫描结果中获取图像、描述等内容。

              @Override
      public void onActivityResult(int requestCode, int resultCode, Intent intent) {
          switch(requestCode) {
          case IntentIntegrator.REQUEST_CODE:
              if (resultCode == RESULT_OK) {
                  IntentResult scanResult = 
                      IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
              } else if (resultCode == RESULT_CANCELED) {
                showDialog("failed", "You messed up");
              }
          }
      }

谢谢你的帮助

4

5 回答 5

4

Zxing 扫描各种条码/二维码,所以你首先需要弄清楚它是产品 UPC 还是二维码:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String response = data.getAction();

            if(Pattern.matches("[0-9]{1,13}", response)) {
                // response is a UPC code, fetch product meta data
                // using Google Products API, Best Buy Remix, etc.          
            } else {
                // QR code - phone #, url, location, email, etc. 
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(response));
                startActivity(intent);
            }
        }
    }   

有许多可用的 Web 服务将返回给定 UPC 代码的产品元数据。一个相当全面的将是 Google 的Search API for Shopping。例如,您可以获得 UPC = 037988482481 的产品的 json 表示形式,其 URL 如下所示:

https://www.googleapis.com/shopping/search/v1/public/products?country=US&key=your_key_here&restrictBy=gtin:037988482481

您需要将“your_key_here”替换为您的 Google API 密钥。

百思买还为他们携带的所有产品提供了一个 RESTful产品 API,可通过 UPC 代码进行搜索。

拥有 UPC 后,您将需要使用 AsyncTask 来获取产品元数据。

于 2011-03-31T19:04:14.923 回答
2

你不需要'IntentResult'或'IntentIntegrator'。
你可以这样做:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);

然后在onActivityResult

if (requestCode == 0) {
    if ((resultCode == RESULT_CANCELED) || (resultCode == -1000)) {
    Toast.makeText(WebViewActivity.this, "Nothing captured",
                    Toast.LENGTH_SHORT).show();
} else {
    String capturedQrValue = data.getStringExtra("barcode_data");
}
}

用这个扫描条形码,现在 Zxing 代码中有另一个使用 Google API 查找 ISBN 的库。在课程Intents.java中,您可以了解意图需要的额外信息,并且课程ISBNResultHandler会显示结果。希望它对将来的人有所帮助。

于 2014-03-27T15:44:23.510 回答
1

您可以查看 Android ZXing 应用程序的功能。安卓客户端源码在:ZXing安卓客户端源码。ISBN号的处理源代码为:Android ZXing app's ISBN Result Handler code

对于产品和书籍搜索,Android 代码从ResultHandler.java调用这两个函数:

// Uses the mobile-specific version of Product Search, which is formatted for small screens.
final void openProductSearch(String upc) {
    Uri uri = Uri.parse("http://www.google." + LocaleManager.getProductSearchCountryTLD() +
        "/m/products?q=" + upc + "&source=zxing");
    launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}

final void openBookSearch(String isbn) {
    Uri uri = Uri.parse("http://books.google." + LocaleManager.getBookSearchCountryTLD() +
        "/books?vid=isbn" + isbn);
    launchIntent(new Intent(Intent.ACTION_VIEW, uri));
}
于 2011-03-31T18:37:05.320 回答
0

在你onActivityResult喜欢下面的代码

    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
        } else {
           String qrCode=result.getContents();
        }
    } else {
        // This is important, otherwise the result will not be passed to the fragment
        super.onActivityResult(requestCode, resultCode, data);
    }

你没有打电话result.getContents()

于 2017-04-24T06:44:16.640 回答
-1

我只想补充一点,如果您希望能够在没有 RuntimeException 的情况下用 try catch 块包围您的 if 语句。所以:

 try{
 /// get scanned code here
 } catch(RuntimeException e) {
 e.getStackTrace();
 {

希望这有助于您在没有它的情况下面临的不可避免的崩溃。

于 2012-04-27T11:36:25.673 回答