How to Wrap Dynamsoft Linux Barcode SDK for Node.js
From version 4.x to 5.x, Dynamsoft improved Barcode SDK performance with a big leap. Unfortunately, only Windows edition is available for download so far. In this post, I will share how to create a Node.js extension with the preview version of Dynamsoft Linux Barcode SDK 5.2. If you are interested in Windows edition, please read - Building Node.js Barcode Addon with DBR v5.0.
Linux Environment: Ubuntu on Windows
If you are using Windows 10, Virtual Machine is not necessary for Linux development. I’d like to use the Linux subsystem installed from Windows Store:
Environment Configuration
Open cmd.exe > bash. Install essential development tools ‘make’ and ‘g++’:
sudo apt-get install make g++
Download Node.js Linux binary (x64) and extract the package:
tar -xvf node-v8.4.0-linux-x64.tar.xz
Export Node.js bin path in ~/.bashrc:
export PATH=/mnt/f/zip/node-v8.4.0-linux-x64/bin:$PATH
Make the change work:
source ~/.bashrc
Extract dbr_linux_5.2.tar.gz:
tar -xvf dbr_linux_5.2.tar.gz
Create a symlink for libDynamsoftBarcodeReaderx64.so:
sudo ln -s <Your PATH>/libDynamsoftBarcodeReaderx64.so /usr/lib/libDynamsoftBarcodeReader.so
Install node-gyp:
npm install -g node-gyp
Node.js Extension
Let’s get started with binding.gyp:
{
"targets": [
{
'target_name': "dbr",
'sources': [ "dbr.cc" ],
'conditions': [
['OS=="linux"', {
'defines': [
'LINUX_DBR',
],
'libraries': [
"-lDynamsoftBarcodeReader"
]
}]
]
}
]
}
In this file, we define the module name, source code files, and libraries. To make -lDynamsoftBarcodeReader work, please ensure that you have generated the symlink (/usr/lib/libDynamsoftBarcodeReader.so) for libDynamsoftBarcodeReaderx64.so beforehand.
Create dbr.cc. The code is same to Windows edition.
Configure building environment:
node-gyp configure
Build the project:
node-gyp build
Import the module to Node.js app:
var dbr = require('./build/Release/dbr');
var fs = require('fs');
var barcodeTypes = 0x3FF | 0x2000000 | 0x8000000 |
0x4000000; // 1D, QRCODE, PDF417, DataMatrix
function decodeFileStreamAsync(fileName) {
let stats = fs.statSync(fileName);
let fileSize = stats["size"];
fs.open(fileName, 'r', function(status, fd) {
if (status) {
console.log(status.message);
return;
}
let buffer = new Buffer(fileSize);
fs.read(fd, buffer, 0, fileSize, 0, function(err, bytesRead, data) {
dbr.decodeFileStreamAsync(buffer, fileSize, barcodeTypes, function(msg) {
console.log(fileName);
let result = null;
for (index in msg) {
result = msg[index];
console.log("Format: " + result['format']);
console.log("Value : " + result['value']);
}
console.log("Done............................................................\n");
});
});
});
}
dbr.initLicense(
"t0068MgAAAGvV3VqfqOzkuVGi7x/PFfZUQoUyJOakuduaSEoI2Pc8+kMwjrojxQgE5aJphmhagRmq/S9lppTkM4w3qCQezxk=");
Run a test:
node test.js -f test.tif
How to Get the Linux Barcode SDK
Source Code
https://github.com/dynamsoft-dbr/linux-barcode-sdk-node-wrapper