使用zxing时获取扫描结果?

发布于 2024-10-28 12:44:52 字数 622 浏览 7 评论 0原文

我目前在我的应用程序中使用 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");
              }
          }
      }

感谢您的帮助

I am currently using the Zxing library in my app. After scanning the bar code of a book for example, how do I get things like the image, description, etc. from the scan result.

              @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");
              }
          }
      }

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彩虹直至黑白 2024-11-04 12:44:52

Zxing 扫描各种条形码/QR 码,因此您需要做的第一件事是确定它是产品 UPC 还是 QR 码:

    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 密钥。

Best Buy 还为其所有产品提供 RESTful 产品 API,可通过 UPC 代码进行搜索。

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

Zxing scans a variety of barcodes/QR codes, so the first thing you need to do is figure out if its a product UPC or a QR code:

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

There are a number of web services available that will return product meta data given a UPC code. A fairly comprehensive one would be Google's Search API for Shopping. For example, you can get a json representations of the product with UPC = 037988482481 with an URL that looks like this:

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

You'll need to replace "your_key_here" with your Google API key.

Best Buy also offers a RESTful products API for all of the products they carry which is searchable by UPC code.

You'll want to use an AsyncTask to fetch the product metadata once you have the UPC.

玩物 2024-11-04 12:44:52

您不需要“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 类则显示结果。
希望它对将来的人有所帮助。

You dont need the 'IntentResult' or 'IntentIntegrator' for that.
You can do this:

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

And then in 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");
}
}

With this you scan the barcode, now there is in the Zxing code another library that uses Google API for looking up that ISBN.
In the class Intents.java you have the info of what extras the intent needs and the class ISBNResultHandler shows what is the result.
Hope it helps someone in the future.

甩你一脸翔 2024-11-04 12:44:52

您可以检查 Android ZXing 应用程序的功能。 Android客户端源码位于:ZXing Android客户端源码。对于 ISBN 编号,处理的源代码为:Android ZXing 应用的 ISBN 结果处理程序代码

对于产品和图书搜索,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));
}

You can check what the Android ZXing app does. The source for the Android client is in: ZXing Android client source code. For ISBN numbers, the source code for handling that is: Android ZXing app's ISBN Result Handler code

For product and book search, the Android code invokes these two functions from 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));
}
南风几经秋 2024-11-04 12:44:52

在您的 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()

In your onActivityResult do like following code

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

You haven't called result.getContents().

节枝 2024-11-04 12:44:52

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

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

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

I just want to add that if you want to be able to press back without a RuntimeException surround your if statement with a try catch block. so:

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

Hope that helped the inevitable crash you would face without it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文