Creates and initializes barcode scanner object. This method creates a video element (and others) inside the HTML element with the ID "ScannerSettings.viewport.id", initializes all inside variables of the DSScanner object, initializes the WebAssembly library (datasymbol-sdk.wasm). After the scanner is created (onScannerReady event), you can start scanning (StartScanner).
Syntax
ParametersDSScanner.Create(scannerSettings);
Return Value
- scannerSettings - ScannerSettings object that defines all scanner properties
ExampleNo return value.
Notevar scannerSettings = { viewport: { id: 'datasymbol-barcode-viewport', width: 640, //null means 100% width }, camera: { resx: 640, resy: 480, }, barcode: { barcodeTypes: ['Code128', 'DataMatrix'], }, }; DSScanner.addEventListener('onError', onError); DSScanner.addEventListener('onBarcode', onBarcodeReady); DSScanner.addEventListener('onScannerReady', function () { DSScanner.StartScanner(); }); DSScanner.Create(scannerSettings);
Stops barcode decoding, turn off camera and free all resources. To start scanner working again need to call Create method.
Syntax
Return ValueDSScanner.Destroy();
NoteNo return value.
Events:
onScannerDestroyed
Starts the scanning cycle of frames. To save energy (especially it is important for mobile devices), there is a pause between scanning frames that is specified by the parameter "ScannerSettings.scanner.FrameTimeout". By default the pause makes 100ms, you can change this value depending on your requirements.
Syntax
Return ValueDSScanner.StartScanner();
NoteNo return value.
Events:
onScannerStarted
Stops the frame scanning cycle. The object of the scanner itself DSScanner is completely ready for use, and you can immediately restart scanning (StartScanner method).
Syntax
ParametersDSScanner.StopScanner([keepVideo[ = false ]]);
Return Value
- keepVideo - keep video preview (default - false)
NoteNo return value.
Events:
onScannerStopped
Decodes image file (png, jpg, bmp, etc.) with barcode. To work in this mode you should not pass the ScannerSettings.camera object. In this case the browser will not request the camera access. To avoid the image drawing you can omit the ScannerSettings.viewport object.
Syntax
ParametersDSScanner.DecodeImage(src [, listener]);
Return Value
ExampleNo return value.
NoteJavaScriptfunction onBarcodeReady (barcodeResult) { var barDataEl = document.getElementById('status'); for (var i = 0; i < barcodeResult.length; ++i) { var sBarcode = DSScanner.bin2String(barcodeResult[i]); var sPoints = ""; for (var j = 0; j < 4; j++) sPoints += "(" + barcodeResult[i].points[j].x + ", " + barcodeResult[i].points[j].y + "),"; barDataEl.innerHTML = barcodeResult[i].type + ": " + sBarcode + "|" + sPoints; console.log(barDataEl.innerHTML); } }; function onError(err) { var statusElement = document.getElementById('status'); statusElement.innerHTML = 'Error: ' + err.message; } function CreateScanner(device){ var scannerSettings = { scanner: { key: '', }, viewport: { id: 'datasymbol-barcode-viewport', }, barcode: { barcodeTypes: ['EAN13', 'UPCA', 'Code128', 'Code39', 'EAN8', 'DataMatrix', 'QRCode'], totalBarcodes: 7, uiLinearFindBarcodes: 7, }, }; DSScanner.addEventListener('onError', onError); DSScanner.addEventListener('onBarcode', onBarcodeReady); DSScanner.addEventListener('onScannerReady', function () { console.log('HTML onScannerReady'); var statusElement = document.getElementById('status'); statusElement.innerHTML = ' '; }); DSScanner.Create(scannerSettings); } window.onload = function () { CreateScanner(); document.getElementById('file-input').addEventListener('change', DecodeImage, false); } function DecodeImage(e) { var file = e.target.files[0]; if (!file) return; DSScanner.DecodeImage(file); //DSScanner.DecodeImage('/barcode.png'); }
HTML<p id='status'>Downloading ...</p> <div id="datasymbol-barcode-viewport" style="display:block;width:640px;height:480px;"></div> Load File:<input type="file" id="file-input"'/>
Adds handlers for various events
Syntax
ParametersDSScanner.addEventListener(type, listener);
Return Value
- type - A case-sensitive string representing the event type to listen for. Find available events here
- listener - The function which receives a notification
ExampleNo return value.
DSScanner.addEventListener('onScannerReady', function () { DSScanner.StartScanner(); });
Changes any scanner settings
Syntax
ParametersDSScanner.setScannerSettings(scannerSettings);
Return Value
- scannerSettings - ScannerSettings object that defines all scanner properties
ExampleNo return value.
Notevar scannerSettings = { camera: { resx: 800, }, }; DSScanner.setScannerSettings(scannerSettings);
Returns the copy of ScannerSettings object contained inside DSScanner object.
Syntax
Return Valuevar scannerSettings = DSScanner.getScannerSettings();
ScannerSettings object
Returns cameras that can be used on this device.
Syntax
ParametersDSScanner.getVideoDevices(listener[, requestLabel[ = false ]]);
Return Value
- listener - the function which receives the video devices
- requestLabel - request camera label. If "true" Firefox will ask for permision to access the camera twice
ExampleNo return value.
DSScanner.getVideoDevices(function (devices) { devices.forEach(function (device) { console.log("device:" + device.label + '|' + device.id); }); });
Returns barcode string converted from binary data.
Syntax
Parametersvar barcodeString = DSScanner.bin2String(barcodeResult [, dataEncoding]);
Return Value
- barcodeResult - object with barcode properties returned by onBarcode event
- dataEncoding - encoding type of binary data in barcodeResult, default - 'utf8', possible values:
'utf-8', 'ibm866', 'iso-8859-2', 'iso-8859-3', 'iso-8859-4', 'iso-8859-5', 'iso-8859-6', 'iso-8859-7', 'iso-8859-8', 'iso-8859-8i', 'iso-8859-10', 'iso-8859-13', 'iso-8859-14', 'iso-8859-15', 'iso-8859-16', 'koi8-r', 'koi8-u', 'macintosh', 'windows-874', 'windows-1250', 'windows-1251', 'windows-1252', 'windows-1253', 'windows-1254', 'windows-1255', 'windows-1256', 'windows-1257', 'windows-1258', 'x-mac-cyrillic', 'gbk', 'gb18030', 'hz-gb-2312', 'big5', 'euc-jp', 'iso-2022-jp', 'shift-jis', 'euc-kr', 'iso-2022-kr', 'utf-16be', 'utf-16le', 'x-user-defined',
ExampleString value
function onBarcodeReady (barcodeResult) { for (var i = 0; i < barcodeResult.length; ++i) { var barcodeString = DSScanner.bin2String(barcodeResult[i], 'sjis'); console.log(barcodeString); } }
Returns value that determines scanner ready state.
Syntax
Return Valuevar bScannerIsReady = DSScanner.IsScannerReady();
Exampleboolean value
console.log(DSScanner.IsScannerReady());
Returns barcode Web SDK and barcode scanner core versions.
Syntax
Return Valuevar version = DSScanner.getVersion();
ExampleObject
{ websdk:WEBSDK, coresdk:CORESDK }
console.log(DSScanner.getVersion());
Returns license information. If Barcode Web SDK works in demo mode then all fields are empty.
Syntax
Return Valuevar li = DSScanner.getLicenseInfo();
ExampleObject
{ licenseInfo: 'MyCompanyName ...', edition: 'professional', //or 'standard' decoders: 'linear,pdf417,datamatrix,qrcode,azteccode', //or any other combination expDate: '2020-02-29' //year-month-day, epiration date (or empty if for life time) }
DSScanner.addEventListener('onInitialized', function () { var li = DSScanner.getLicenseInfo(); console.log("licenseInfo:\n" + li.licenseInfo); console.log("edition: " + li.edition); console.log("decoders: " + li.decoders); console.log("expDate: " + li.expDate); });
Returns the current video frame.
Syntax
Return Valuevar imageData = DSScanner.getFrame();
ExampleImageData Object
//myCanvas - CANVAS HTML element with 'myCanvas' id //<canvas id='myCanvas' width='320' height='240'></canvas> var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); var imageData = DSScanner.getFrame(); canvas.width = imageData.width; canvas.height = imageData.height; ctx.putImageData(imageData, 0, 0); //draw current frame in 'myCanvas'
Adds a barcode to the list of barcodes to ignore.
This method can be used if you want to decode all remaining barcodes except this one.
The barcode will be removed from this list after ScannerSettings.scanner.ignoreBarcodeTime ms.
Online Demo
Syntax
ParametersDSScanner.addIgnoreBarcode(barcodeObj);
Return Value
- barcodeObj - object with barcode
ExampleNo return value.
function onBarcodeReady (barcodeResult) { for (var i = 0; i < barcodeResult.length; ++i) { if( barcodeResult[i].ignoreBarcode ) continue; DSScanner.addIgnoreBarcode( barcodeResult[i] ); } }
Sets the array of barcodes to ignore. Syntax
ParametersDSScanner.setIgnoreBarcode(barcodeArray);
Return Value
- barcodeArray - array with barcodes
No return value.
Returns the current video frame.
Syntax
Return Valuevar ignoredBarcodes = DSScanner.getIgnoreBarcode();
Array of barcodes to ignore