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 はさまざまなバーコード/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);
            }
        }
    }   

UPC コードを指定して製品メタデータを返す Web サービスは多数あります。かなり包括的なものは、Google のSearch API for Shoppingです。たとえば、次のような URL で UPC = 037988482481 の製品の json 表現を取得できます。

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は、UPC コードで検索可能なすべての製品に対して、RESTful製品 APIを提供しています。

UPC を取得したら、AsyncTask を使用して製品のメタデータをフェッチする必要があります。

于 2011-03-31T19:04:14.923 に答える
1

Android ZXing アプリの機能を確認できます。Android クライアントのソースは、ZXing Android クライアント ソース コードにあります。ISBN 番号の場合、それを処理するためのソース コード: Android ZXing アプリの ISBN 結果ハンドラー コード

商品検索と書籍検索の場合、Android コードはResultHandler.javaから次の 2 つの関数を呼び出します。

// 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なしで押し戻すことができるようにしたい場合は、ifステートメントをtrycatchブロックで囲んでください。それで:

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

それなしで直面する避けられないクラッシュに役立つことを願っています。

于 2012-04-27T11:36:25.673 に答える