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.

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(); } })
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?.
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.
Please make sure this library is added and accessible in code – http://serratus.github.io/quaggaJS/
Hi Sanjeev,
Were you able to resolve this issue? I’m currently running into a similar error that says “ReferenceError: Quagga is not defined”
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}
I am also getting this error. Any ideas?
hey i was not able to upload “Quaggajs” file in static resource . Can you please tell me how you uploaded
Hi Ji!
I am getting CSP error for Quaggajs. Pls post me n how to fix it.
This code will work with laptop camara?
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 ?
Regards
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?
Thanks