SAP Fiori Client Guide

Barcode Scanner Samples

Describes sample barcode scanner projects.

The following sample configures the namespace, and calls the scan method to launch the scanner, and upon detection of a barcode, pass control back to your application, along with the barcode data. For Android only, the encode method encodes a supported type of information (in this sample, text) as a barcode. For a complete Barcode Scanner sample, see Getting Started with KapselInformation published on SAP site.

document.addEventListener("deviceready", init, false);   
function scan() {
  log("scanning");
  cordova.plugins.barcodeScanner.scan(scanSuccessCallback, scanErrorCallback);
}

function scanSuccessCallback(result) {
  log(JSON.stringify(result));
  /*
  alert("We got a barcode\n" +
  "Result: " + result.text + "\n" +
  "Format: " + result.format + "\n" +
  "Cancelled: " + result.cancelled);
   */
}

function scanErrorCallback(error) {
  alert("Scanning failed: " + JSON.stringify(error));
}
        
function encode() {
  log("encoding");
  if (device.platform == "Android") {  //Not supported on iOS
    var stringToEncode = "http://www.sap.com";
    cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, stringToEncode, encodeSuccessCallback, encodeErrorCallback);
  }
  else {
    log("Encoding is not supported on iOS.  See https://github.com/wildabeast/BarcodeScanner/issues/106");	
  }
}

 function encodeSuccessCallback(result) {
   log(JSON.stringify(result));
 }

 function encodeErrorCallback(error) {
   alert("Encoding failed: " + JSON.stringify(error));
 }
            
 function log(line) {
   var results = document.getElementById("scan_results");
   results.innerHTML+= "<br>" + line;
 }

To execute the sample:

  1. Create a new project and add the Barcode Scanner plugin.
  2. Copy the sample code into the application's www folder.
  3. Run the sample application using the Cordova CLI.
  4. Click the scan button. You see the device's camera view for scanning a barcode.
  5. Position the camera's viewfinder over the barcode. The sample application scans the barcode, captures it and reads the data, and a dialog box appears with the value from the barcode.