Barcode Scanner

The barcode scanner feature enables you to implement scanning of barcodes or QR codes through your mobile application.

Supported Platforms

This feature is supported in the following platforms:

  • iOS
  • Android

Installation

To install the barcode scanner plugin, execute the following command in your Cordova project directory:

cordova plugin add cordova-plugin-barcodescanner

Usage

Let us assume that there is a “BARCODE SCAN” button in your mobile app. When that button is clicked, the phone camera will open. The phone camera will act as the barcode scanner. The following is a sample code snippet that can be used in the listener JS file for invoking the barcode scanner in your mobile application:

// Sets the Barcode Scanner Handler in the Device Ready Event
canvas.env.scanner.setHandler(new canvas.env.handler.HybridScannerHandler());

// The following function opens the barcode scanner when the "BARCODE SCAN" button is clicked.
this.fm.registerHandler(CFEC.CLICK, "BARCODESCAN", function (fm, event, fieldName, value)
{
	canvas.env.scanner.scanBarCode(successCallback, errorCallback);
}

/* Here, CFEC.CLICK is the Canvas event constant for click action.
*  BARCODESCAN is the form item ID for the "BARCODE SCAN" button.
*  successCallback is the callback function of scanBarCode that returns the barcode content.
*  errorCallback is the callback function of scanBarCode that returns the failure message.
*/

// The following function is called when scanBarCode method is successfully executed.
function successCallback(result) 
{
    showAlert(result.text);
}

// The following function is called when scanBarCode method is not successfully executed.
function errorCallback(error) 
{
    showAlert("Could not scan barcode.");
}