Barcode Scanner Web SDK Assembly

Barcode Decoder WebAssembly (datasymbol-sdk.wasm file) provides the low level interface. You can write your own JavaScript code (instead our DSScanner) that works directly with WebAssembly.



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.


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

InitLib

Initialize WebAssembly library.

Syntax
object.InitLib(pszKey)

Parameters
pszKey Pointer to the null-terminated string with library key. If parameter is NULL then library will work in demo mode.
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

Calls "Module.onDataSymbolWASMReady" function when initialization has been finished



FreeLib

Release WebAssembly library.

Syntax
object.FreeLib(decHandle)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
Return Value
integer. Returns the Error Code.



DecodeRGBA

Decodes the RGBA encoded image.

Syntax
object.DecodeRGBA(decHandle, pRGBA, width, height)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pRGBA Pointer to the byte array containded RGBA image
width An integer value that defines the width of RGBA image
height An integer value that defines the height of RGBA image
Return Value
integer. Returns the Error Code.



DecodeGraymap

Decodes the gray map.

Syntax
object.DecodeGraymap(decHandle, pImg, width, height)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pImg Pointer to the byte array containded gray map.
width An integer value that defines the width of the gray map image
height An integer value that defines the height of gray map image
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.



DecodePGM

Decodes the PGM (portable gray map) file.

Syntax
object.DecodePGM(decHandle, pImg, width, height)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pPGM Pointer to the byte array containded the whole PGM file.
pgmLen An integer value that defines the PGM file length
Return Value
integer. Returns the Error Code.



setProperty

Sets the various properties of Barcode Decoder.

Syntax
object.setProperty(decHandle, pszPropName, pData)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pszPropName Pointer to the null-terminated string that defines the Propery name.
pData Pointer to byte array that defines the property value. Length of byte array depends from property.

