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.

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


<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}" />



	scan : function(component, event, helper) { 

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

        var App = {
            init: function() {
            } ,
            attachListeners: function() {
                     var imgURL = URL.createObjectURL(file);
                     component.set("v.codeOutput","Error !!! Select file to scan") ;
            decode: function(src) {
                      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
                      if(result && result.codeResult && result.codeResult.code)
                        component.set("v.codeOutput",result.codeResult.code) ;
                        component.set("v.codeOutput","Error !!! Unable to read Barcode") ;


Related posts

11 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.

    1. Hi Sanjeev,
      Were you able to resolve this issue? I’m currently running into a similar error that says “ReferenceError: Quagga is not defined”

  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

  4. I had been train this code didn’t work, I try the other version for vf page and some time work and the other say unable to read for the same bar code???
    Jitendra do you have any updated version ?

  5. Hi,

    I am not able to upload the zip file to static resource as the file size is more than 10 mb.
    Can you suggest?


Leave a Reply

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