SAP Mobile App Development

Barcode scanner using device camera in SAPUI5 hybrid app

         This is a step by step tutorial showing how to use your device camera as a barcode scanner in  SAPUI5 Hybrid Mobile App with SAP Web IDE and hybrid app toolkit.We make use of kapsel barcode scanner plugin.This plugin enables Hybrid SDK applications to scan and decode a barcode using the device camera.The plugin support android and ios platforms.For more information and list of supported barcode types visit

       We will continue with our previous tutorial app here How to Create SAPUI5 Hybrid Mobile App with SAP Web IDE?.


  1. Windows system setup with SAP Mobile Platform SDK installed and Hybrid App Toolkit configured.(If not setup already visit my  blog post Windows System Setup for Developing Mobile Apps with SAPWebIDE and SAP Mobile SDK )
  2. Created basic SAPUI5 Hybrid Mobile App(If you haven't created one yet follow previous blog How to Create SAPUI5 Hybrid Mobile App with SAP Web IDE).


  1. Start HAT Connector ,Open SAP Web IDE and check HAT connectity is avialable.If not familiar follow pre-requisites 2 section.
  2. Right click on project folder and choose Project Settings.Go to Hybrid App Toolkit->Hybrid App Configuration.Under Plugins section tick mark Kapsel Barcode Scanner plugin and press save button.
  3. Time to code.Open View1.view.xml file(Your file name may be different if you have changed name of view).In the view add two sap.m.Button controls.One for scanning barcode and one for encoding a predefined text to QR code.Then add one sap.m.Textcontrol to show the scanned barcode value.My view code is below.The button's press event is used for calling the scan and encode methods of Kapsel Barcode Scanner api.

  4. <core:View
     xmlns="sap.m" controllerName="myFirstMobileApp.view.View1"
     <Page title="BarCodeReader">
       <HBox justifyContent="SpaceAround">
         <Button text="Scan" press="onScan"></Button>
         <Button text="Encode" press="onEncode"></Button>
       <HBox width="100%" justifyContent="Center">
         <Text text="" id="myBarCode"></Text>

  5. Open View1.controller.js.Inside onScan method  we will call scan method of cordova barcode scanner plugin.Below is my controller logic.In the success handler set the Text control's text attribute by using setText() method.

  6. sap.ui.controller("myFirstMobileApp.view.View1", {
        oView: null,
        onInit: function() {
            oView = this.getView();
        onScan: function() {
            var options = {
                preferFrontCamera: false, // iOS and Android
                showFlipCameraButton: true, // iOS and Android
                showTorchButton: true, // iOS and Android
                torchOn: true, // Android, launch with the torch switched on (if available)
                prompt: "Place barcode inside the scan area", // Android
                resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
                formats: "QR_CODE,PDF_417,UPC_A", // default: all but PDF_417 and RSS_EXPANDED
                orientation: "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
                disableAnimations: true // iOS
            cordova.plugins.barcodeScanner.scan(this.onScanSuccess, this.onScanError, options);
        onScanSuccess: function(result) {
            var oText = oView.byId("myBarCode");
            oText.setText("barcode is " + result.text + " and format is " + result.format);
        onScanError: function(error) {
            alert("error" + JSON.stringify(error));
        onEncode: function() {
            cordova.plugins.barcodeScanner.encode(cordova.plugins.barcodeScanner.Encode.TEXT_TYPE, "", this.onEncodeSuccess,
        onEncodeSuccess: function(success) {
            alert("encode success: " + JSON.stringify(success));
        onEncodeFail: function(fail) {
            alert("encoding failed: " + JSON.stringify(fail));

  7. To test the App Right Click ->Run->Run on Local Device/Simulator->Android Device.Testing on emulator may not work properly because of lack of camera in emulator.
  8. See screenshots taken before and after scanning a barcode and see generated QRcode also.

Note:The encode method's success handler method is never called.We can just see the generated QR code image only.Since the success handler is never called I was not able to get the generated image in my app.This may be a bug of cordova barcode scanner plugin.
If you enjoyed this post, Please Share!!


  1. Hi,

    I am able to use barcode scanner in webide. But when i deploy the application to Fiori launchpad, cordova is undefined.

    can anyone help :(


    1. Try using it with fiori client.The plugin will work only in mobile devices.

  2. they can be utilized to check material of changing sizes and thicknesses - from little postage stamps to vast mechanical diagrams and 3D objects.

  3. I find scanning a tedious process. Well, more the processing afterwards than the actual scanning. My scanner never gets the colour anywhere near right, and no matter how much I clean they always end up covered in dust and hairs, leading to hours in Photoshop for each one. It's also pretty subjective scanning Polaroids, the film scanner

  4. Superb examining .Thanks for sharing. It's late finding this show up. scanning

  5. Nice article, which you have described very well about the Barcode. Your article is very useful for those who are looking to buy barcode. thanks for sharing. Buy Ean Barcodes Online

  6. There are so many types of scanner available. The 3D laser scanner is the latest scanner that used to capture the three-dimensional data an object using laser light. This scanner can capture data so accurately. Best 3D laser scanner Grand Prairie, Alberta

  7. What is this magic marketing bullet you ask? Its common moniker is a "QR code," and it is essentially a two-dimensional bar code much like the bar codes that have become so ubiquitous in the retail world. QR Code Creator

  8. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. blackmart alpha apk free download for android

  9. It’s amazing in support of me to truly have a blog site, which will be valuable meant for my knowledge. Thanks admin.Small Business ERP Software

  10. The ideas you provided allow me to share extremely precious. It turned out this sort of pleasurable surprise to obtain that anticipating me whenever i awoke today. They can be constantly to the issue and to recognise. Thanks quite a bit for that valuable ideas you’ve got shared listed here. device mockup

  11. Great weblog right here! after reading, i decide to buy a sleeping bag ASAP iphone mockup


Powered by Blogger.