2011-03-31 130 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()