How to use the barcode reader

The barcode reader was designed for simplicity. There are merely 3 APIs

Addon.Barcode.Download, Addon.Barcode.Read, Addon.Barcode.ReadRect

At the same time, there are 3 JS files and 2 zip files directly related to this add-on, they are found under /Resources/addon/

image026

In order to use this add-on, you need to first make sure you have referenced the file dynamsoft.webtwain.addon.barcode.js in your code.

<script type="text/javascript" src="Resources/dynamsoft.webtwain.initiate.js"> </script>

<script type="text/javascript" src="Resources/dynamsoft.webtwain.config.js"> </script>

<script type="text/javascript" src="Resources/addon/dynamsoft.webtwain.addon.barcode.js"> </script>

Let’s start with a simple code snippet

function ReadBarcode() {
    if (DWObject) {
        DWObject.Addon.Barcode.Download(
            "<u>http://localhost/dwt/addon/Barcode.zip</u>", function () {
               DWObject.Addon.Barcode.Read(DWObject.CurrentImageIndexInBuffer,
                    EnumDWT_BarcodeFormat.CODE_39, GetBarcodeInfo, GetErrorInfo);
            }, function (errorCode, errorString) {alert(errorString); });
    }
}

The first thing to note here is the JS-style 'asynchronous syntax' of both 'Download' and 'Read' methods.

Here is how the 'download' method works

Syntax
.Addon.Barcode.Download(remoteFile, [optionalAsyncSuccessFunc, optionalAsyncFailureFunc])

Workflow

image027

When the barcode reader (DLLs) is in place, barcode reading begins. Here is how the ‘read’ method works

Syntax
.Addon.Barcode.Read(sImageIndex, format, [optionalAsyncSuccessFunc, optionalAsyncFailureFunc]);

Workflow

image028

Here is how we get the actual barcode information in the function GetBarcodeInfo

function GetBarcodeInfo(sImageIndex, result) {
    var count = result.GetCount();
    if (count == 0) { alert("The barcode for the selected format is not found."); return;
    } else {
for (i = 0; i < count; i++) {
var text = result.GetContent(i); var format = result.GetFormat(i);
            var x = result.GetX1(i); var y = result.GetY1(i); 
            var barcodeText = ("barcode[" + (i + 1) + "]: " + text + "\n");
            barcodeText += ("format:" + (format==4?"Code 39":"Code 128") + "\n");
            barcodeText += ("x: " + x + " y:" + y + "\n");
            alert(barcodeText);
        }
    }
}

result is the key object that contains all the info about the barcode(s). More info can be found on the API document Addon.Barcode.Read.

Compared with the main API ‘Read’, the only difference of the last API ‘ReadRect’ is that it only reads the selected area of an image instead of the entire image. Because of this, it works much faster and is especially useful when all documents being scanned have a barcode in a fixed position. The following is a simple code snippet on how to use it.

function Dynamsoft_OnReady() {
 DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
     if (DWObject) {

         DWObject.RegisterEvent('OnImageAreaSelected',
           function (sImageIndex, left, top, right, bottom){ 
                   iLeft = left; iTop = top; iRight = right; iBottom = bottom;
         });
     }
}
function ReadBarcode() {
    if (DWObject) {
       DWObject.Addon.Barcode.Download(
            "http://localhost/dwt/addon/Barcode.zip",
            function () {
                DWObject.Addon.Barcode.ReadRect(DWObject.CurrentImageIndexInBuffer,
                  iLeft, iTop, iRight, iBottom,
                  EnumDWT_BarcodeFormat.CODE_39, GetBarcodeInfo, GetErrorInfo);
            },
           function (errorCode, errorString) {
                alert(errorString);
            });
    }

More info can be found on this page Addon.Barcode.ReadRect.

 

Is this page helpful?

Leave a Reply

Your email address will not be published.