|
| 1 | +# Dynamsoft JavaScript Barcode SDK for Node |
| 2 | + |
| 3 | +> 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](https://github.com/dynamsoft-dbr/javascript-barcode/?utm_source=github&package=js). |
| 4 | +
|
| 5 | +Both 1D and 2D barcode symbiology are supported including the popular `Code 39`, `EAN-13`, `QR`, `PDF417`, etc.+ Find the full list [here](https://www.dynamsoft.com/barcode-reader/overview/?utm_source=github&package=js). |
| 6 | + |
| 7 | +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](#how-to-use-the-library-in-nodejs-lts-8). That said, Node.js version >= LTS 12 is recommended because the library will try to use `worker_threads` when decoding. |
| 8 | + |
| 9 | +> Also see [Dynamsoft JavaScript Barcode SDK for Web](https://github.com/Dynamsoft/javascript-barcode/blob/master/README.md). |
| 10 | +
|
| 11 | +## Get Started |
| 12 | + |
| 13 | +* Check your Node.js version |
| 14 | + |
| 15 | +```shell |
| 16 | +> node -v |
| 17 | +v12.13.1 |
| 18 | +``` |
| 19 | + |
| 20 | +* Installs the library from npm |
| 21 | + |
| 22 | +```shell |
| 23 | +> npm install dynamsoft-javascript-barcode --save |
| 24 | +``` |
| 25 | +* Create a `js` file and include the library |
| 26 | + |
| 27 | +```js |
| 28 | +let DBR = require("dynamsoft-javascript-barcode"); |
| 29 | +``` |
| 30 | + |
| 31 | +The following also works |
| 32 | +```js |
| 33 | +let DBR = require("path/to/dist/dbr.js"); |
| 34 | +``` |
| 35 | + |
| 36 | +> **Note** |
| 37 | +> The library uses `Promise` a lot, so it's recommended to write the related code in a `async` function so that later you can use `await` |
| 38 | +> |
| 39 | +> ```js |
| 40 | +> (async()=>{ |
| 41 | +> // many work will done here |
| 42 | +> })(); |
| 43 | +> ``` |
| 44 | +
|
| 45 | +* Create an instance of the reader |
| 46 | +
|
| 47 | +```js |
| 48 | +let reader = await DBR.BarcodeReader.createInstance(); |
| 49 | +``` |
| 50 | +
|
| 51 | +* Decode a file by its path |
| 52 | + |
| 53 | +```js |
| 54 | +let results = await reader.decode('path/to/sample.png'); |
| 55 | +``` |
| 56 | + |
| 57 | +Or just decode a file by its URL |
| 58 | + |
| 59 | +```js |
| 60 | +let results = await reader.decode('https://demo.dynamsoft.com/barcode-reader/img/AllSupportedBarcodeTypes.png'); |
| 61 | +``` |
| 62 | +> **NOTE** |
| 63 | +> The following image formats are supported by default: `png`, `jpg`, `bmp`, `gif`. |
| 64 | +> |
| 65 | +> If you want to decode other files like `pdf`'s, you need to convert them to images first. Contact [Dynamsoft Support](https://www.dynamsoft.com/company/contact/?utm_source=github&package=js) to find out more. |
| 66 | +> |
| 67 | +> If you want to decode raw image data (`RGBA`) from sources like a camera. You can use the API `deocdeBuffer`. Check out [C++ API decodeBuffer](https://www.dynamsoft.com/barcode-reader/programming/cplusplus/api-reference/cbarcodereader-methods/decode.html?ver=latest&utm_source=github&package=js#decodebuffer) for more details. |
| 68 | +
|
| 69 | +* Print out the results |
| 70 | + |
| 71 | +```js |
| 72 | +for(let result of results){ |
| 73 | + console.log(result.barcodeText); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +* Run your code. |
| 78 | + |
| 79 | +```shell |
| 80 | +> node your-code.js |
| 81 | +``` |
| 82 | + |
| 83 | +Last not but least, don't forget to set a `productKey`! If you don't have a key yet, click [here](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&package=js&utm_source=github) to get one. |
| 84 | + |
| 85 | +```js |
| 86 | +DBR.BarcodeReader.productKeys = 'PRODUCT-KEYS'; |
| 87 | +``` |
| 88 | + |
| 89 | +**Full code** |
| 90 | + |
| 91 | +```js |
| 92 | +let DBR = require('dynamsoft-node-barcode'); |
| 93 | +// Please visit https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&package=js&utm_source=github to get a trial license |
| 94 | +DBR.BarcodeReader.productKeys = 'PRODUCT-KEYS'; |
| 95 | + |
| 96 | +let pReader = null; |
| 97 | +(async()=>{ |
| 98 | + let reader = await DBR.BarcodeReader.createInstance(); |
| 99 | + for(let result of await reader.decode('https://demo.dynamsoft.com/barcode-reader/img/AllSupportedBarcodeTypes.png')){ |
| 100 | + console.log(result.barcodeText); |
| 101 | + } |
| 102 | + reader.destroy(); |
| 103 | + |
| 104 | + // Since the worker keep alive, you can call |
| 105 | + await DBR.BarcodeReader._dbrWorker.terminate(); |
| 106 | + // when you need to exit this process. |
| 107 | + // Or call |
| 108 | + process.exit(); |
| 109 | + // directly. |
| 110 | +})(); |
| 111 | + |
| 112 | +``` |
| 113 | + |
| 114 | +## Change Decoding Settings |
| 115 | + |
| 116 | +To set up the library for decoding, use the APIs `getRuntimeSettings` & `updateRuntimeSettings`. |
| 117 | + |
| 118 | +```js |
| 119 | +await barcodeScanner.updateRuntimeSettings("speed"); |
| 120 | +``` |
| 121 | +```js |
| 122 | +await barcodeScanner.updateRuntimeSettings("balance"); |
| 123 | +``` |
| 124 | +```js |
| 125 | +await barcodeScanner.updateRuntimeSettings("coverage"); |
| 126 | +``` |
| 127 | +```js |
| 128 | +let settings = await reader.getRuntimeSettings(); |
| 129 | +settings.localizationModes = [ |
| 130 | + Dynamsoft.DBR.EnumLocalizationMode.LM_CONNECTED_BLOCKS, |
| 131 | + Dynamsoft.DBR.EnumLocalizationMode.LM_SCAN_DIRECTLY, |
| 132 | + Dynamsoft.DBR.EnumLocalizationMode.LM_LINES, 0, 0, 0, 0, 0]; |
| 133 | +settings.deblurLevel = 2; |
| 134 | +await reader.updateRuntimeSettings(settings); |
| 135 | +``` |
| 136 | + |
| 137 | +See [Barcode reading settings Guide](https://www.dynamsoft.com/barcode-reader/programming/cplusplus/user-guide.html?ver=latest#use-publicruntimesettings-struct-to-change-settings?utm_source=github&package=js) for basic usage. |
| 138 | + |
| 139 | +See [C++ API RuntimeSettings](https://www.dynamsoft.com/barcode-reader/programming/c-cplusplus/struct/PublicRuntimeSettings.html?utm_source=github&package=js) for more details. |
| 140 | + |
| 141 | +To find out which settings best suit your usage scenario, visit [DBR Main Online Demo](https://demo.dynamsoft.com/barcode-reader/?utm_source=github&package=js). |
| 142 | + |
| 143 | +Any questions, please contact [Dynamsoft support](https://www.dynamsoft.com/company/contact/?utm_source=github&package=js). |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +## How to use the library in Node.js LTS 8 |
| 148 | + |
| 149 | +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. |
| 150 | + |
| 151 | +**Decode** |
| 152 | + |
| 153 | +```js |
| 154 | +var dbr = require('path/to/dist/dbr-<version>.node.wasm.js'); |
| 155 | +dbr.onRuntimeInitialized = ()=>{ |
| 156 | + dbr.BarcodeReaderWasm.init('{"productKeys":"PRODUCT-KEYS"}'); |
| 157 | + var reader = new dbr.BarcodeReaderWasm(false,-1); |
| 158 | + var fs = require('fs'); |
| 159 | + var img = fs.readFileSync('./sample.png'); |
| 160 | + var resultsInfo = JSON.parse(reader.decodeFileInMemory(new Uint8Array(img))); |
| 161 | + console.log(resultsInfo); |
| 162 | +}; |
| 163 | +``` |
| 164 | + |
| 165 | +**Change settings** |
| 166 | + |
| 167 | +```js |
| 168 | +var settings = JSON.parse(reader.getRuntimeSettings()); |
| 169 | +settings.expectedBarcodesCount = 999; |
| 170 | +reader.updateRuntimeSettings(JSON.stringify(settings)); |
| 171 | +``` |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
0 commit comments