This library is the Node.js edition of Dynamsoft Barcode Reader. If you are looking to implement barcode reading feature in a web page, please check out the other library Dynamsoft JavaScript Barcode SDK for Web.
Both 1D and 2D barcode symbiology are supported including the popular Code 39, EAN-13, QR, PDF417, etc.+ Find the full list here.
The library is based on webassembly which has been an official feature of Node.js since LTS 8. If you are using Node.js LTS 8 and have no plan to upgrade it, check out how to use the library in Node.js LTS 8. That said, Node.js version >= LTS 12 is recommended because the library will try to use worker_threads when decoding.
- Check your Node.js version
> node -v
v12.13.1- Installs the library from npm
> npm install dynamsoft-node-barcode --save- Create a
jsfile and include the library
letDynamsoft=require("dynamsoft-node-barcode");The following also works
letDynamsoft=require("path/to/dist/dbr.js");Note The library uses
Promisea lot, so it's recommended to write the related code in aasyncfunction so that later you can useawait(async()=>{// many work will done here})();
- Create an instance of the reader
letreader=awaitDynamsoft.BarcodeReader.createInstance();- Decode a file by its path
letresults=awaitreader.decode('path/to/sample.png');Or just decode a file by its URL
letresults=awaitreader.decode('https://demo.dynamsoft.com/dbr/img/AllSupportedBarcodeTypes.png');NOTE
The following image formats are supported by default:png,jpg,bmp,gif.If you want to decode other files like
If you want to decode raw image data (
RGBA) from sources like a camera. You can use the APIdeocdeBuffer. Check out C++ API decodeBuffer for more details.
- Print out the results
for(letresultofresults){console.log(result.barcodeText);}- Run your code.
> node your-code.jsLast not but least, don't forget to set a productKey! If you don't have a key yet, click here to get one.
Dynamsoft.BarcodeReader.productKeys='PRODUCT-KEYS';Full code
letDynamsoft=require('dynamsoft-node-barcode');// Please visit https://www.dynamsoft.com/CustomerPortal/Portal/TrialLicense.aspx to get a trial licenseDynamsoft.BarcodeReader.productKeys='PRODUCT-KEYS';(async()=>{letreader=awaitDynamsoft.BarcodeReader.createInstance();for(letresultofawaitreader.decode('https://demo.dynamsoft.com/dbr/img/AllSupportedBarcodeTypes.png')){console.log(result.barcodeText);}reader.destroy();process.exit();})();To set up the library for decoding, use the APIs getRuntimeSettings & updateRuntimeSettings.
letsettings=awaitreader.getRuntimeSettings();settings.expectedBarcodesCount=999;awaitreader.updateRuntimeSettings();See Barcode reading settings Guide for basic usage.
See C++ API RuntimeSettings for more details.
To find out which settings best suit your usage scenario, visit DBR Main Online Demo.
Any questions, please contact Dynamsoft support.
Node.js LTS 8 doesn't support worker_threads, so the decoding will happen in the same main thread which means it's a blocking operation. The following code snippets demonstrate the basic usage.
Decode
vardbr=require('path/to/dist/dbr-<version>.node.wasm.js');dbr.onRuntimeInitialized=()=>{dbr.BarcodeReaderWasm.init('{"productKeys":"PRODUCT-KEYS"}');varreader=newdbr.BarcodeReaderWasm(false,-1);varfs=require('fs');varimg=fs.readFileSync('./sample.png');varresultsInfo=JSON.parse(reader.decodeFileInMemory(newUint8Array(img)));console.log(resultsInfo);};Change settings
varsettings=JSON.parse(reader.getRuntimeSettings());settings.expectedBarcodesCount=999;reader.updateRuntimeSettings(JSON.stringify(settings));