Tuesday, August 7, 2012

Android - How to write a simple barcode scanner or a QR scanner


Integrating a barcode scanner to your android application is simpler than in other platforms.

There are two ways you can get your android app the ability to scan barcodes.
1. Sending intents to the barcode scanner app - The simplest way
2. Using google source for Zxing - For those who can easily crunch huge volume of code

As the post title says "Simple barcode / QR code scanner" I will discuss the first method here

1. Sending intent for starting the scan

We have to use the intents provided by the barcode scanner app and start the activity for result using startActivityForResult

           Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");  
           intentScan.addCategory(Intent.CATEGORY_DEFAULT);  
   
           // If only QR code should be scanned  
           //intentScan.putExtra("SCAN_FORMATS", "QR_CODE");  
   
           try {  
                this.startActivityForResult(intentScan, MY_REQUEST_CODE);  
           } catch (ActivityNotFoundException e) {  
                downloadFromMarket();  
           }  

If you don't specify any "SCAN_FORMATS" as intent extras, it will scan for all the supported formats (QR_CODE,UPC_A,UPC_E,EAN_8,EAN_13,CODE_39,CODE_93,CODE_128 )
You can speficy only the formats you want by comma separated strings. Example:

 intentScan.putExtra("SCAN_FORMATS", "QR_CODE,UPC_A,UPC_E");  

2. Handling the onActivityResult

Retrieve the "SCAN_RESULT" and "SCAN_RESULT_FORMAT" from the result. In the below code, both the data are extracted and set to a textview.
Make sure you access the intent only when resultCode == Activity.RESULT_OK

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   
           if (requestCode == MY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {  
                String contents = data.getStringExtra("SCAN_RESULT");  
                String formatName = data.getStringExtra("SCAN_RESULT_FORMAT");  
                  
                TextView tv = (TextView)findViewById(R.id.tvResult);  
                tv.setText("Format: " + formatName+ " Code: "+contents);  
           }  
      }  

3. The Barcode scanner app 

The Barcode scanner app from zxing is opensource and you can download the source code form google code. But here we are interested only in the final application.
   
If the barcode scanner app is not present in the device, we can make the user to download it when startActivityForResult() fails with ActivityNotFoundException

      public void downloadFromMarket() {  
           AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);  
           downloadDialog.setTitle("Warning");  
           downloadDialog.setMessage("Barcode app not found. Download?");  
           downloadDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialogInterface, int i) {  
                     Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");  
                     Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
                     startActivity(intent);  
                }  
           });  
   
           downloadDialog.show();  
      }  

3 comments: