Examples
(click to expand) Node.js
The example below shows how to use our barcode decoder WebAssembly outside of Internet browser using only JavaScript.
The complete source code of this Node.js file you can download here.
Also you need to download "datasymbol-sdk.js" and "datasymbol-sdk.wasm" file.
The complete source code of this Node.js file you can download here.
Also you need to download "datasymbol-sdk.js" and "datasymbol-sdk.wasm" file.
const fs = require('fs');
const canvas = require('canvas');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
//if .wasm file placed in not current folder
//var DEF_WASM_PATH = "/home/john/Desktop/nodejs/datasymbol-sdk.wasm";
var Module = {
decHandle: 0,
locateFile: function (fName) {
if ((fName == 'datasymbol-sdk.wasm') && (typeof DEF_WASM_PATH !== 'undefined'))
return DEF_WASM_PATH;
return fName;
},
onDataSymbolWASMReady: function (hDecoder) {
decHandle = hDecoder;
DecodeImageFile("/home/john/Desktop/nodejs/linear-4.jpg");
},
onRuntimeInitialized: function () {
Module.ccall('InitLib', 'number', ['array'], [str2AsciiArr("")] );
},
onAbort: function () {
console.log("onModuleAbort");
},
};
// include JS file
eval(fs.readFileSync('datasymbol-sdk.js')+'');
function str2AsciiArr(str) {
var byteStr = new Uint8Array(str.length + 1);
for (var i = 0; i < str.length; ++i) {
var charCode = str.charCodeAt(i);
if (charCode <= 0xFF)
byteStr[i] = charCode;
}
byteStr[i] = 0x00;
return byteStr;
}
function SetProperty(decHandle, propName, propVal) {
return Module.ccall('setProperty',
'number',
['number', 'array', 'array'],
[decHandle, str2AsciiArr(propName), new Uint8Array(new Uint32Array([propVal]).buffer)]);
}
function DecodeFrame(decHandle, pFrameBuf, width, height) {
//decode and collect barcodes in "barcodeResult"
var barcodeResult = [];
var res = _DecodeRGBA(decHandle, pFrameBuf, width, height);
var bufLen = width * height * 4;
if (res == 0) {
var numBarodes = _getResNum(decHandle);
for (var i = 0; i < numBarodes; ++i) {
var barcode = {};
var dataLen = _getResData(decHandle, pFrameBuf, bufLen, i);
if (dataLen >= 0) {
//barcode data
barcode.data = [];
for (var j = 0; j < dataLen; j++)
barcode.data.push(Module.HEAPU8[pFrameBuf + j]);
//collect points
barcode.points = [];
res = _getResPoints(decHandle, pFrameBuf, bufLen, i);
for (var j = 0; j < 4; j++)
barcode.points.push( { x: Module.getValue(pFrameBuf + j * 8, 'i32'), y: Module.getValue(pFrameBuf + j * 8 + 4, 'i32') } );
//type
barcode.bt = _getResBarcodeType(decHandle, i);
barcodeResult.push(barcode);
}
}
}
return barcodeResult;
}
function DecodeImageFile(imageSrc) {
//read image file
const fileData = fs.readFileSync(imageSrc, {flag:'r'});
var img = new canvas.Image();
img.src = fileData;
//draw image into the canvas and get RGBA data
var cnv = new canvas.Canvas(img.width, img.height);
var ctx = cnv.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
var imageData = ctx.getImageData(0, 0, img.width, img.height);
//set any barcode reader properties
var res = SetProperty(decHandle, 'uiLinearFindBarcodes', 4);
//allocate frame buffer
var bufLen = img.width * img.height * 4;
var pFrameBuf = _malloc(bufLen);
//decode frame
for( var i=1; i <= 3; ++i ) {
console.log(i + ':');
Module.HEAPU8.set(imageData.data, pFrameBuf);
var barcodeResult = DecodeFrame(decHandle, pFrameBuf, img.width, img.height);
//print result
console.log('Decoded: ' + barcodeResult.length);
for( var n=0; n < barcodeResult.length; ++n )
console.log( String.fromCharCode.apply(null, barcodeResult[n].data) );
console.log('');
}
//free resources
_FreeLib(decHandle);
if (pFrameBuf)
_free(pFrameBuf);
}
DataSymbol WebAssembly Methods
Initialize WebAssembly library. Syntax Parametersobject.InitLib(pszKey) Return Value long. Returns the HANDLE of barcode decoder. You should to save this value to use it in future. If returns NULL then SDK cannot be initialized.Remarks This method should be called before any else. To free the library you should to call FreeLib |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Release WebAssembly library. Syntax Parametersobject.FreeLib(decHandle) Return Value integer. Returns the Error Code. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Decodes the RGBA encoded image. Syntax Parametersobject.DecodeRGBA(decHandle, pRGBA, width, height) Return Value integer. Returns the Error Code. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Decodes the gray map. Syntax Parametersobject.DecodeGraymap(decHandle, pImg, width, height) Return Value integer. Returns the Error Code.Remarks A gray map is a byte matrix. Each byte has the value from 0 to 255 and represents one image pixel. 0 means a black pixel, 255 means a white pixel. The matrix is passed to the DecodeGrayMap method as a one-dimensional array. The first upper line of the image is sent first, then comes the second line and so on. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Decodes the PGM (portable gray map) file. Syntax Parametersobject.DecodePGM(decHandle, pImg, width, height) Return Value integer. Returns the Error Code. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sets the various properties of Barcode Decoder. Syntax Parametersobject.setProperty(decHandle, pszPropName, pData) Return Value integer. Returns the Error Code. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets the number of decoded barcodes. Syntax Parametersobject.getResNum(decHandle) Return Value integer. If less than 0 then it returns the Error Code, else the barcode number. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets the barcode type. Syntax Parametersobject.getResBarcodeType(decHandle, barcodeNum) Return Value integer. If less than 0 then it returns the Error Code, else the barcode type. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets the decoding quality. Syntax Parametersobject.getResDQ(decHandle, barcodeNum) Return Value integer. If less than 0 then it returns the Error Code, else the decoding quality (0...100). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets the barcode data. Syntax Parametersobject.getResData(decHandle, pOutBuf, bufLen, barcodeNum) Return Value integer. If less than 0 then it returns the Error Code, else the barcode data. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Gets the barcode corners coordinates. Syntax Parametersobject.getResPoints(decHandle, pOutBuf, bufLen, barcodeNum) Return Value integer. Returns the Error Code.Remarks Method copies in the pOutBuf 8 integer (32 bit) values. 0 value - x1 coordinate, 1 value - y1 coordinate, 2 value - x2 coordinate, 3 value - y2 coordinate, etc. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|