2011-03-31 128 views
2

我目前在我的应用程序中使用Zxing库。例如,扫描一本书的条形码后,如何从扫描结果中获取图像,说明等内容。使用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"); 
       } 
      } 
     } 

感谢您的帮助

+0

请查看这里我的回答,希望这有助于 http://stackoverflow.com/a/30627814/2904625 – user2904625 2015-06-03 18:33:14

回答

1

您可以检查的Android应用斑马线做什么。 Android客户端的源代码位于:ZXing Android client source code。为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)); 
} 
4

斑马线扫描各种条形码/ 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码的产品元数据。一个相当全面的将是谷歌的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”与谷歌API键。

百思买还为其提供的所有产品提供RESTful products API,这些产品可通过UPC代码进行搜索。

一旦拥有UPC,您就需要使用AsyncTask来获取产品元数据。

-1

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

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

希望能够帮助你在没有它的情况下不可避免的崩溃。

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

有了这个,你扫描条码,现在有在斑马线代码,使用谷歌API用于查找另一个库ISBN。 在类Intents.java中,您可以获得有关额外需求的信息,而类ISBNResultHandler可显示结果。 希望它能帮助未来的人。

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()