Back to Demos List

GSW Scanner

This demonstration will show the ability to access scanner from the soft button in the browser.

Interface Detected:





The fields below will contain the extra data captured by the scan.








About This Demo

This demo is using the GSW Scanner plugin. For more information click here:

var GSW = GSW || {};
GSW.Tests = GSW.Tests || {};

GSW.Tests.addTableRow = function(element, index, array) {
    'use strict';

    /* builds up a single row for the output table
     */

    /*first cell: the AI of the element:
     */
    let tableRow = document.createElement("tr");
    let tableCell = document.createElement("td");
    tableCell.innerHTML = element.ai;
    tableRow.appendChild(tableCell);
    /*second cell: the title or name of the element
     */
    tableCell = document.createElement("td");
    tableCell.innerHTML = element.dataTitle;
    tableRow.appendChild(tableCell);
    /* third cell: the value or contents of the element*/
    tableCell = document.createElement("td");
    tableCell.innerHTML = element.data;
    tableRow.appendChild(tableCell);
    /* fourth cell: the unit of measurement/the currency*/
    tableCell = document.createElement("td");
    tableCell.innerHTML = element.unit;
    tableRow.appendChild(tableCell);
    /*row finished: append to table 
     */
    let parsedElementsOutput = document.getElementById("parsedElementsOutput");
    parsedElementsOutput.appendChild(tableRow);
}

GSW.Tests.interpreteBarcode = function(barcode) {
    'use strict';

    console.log('GSW.Tests.interpreteBarcode', barcode);
    // here, we finally use the library function ...
    try {
        /**
         * sometimes malconfigured scanners replace the FNC1 by
         * some other character or sequence of characters.
         *
         * Here you could fix this behaviour.
         *
         * Example: the scanner sends "^" instead of ASCII 29:
         *
         * var re = /\^/g;
         *
         * barcode = barcode.replace(re, String.fromCharCode(29));
         *
         */

        //console.log('About to call navigator.gswbarcodeparser.parseGS1');
        let answer = navigator.gswbarcodeparser.parseGS1(barcode);
        //console.log('After call to navigator.gswbarcodeparser.parseGS1');
        console.log(JSON.stringify(answer));

        //symbologyIdentification.innerHTML = answer.codeName;

        // clear previous entries of "parsedElementsOutput":
        var prevRows = document.getElementsByTagName("tr"),
            numberOfPrevRows = prevRows.length,
            i = 0;

        for (i = 0; i < numberOfPrevRows; i = i + 1) {
            // delete the first element
            prevRows[0].parentNode.removeChild(prevRows[0]);
        }

        //    attach headerlines:
        let tableRow = document.createElement("tr");
        let tableCell = document.createElement("th");
        tableCell.innerHTML = "AI";
        tableRow.appendChild(tableCell);

        tableCell = document.createElement("th");
        tableCell.innerHTML = "Title";
        tableRow.appendChild(tableCell);

        tableCell = document.createElement("th");
        tableCell.innerHTML = "Contents";
        tableRow.appendChild(tableCell);

        tableCell = document.createElement("th");
        tableCell.innerHTML = "Unit/Currency";
        tableRow.appendChild(tableCell);

        /* header row finished: append to table 
         */
        let parsedElementsOutput = document.getElementById("parsedElementsOutput");
        parsedElementsOutput.appendChild(tableRow);
        answer.parsedCodeItems.forEach(GSW.Tests.addTableRow);
        document.getElementById("error").innerHTML = "";
    } catch (e) {
        document.getElementById("error").innerHTML = e;
    
        // Clear the table
        let parsedElementsOutput = document.getElementById("parsedElementsOutput");
        parsedElementsOutput.innerHTML = '';
    
        // Clear scan result fields
        const fieldsToClear = [
            'character_set',
            'code_id',
            'aim_id',
            'timestamp',
            'label_type',
            'code_type'
        ];
        fieldsToClear.forEach(id => {
            const el = document.getElementById(id);
            if (el) el.value = '';
        });
    }

}


GSW.Tests.onDeviceReady = function() {
    // Cordova is now initialized. Have fun!
    console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);
    navigator.gswscanner.initialize(function(data) {
        console.log('initialize OK');
        document.getElementById('scan_now').addEventListener("touchstart", GSW.Tests.scanPressed);
        document.getElementById('scan_now').addEventListener("touchend", GSW.Tests.scanReleased);
        document.getElementById('scan_now').addEventListener("touchmove", GSW.Tests.scanReleased);
        document.getElementById('scan_now').addEventListener("touchcancel", GSW.Tests.scanReleased);
        GSW.Tests.disable();
        document.getElementById("interface").innerHTML = navigator.gswscanner.api.bold();
    }, function(error) {
        if (error == "Scanner api is unavailable on this device") {
            let notice = document.getElementById("noticeContainer");
            notice.style.display = "block";
            console.log(error);
            document.getElementById("error").innerHTML = "This device does not have a compatible scanner present, and is not compatible with this demo."
        } else if (error.indexOf('scanner decode service is unavailable on this device') != -1) {
            let notice = document.getElementById("noticeContainer");
            notice.style.display = "block";
            notice.style.color = "white"
            document.getElementById("error").innerHTML = 'This device was not manufactured by a supported manufacturer and is not compatible with this demo.'
        } else console.log('Initialize error occured: ' + error);
    });
}

GSW.Tests.scanPressed = function() {
    console.log("scanPressed");
    navigator.gswscanner.enableTrigger(function(result) {
        navigator.gswscanner.softwareTriggerStart(function(result) {
            GSW.Tests.interpreteBarcode(result.data);
            document.getElementById('character_set').value = result.character_set;
            document.getElementById('code_id').value = result.code_id;
            document.getElementById('aim_id').value = result.aim_id;
            document.getElementById('timestamp').value = result.timestamp;
            document.getElementById('label_type').value = result.label_type;
            document.getElementById('code_type').value = result.code_type;
            console.log('Software scanned: ' + result.data + ', character_set: ' + result.character_set + ', code_id: ' + result.code_id + ', aim_id: ' + result.aim_id + ', timestamp: ' + result.timestamp);
        }, function(error) {
            console.log('Error occured: ' + error);
        });
    }, function(error) {
        console.log('Unable to enable trigger: ' + error);
    })
}

GSW.Tests.scanReleased = function() {
    console.log("scanReleased");
    navigator.gswscanner.disableTrigger(function(result) {
        navigator.gswscanner.softwareTriggerStop();
    }, function(error) {
        console.log('Unable to disable trigger: ' + error);
    })
}

GSW.Tests.disable = function() {
    navigator.gswscanner.disableTrigger(function(result) {
        console.log('Trigger disabled');
    }, function(error) {
        console.log('Unable to disable trigger: ' + error);
    })
}
document.addEventListener('gswscannerready', GSW.Tests.onDeviceReady, false);