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:
- Static Image
- 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
- npm install quagga
Import the reference of Quagga.js in the file where we need to scan the barcode.
- 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.
- <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
- Quagga.init({
- inputStream: {
- name: "Live",
- type: "LiveStream",
- target: document.querySelector('#liveStreamElement'),
- constraints: {
- width: 320,
- height: 240,
- facingMode: "environment"
- }
- },
- locator: {
- patchSize: "x-large",
- halfSample: true
- },
- locate: true,
- decoder: {
- readers: ["code_128_reader"]
- }
- }, (err) => {
- if (err) {
- return true;
- }
- console.log("Initialization finished. Ready to start");
- Quagga.start();
- });

S BrettPosted Sep 11, 2020, 10:40 AM
Where does the Quagga.init() and subsequent code live?
Rikam PalkarPosted May 4, 2020, 12:04 PM
Good one. Appreciate it.
Vijai Anand RamalingamPosted May 4, 2020, 9:48 AM
Nice one Link!! Keep contributing..