Properties
Name Value
barcodeTypes 4 bytes. what types of barcodes should be decoded.
totalBarcodes 4 bytes. how many barcodes of all types to search for in the entire document.
Default value - 0 (unlimited)
frameTime 4 bytes. Time to decode 1 frame in milliseconds.
Default value - 0 (unlimited).
bCalcBlurring 4 bytes boolean. calculate the blurring value of decoded barcode (default - false).
scanDirection 4 bytes. Scanning direction, horizontaly (1) or verticaly (2). You can use it to reduce the barcode decoding time
Default value - 3 (horizontaly and verticaly).
uiLinearFindBarcodes 4 bytes. how many Linear barcodes to search
Default value - 1
bLinearVerifyCheckDigit 4 bytes boolean. Check the optional Linear check digit
Default value - 0 (false)
bLinearShowCheckDigit 4 bytes boolean. Show Linear check digit
Default value - 1 (true)
bLinearShowStartStop 4 bytes boolean. Show Linear check digit
Default value - 0 (false)
bLinearVerify 4 bytes boolean. Verify linear barcode after decoding. Improves reliability linear barcode reading.
Default value - False (don't verify).
bLinearCode39EnableExtended 4 bytes boolean. Decode Code 39 as Code 39 Extended or not.
Default value - 0 (false)
bLinearUPCE2UPCA 4 bytes boolean. Convert a UPC-E barcode to UPC-A or not.
Default value - 0 (false)
uiLinearInterl25MinLen 4 bytes. minimum length of an Interleaved 2/5 barcode
Default value - 2
uiLinearInterl25MaxLen 4 bytes. miximum length of an Interleaved 2/5 barcode
Default value - 50
uiLinearIndustr25MinLen 4 bytes. minimum length of an Industrial 2/5 barcode
Default value - 1
uiLinearIndustr25MaxLen 4 bytes. miximum length of an Industrial 2/5 barcode
Default value - 50
uiLinearCode11MinLen 4 bytes. minimum length of an Code 11 barcode
Default value - 1
uiLinearCode11MaxLen 4 bytes. miximum length of an Code 11 barcode
Default value - 50
uiLinearCode39MinLen 4 bytes. minimum length of an Code 39 barcode
Default value - 1
uiLinearCode39MaxLen 4 bytes. miximum length of an Code 39 barcode
Default value - 50
uiLinearCode128MinLen 4 bytes. minimum length of an Code 128 barcode
Default value - 1
uiLinearCode128MaxLen 4 bytes. miximum length of an Code 128 barcode
Default value - 50
uiLinearCodabarMinLen 4 bytes. minimum length of an Codabar barcode
Default value - 1
uiLinearCodabarMaxLen 4 bytes. miximum length of an Codabar barcode
Default value - 50
uiLinearCode93MinLen 4 bytes. minimum length of an Code 93 barcode
Default value - 1
uiLinearCode93MaxLen 4 bytes. miximum length of an Code 93 barcode
Default value - 50
LinearDecSpeed 4 bytes. Speed of Linear barcode decoder
Default value - 0
Possible values:
0 - Normal
1 - Fast
2 - Slow
uiPDF417FindBarcodes 4 bytes. how many PDF417 barcodes to search
Default value - 1
PDF417DecSpeed 4 bytes. Speed of PDF417 barcode decoder
Default value - 0
Possible values:
0 - Normal
1 - Fast
2 - Slow
bPDF417FindMicro 4 bytes boolean. Search PDF417 Micro barcodes
Default value - 0 (false)
bPDF417SupportECI 4 bytes boolean. Support PDF417 ECI
Default value - 0 (false)
bPDF417Robust 4 bytes boolean. Robust PDF417 decoding
Default value - 0 (false)
uiDataMatrixFindBarcodes 4 bytes. how many DataMatrix barcodes to search
Default value - 1
DataMatrixDecSpeed 4 bytes. Speed of DataMatrix barcode decoder
Default value - 0
Possible values:
0 - Normal
1 - Fast
2 - Slow
DataMatrixInverseType 4 bytes. what barcodes should be decoded (darks on light or lights on dark).
Default value - (1) darks on light
2-light on dark, 3-light and dark
bDataMatrixSupportECI 4 bytes boolean. Support DataMatrix ECI
Default value - 0 (false)
DataMatrixDecMirror 4 bytes. Normal/Mirror DataMatrix decoding
Default value - 0
Possible values:
0 - only Normal
1 - only Mirror
2 - Normal and Mirror
uiQRCodeFindBarcodes 4 bytes. how many QRCode barcodes to search
Default value - 1
QRCodeDecSpeed 4 bytes. Speed of QRCode barcode decoder
Default value - 0
Possible values:
0 - Normal
1 - Fast
2 - Slow
bQRCodeFindMicro 4 bytes boolean. Search Micro QRCode
Default value - 0 (false)
QRCodeDecMirror 4 bytes. Normal/Mirror QRCode decoding
Default value - 0
Possible values:
0 - only Normal
1 - only Mirror
2 - Normal and Mirror
uiAztecCodeFindBarcodes 4 bytes. how many AztecCode barcodes to search
Default value - 1
AztecCodeDecSpeed 4 bytes. Speed of AztecCode barcode decoder
Default value - 0
Possible values:
0 - Normal
1 - Fast
2 - Slow
ScanningRectLeft 4 bytes. Left coordinate of scanning area. It works when all ScanningRectX parameters are correct
ScanningRectTop 4 bytes. Top coordinate of scanning area
ScanningRectWidth 4 bytes. Width of scanning area
ScanningRectHeight 4 bytes. Height of scanning area
Return Value
integer. Returns the Error Code.



getResNum

Gets the number of decoded barcodes.

Syntax
object.getResNum(decHandle)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
Return Value
integer. If less than 0 then it returns the Error Code, else the barcode number.



getResBarcodeType

Gets the barcode type.

Syntax
object.getResBarcodeType(decHandle, barcodeNum)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
barcodeNum integer value that defines the barcode number.
Return Value
integer. If less than 0 then it returns the Error Code, else the barcode type.



getResDQ

Gets the decoding quality.

Syntax
object.getResDQ(decHandle, barcodeNum)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
barcodeNum integer value that defines the barcode number.
Return Value
integer. If less than 0 then it returns the Error Code, else the decoding quality (0...100).



getResData

Gets the barcode data.

Syntax
object.getResData(decHandle, pOutBuf, bufLen, barcodeNum)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pOutBuf pointer to the outer buffer
bufLen outer buffer length
barcodeNum integer value that defines the barcode number.
Return Value
integer. If less than 0 then it returns the Error Code, else the barcode data.



getResPoints

Gets the barcode corners coordinates.

Syntax
object.getResPoints(decHandle, pOutBuf, bufLen, barcodeNum)
Parameters
decHandle HANDLE of barcode decoder returned by InitLib method.
pOutBuf pointer to the outer buffer
bufLen outer buffer length
barcodeNum integer value that defines the barcode number.
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.



Error Codes

0 Ok
1 Error
2 invalid decoder handle
3 invalid parameter
4 invalid barcode number
5 not enought memory
6 unknown property



Barcode Types

0x00000001 Code 128
0x00000002 Code 39
0x00000004 Interleaved 2/5
0x00000008 EAN-13
0x00000010 EAN-8
0x00000020 Codabar
0x00000040 Code 11
0x00000080 UPC-A
0x00000100 UPC-E
0x00000200 Industrial 2/5
0x00000400 Code 93
0x00010000 MSI Plessey
0x00000800 DataBar omnidirectional, DataBar Truncated
0x00001000 DataBar limited
0x00002000 DataBar stacked
0x00004000 DataBar Expanded
0x00008000 DataBar Expanded stacked
0x01000000 unrecognized linear
0x02000000 unrecognized PDF417
0x04000000 unrecognized DataMatrix
0x08000000 unrecognized QRCode
0x00100000 unrecognized AztecCode
0x10000000 DataMatrix
0x20000000 PDF417
0x40000000 QRCode
0x80000000 AztecCode