getting scan result when using zxing?

前端 未结 5 350
心在旅途
心在旅途 2021-01-03 11:51

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 resu

相关标签:
5条回答
  • 2021-01-03 12:32

    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.

    0 讨论(0)
  • 2021-01-03 12:41

    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));
    }
    
    0 讨论(0)
  • 2021-01-03 12:47

    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.

    0 讨论(0)
  • 2021-01-03 12:51

    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.

    0 讨论(0)
  • 2021-01-03 12:56

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

    0 讨论(0)
提交回复
热议问题