Scan Barcode In SPFx WebPart

Overview

 
This blog will help you use Quagga.js in the SPFx webpart to scan barcodes and retrieve barcode data.

Quagga.js works with all modern browsers except IE 11.
 
Quagga.js helps us to scan the barcode from two different sources:
  1. Static Image
  2. Live Stream
For more details on Quagga.js check out this link.
 
For the below demo we would use the Live Stream source which would use our device's camera to scan the barcode
 
To download the Code,

Installation

 
To install Quagga.js in SPFx solution please use the below command
  1. npm install quagga  
 
Import the reference of Quagga.js in the file where we need to scan the barcode.
  1. import Quagga from 'quagga';   
If you are using React.js as the framework add the below code in the render method, otherwise if you are using any other framework add in the html section.
  1. <div id="liveStreamElement"></div>  
Use the below code to initialize and start the scan. This would start the camera and start the scanning of the barcode
  1. Quagga.init({  
  2.       inputStream: {  
  3.         name: "Live",  
  4.         type: "LiveStream",  
  5.         target: document.querySelector('#liveStreamElement'),  
  6.         constraints: {  
  7.           width: 320,  
  8.           height: 240,  
  9.           facingMode: "environment"  
  10.         }  
  11.       },  
  12.       locator: {  
  13.         patchSize: "x-large",  
  14.         halfSample: true  
  15.       },  
  16.       locate: true,  
  17.       decoder: {  
  18.         readers: ["code_128_reader"]  
  19.       }  
  20.     }, (err) => {  
  21.       if (err) {  
  22.         return true;  
  23.       }  
  24.       console.log("Initialization finished. Ready to start");  
  25.       Quagga.start();  
  26.     });  
Note
We can change the decoder reader's values -- currently the decoding is allowed for the below formats:

EAN, CODE 128, CODE 39, EAN 8, UPC-A, UPC-C, I2of5, 2of5, CODE 93 and CODABAR.
 
For displaying the borders when we detect the barcode use the below code:
  1. Quagga.onProcessed((result) => {  
  2.         var drawingCtx = Quagga.canvas.ctx.overlay,  
  3.           drawingCanvas = Quagga.canvas.dom.overlay;  
  4.         if (result) {  
  5.           if (result.boxes) {  
  6.             drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));  
  7.             result.boxes.filter((box) => {  
  8.               return box !== result.box;  
  9.             }).forEach((box) => {  
  10.               Quagga.ImageDebug.drawPath(box, { x: 0, y: 1 }, drawingCtx, { color: "green", lineWidth: 2 });  
  11.             });  
  12.           }  
  13.           if (result.box) {  
  14.             Quagga.ImageDebug.drawPath(result.box, { x: 0, y: 1 }, drawingCtx, { color: "#00F", lineWidth: 2 });  
  15.           }  
  16.           if (result.codeResult && result.codeResult.code) {  
  17.             Quagga.ImageDebug.drawPath(result.line, { x: 'x', y: 'y' }, drawingCtx, { color: 'red', lineWidth: 3 });  
  18.           }  
  19.         }  
  20.       });  
Once the barcode is detected, to get the result use the below method:
  1. Quagga.onDetected((val) => {  
  2.       alert(val.codeResult.code);  
  3.     });  
To stop the scanning use the below code
  1. Quagga.stop();  
Demo