Barcode scanner component in Salesforce lightning – Video

Recording of Live code – Creating Barcode decoder component using Javascripyt library in Salesforce Lightning

This post is rewritten of previous post for decoding Barcode using JavaScript in Salesforce1 mobile application. Assuming you already have static resource of javascript library  QuaggaJS (we need only js file from dist folder).

This code was written live on my streaming channel on 10-Jul-2015, You can find recording here.

Barcode Scanner Lightning App
Barcode Scanner Lightning App

I have not added capability for image preview like I did in previous did, but it can be easily done by referring this blog post from Peter Knolle.

BarcodeApp.app

<aura:application >
    <ltng:require scripts="/resource/Quaggajs" />
    <c:BarcodeScanner />
</aura:application>

BarcodeScanner.cmp

<aura:component >
    <aura:attribute name="codeOutput" type="String" />

    <input type="file" accept="image/*;capture=camera" aura:id="file" />
    <ui:button label="Scan" press="{!c.scan}"/>
    <br />
    Output : <ui:outputText value="{!v.codeOutput}" />

</aura:component>

BarcodeScannerController.js

({
	scan : function(component, event, helper) { 

        var fileInput = component.find("file").getElement();
        var file = fileInput.files[0] ; 

        var App = {
            init: function() {
                App.attachListeners();
            } ,
            attachListeners: function() {
                 if(file)
                 {
                     var imgURL = URL.createObjectURL(file);
					 App.decode(imgURL);
                 }else{
                     component.set("v.codeOutput","Error !!! Select file to scan") ;
                 }
            },
            decode: function(src) {
                Quagga.decodeSingle(
                    {
                      inputStream: {
                        size: 640,
                        singleChannel: false
                      },
                      locator: {
                        patchSize: "large",
                        halfSample: false
                      },
                      decoder: {
                        readers: ["upc_reader", "code_128_reader", "code_39_reader", "code_39_vin_reader", "ean_8_reader", "ean_reader", "upc_e_reader", "codabar_reader"]
                    },
                      locate: true,
                      src: src
                    },
                 function(result){
                      if(result && result.codeResult && result.codeResult.code)
                      {
                        component.set("v.codeOutput",result.codeResult.code) ;
                      }else{
                        component.set("v.codeOutput","Error !!! Unable to read Barcode") ;
                      }
                    });
            }
        };
        App.init();

	}
})

Related posts

7 thoughts on “Barcode scanner component in Salesforce lightning – Video”

  1. Hi Jitendra, Thanks for excellent article. I’m using IOS which seems to be working ok using camera, but it it taking too long to scan the code. Do you have any recommendations to make this process faster?.

  2. Hi Jitendra,

    I got two error. ”
    1. Uncaught ReferenceError: Quagga is not defined”.
    2. Barcode_quaggaJS:1 Uncaught SyntaxError: Unexpected token !

    Please help me, how to resolve it.

    Thank you.

  3. Hi Jitendra,

    Thanks for this example! I have your previous version of the code (using VF instead of LightningComponents) but I can’t get the Lightning version to work. I have added the Quagga js file and it’s working fine with my VF page, but I get the following error when trying to scan:

    This page has an error. You might just need to refresh it.
    Action failed: c$BarcodeScanner$controller$scan [Failed to construct ‘Image’: Please use the ‘new’ operator, this DOM object constructor cannot be called as a function.]
    Failing descriptor: {c$BarcodeScanner$controller$scan}

    1. hey i was not able to upload “Quaggajs” file in static resource . Can you please tell me how you uploaded

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.