Getting scan result when using zxing?

I am currently using the Zxing library in my application. After scanning the barcode of the book, for example, how to get the result, like an 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 the help

+2
source share
5 answers

Zxing scans various barcodes / QR codes, so the first thing you need to do is find out if its product UPC code or 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);
            }
        }
    }   

-, UPC. Google API . , json- UPC = 037988482481 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.

AsyncTask , UPC.

+4

"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 , . , - .

+2

, Android ZXing. Android- : ZXing Android. ISBN : Android ZXing app 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));
}
+1
source

In yours, onActivityResultexecute the 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 did not call result.getContents().

0
source

I just want to add that if you want to be able to go back without a RuntimeException, surround the if statement with the catch try block. So:

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

Hope this helps the inevitable crash that you would encounter without it.

-1
source

All Articles