Android QR Code / Bar Code Scanner

During the development of an android application, sometimes it is required to provide a functionality that scans a QR code or barcode. Scanning the QR code can be done programmatically by using many ways:

  • Using a web-based API solution, where the QR code or barcode is uploaded to the server, and the server returns the results.
  • Using a web-based application that accesses your camera and scans the QR code or barcode and returns the results.
  • By Integrating the Mobile Vision API of Google Play Service.

Android QR Code Scanner Example

In this example, we will scan the QR code of a web URL and Email address, and act on it. Here we will use the Mobile Vision API of Google Play Service to scan the QR code. The Mobile Vision API supports the following formats of the barcode.

  • 1D barcodes: EAN-8, UPC-A, EAN-13, EAN-8, UPC-E, Code-93, Code-128, Code-39, Codabar, ITF.
  • 2D barcodes: QR Code, Data Matrix, AZTEC, PDF-417.

Create an activity_main.xml and add the following code.

activity_main.xml


Create an activity_scanned_barcode.xml layout and add the following code. The SurfaceView widget is used for camera source.

activity_scanned_barcode.xml


Create an activity_email.xml layout to perform email action.

activity_email.xml


Add the Google Mobile Vision API in build.gradle file.

build.gradle


In the MainActivity.java file, add the following code. By clicking the button btnScanBarcode it call the ScannedBarcodeActivity.java class.

MainActivity.java


In the ScannedBarcodeActivity.java activity class, add the following code. This class scans the QR code through the camera. In this class, we will generate two QR code, one for web URL and another is for an email address. A QR code can be generated from any of QR code generator website.

ScannedBarcodeActivity.java


In the EmailActivity.java class, add the following code. This class performs the email sending task to the address mention in the QR code.

EmailActivity.java


Add the following code in AndroidMenifest.xml file.

AndroidMenifest.java

Output:

Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner Android QR Code or Bar Code Scanner
Next TopicRSS Feed Reader