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:
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.
Create an activity_main.xml and add the following code.
Create an activity_scanned_barcode.xml layout and add the following code. The SurfaceView widget is used for camera source.
Create an activity_email.xml layout to perform email action.
Add the Google Mobile Vision API in build.gradle file.
In the MainActivity.java file, add the following code. By clicking the button btnScanBarcode it call the ScannedBarcodeActivity.java class.
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.
In the EmailActivity.java class, add the following code. This class performs the email sending task to the address mention in the QR code.
Add the following code in AndroidMenifest.xml file.