Document Imaging Blog

Web Scanning Control

Reading Barcodes from an Image in a Web App

Introduction

Because of its accuracy and its ease of use, Barcodes have become more and more popular as a part of daily document management workflow. You can use barcodes to identify documents, separate batch scanning, or even get reliable metadata as a document identifier.

This article will show you how to decode barcodes from an image captured or scanned in a web application with the help of ImageCapture Suite.

Background

Dynamsoft’s Image Capture Suite is a document imaging library for web applications. It allows you to capture images from scanners, webcams and other TWAIN/UVC/WIA compatible devices from all mainstream browsers – Internet Explorer (32-bit/64-bit), Firefox, Chrome and Safari – on Windows and Mac. The toolkit also comes with a Barcode Reader SDK which allows you to decode both 1D and 2D barcode symbols online.

If you are interested in the SDK, the 30-day Free Trial can be downloaded from Dynamsoft website.

Coding Instruction

Special Note

As a component running in the client-side browsers, JavaScript is the preferred language to call all the methods/properties/events of ImageCapture Suite

Capture/load the barcode image

ImageCapture Suite allows you to load an existing image or to capture images from scanners, webcams, etc.

/*-----------------Load an existing image from local disk---------------------*/
function btnLoad_onclick() {
    DWObject.IfShowFileDialog = true; //show File dialog box for you to browse and load an image(s) from local disk
    DWObject.LoadImageEx("", 5); //load the selected image into the control
}

/*-----------------Download an existing image from server---------------------*/

if(location.hostname != "")
    {
        DWObject.HTTPPort = location.port == "" ? 80 : location.port;
        DWObject.HTTPDownload(location.hostname, location.pathname.substring(0, location.pathname.lastIndexOf('/')) + ImgArr);  
    }

/*-----------------Capture a new image from scanner or webcam---------------------*/

function AcquireImageInner(){
    if (DW_DWTSourceContainerID == "")
        DWObject.SelectSource();
    else
        DWObject.SelectSourceByIndex(document.getElementById(DW_DWTSourceContainerID).selectedIndex);
    DWObject.CloseSource();
    DWObject.OpenSource();
    var iSelectedIndex = document.getElementById(DW_DWTSourceContainerID).selectedIndex;
    var iTwainType = DWObject.GetSourceType(iSelectedIndex);

    if(iTwainType == 0) // capture from TWAIN device
    {
        DWObject.IfShowUI = document.getElementById("ShowUI").checked; //set whether to show the user interface of the device

        var i;
        for(i=0;i<3;i++)
        {
            if(document.getElementsByName("PixelType").item(i).checked==true)
            DWObject.PixelType = i; //set the pixel type of the image
        }  
        DWObject.Resolution = Resolution.value; //set the resolution
        DWObject.IfFeederEnabled = document.getElementById("ADF").checked ; //scan from ADF or flatbed
        DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked ; //if duplex scan
        AppendMessage("Pixel Type: " + DWObject.PixelType + "<br />Resolution: " + DWObject.Resolution + "<br />");
    }
    Else // capture from webcam
    {
        DWObject.IfShowUI = document.getElementById("ShowUIForWebcam").checked;

	if (DW_InWindows) {
        DWObject.SelectMediaTypeByIndex(document.getElementById("MediaType").selectedIndex);
        DWObject.SelectResolutionForCamByIndex(document.getElementById("ResolutionWebcam").selectedIndex);

        AppendMessage("MediaType: " + DWObject.MediaType + "<br />Resolution: " + DWObject.ResolutionForCam + "<br />");  
	}
    }
    DWObject.IfDisableSourceAfterAcquire = true;
    DWObject.AcquireImage(); //capture the image
}

Decode the barcode using JavaScript

After we load or capture a barcode image into the control, we can then work on decoding the barcode symbols.

function J_Barcoding() {
//check if the barcode reader SDK exists on the client machine, and if it's of the latest version, if not, download the latest barcode reader SDK from the web server
     var barcodeVerStr = DWObject.BarcodeVersion;
    if (!barcodeVerStr ||barcodeVerStr != DW_BarcodeVersion) {
        if (location.hostname != "") {
            var strHostIP = location.hostname;
            DWObject.HTTPPort = location.port == "" ? 80 : location.port;       
            var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII	
            var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
            var strBarcodepath = CurrentPath + "Resources/barcode.zip";
            DWObject.HTTPDownloadResource(strHostIP, strBarcodepath, "barcode.zip"); //download barcode reader DLL from web server to the client machine
        }
    }

    var strLength = DWObject.GetImageSize(DWObject.CurrentImageIndexInBuffer, DWObject.GetImageWidth(DWObject.CurrentImageIndexInBuffer), DWObject.GetImageHeight(DWObject.CurrentImageIndexInBuffer));
    if (strLength > 300000) //show progress bar if the image size is too big
        DWObject.IfShowProgressBar = true;
    else
        DWObject.IfShowProgressBar = false;
    var barcodeformat;
    barcodeformat = document.getElementById("ddl_barcodeFormat").value; //specify the barcode type
    DWObject.ReadBarcode(DWObject.CurrentImageIndexInBuffer, barcodeformat); // decode the barcode
    DWObject.IfShowProgressBar = true;
    var barcodeText = "";
    barcodeText += "ReadBarcode : " + DWObject.ErrorString + "<br/>"; 

    var count = DWObject.BarcodeCount;
    barcodeText += "BarcodeCount: " + count + "<br/>";
    for (i = 0; i < count; i++) {
        var text = DWObject.GetBarcodeText(i); 
        var x = DWObject.GetBarcodeInfo(0, i); 
        var y = DWObject.GetBarcodeInfo(1, i);
        var type = DWObject.GetBarcodeInfo(2, i);
        var len = DWObject.GetBarcodeInfo(5, i);
        barcodeText += ("barcode[" + (i + 1) + "]: " + text + "<br/>");
        barcodeText += ("text len:" + len + "<br/>");
        barcodeText += ("type:" + getBarcodeType(type) + "<br/>");
        barcodeText += ("x: " + x + " y:" + y + "<br/>");

        var strBarcodeString = text + "\r\n" + getBarcodeType(type);
        DWObject.AddText(DWObject.CurrentImageIndexInBuffer, x, y, strBarcodeString, 255, 4894463, 0, 1);
    }
    AppendMessage(barcodeText);    
    J_SetBtnProcessingAndText("btnReadBarcode", false, "Try Barcode");
}

Run or deploy the application on web server

The complete source code can be downloaded from the article.

There is a “Scripts\ProductKey.js” file with a temporary trial product key. If you get license error when running the sample, you can download ImageCapture Suite from Dynamsoft’s website to get a valid trial license. ImageCapture Suite 30-Day Free Trial Download

To test barcode reading from different client machines, you can simply copy the sample code with ImageCapture Suite to your web server (IIS, Apache or Tomcat). Users only need to download and install the ActiveX/Plugin in the browser on their first visit to the web page. More details on how to deploy ImageCaptureSuite

The online demo is also available for your reference.
ImageCapture Suite Barcode Reader Online Demo

Using TWAIN in .NET Applications

Background

If you want to embed image capture feature into your .NET application, you will need to firstly determine what API to use for communicating with the imaging devices.  Here we will choose TWAIN to interact with scanners and we will talk about how to use TWAIN in .NET (C# or VB.NET) in this article.

What’s TWAIN?

TWAIN is a standard software protocol defined by TWAIN Working Group. It is an applications programming interface (API) that regulates communication between software applications and imaging devices, such as scanner, digital camera and capture card.

The four key elements in TWAIN are:

Four key elements in TWAIN

How to call TWAIN interface in .NET?

Since .NET Framework has no built-in support for TWAIN, we can either code from scratch by working with the interop methods of .NET to access the TWAIN API, or we can use a 3rd-party .NET wrapper class for TWAIN to save some time.

Code from scratch

To develop the TWAIN scanning code in .NET from scratch, here are some resources that might be helpful for you.

Use a 3rd-party .NET wrapper class for TWAIN

If you want to complete your document scanning project in a short timeframe and don’t have time to read through the long TWAIN Specification and write and test the TWAIN code, you can take advantage of a 3rd-party .NET TWAIN SDK.

About Dynamic .NET TWAIN

Dynamsoft’s Dynamic .NET TWAIN is a C# TWAIN wrapper. It is based on Microsoft .NET Framework, which is optimized for use in C# and VB.NET.  With Dynamic .NET TWIAN, you can basically capture image using one line of code.

dynamicDotNetTwain.AcquireImage();

Here are some highlights of the features of Dynamic .NET TWAIN:

  • Compatible with TWAIN Specification 2.1;
  • Compatible with UVC/WIA Webcams;
  • Support WinForms & WPF applications;
  • Customizable Scanning and Loading Process (pixel type, resolution, duplex, ADF, etc.);
  • Support BMP, JPEG, PNG, TIFF, PDF and multi-page TIFF and PDF

See Dynamic .NET TWAIN in Action

Dynamic .NET TWAIN is royalty free and priced at $799.00 per developer. You can visit the page to learn more if you are interested.

Develop a Cross-Platform Document Imaging Web App

Introduction

Document management is a big challenge nowadays, especially for paper-heavy industries like healthcare, financial, government etc. You may want to develop a document imaging application which allows users, either using Windows or Mac OS, to easily scan documents or capture images via browsers, so that you can manage the documents/records more efficiently.

The article will show you how to develop a cross-platform document imaging solution that works with all mainstream browsers by using ImageCapture Suite.

Background

Dynamsoft’s ImageCapture Suite is a browser-based document imaging library. It allows you to capture images from scanners, webcams and other TWAIN/UVC/WIA compatible devices on Windows or Mac. If you are interested in the SDK, the 30-day Free Trial can be downloaded from Dynamsoft website.

Key Features

  • Capture images/documents from scanners, webcams and other TWAIN/WIA/UVC compatible imaging devices.
  • Compatible with the mainstream browsers including IE (both 32-bit & 64-bit), Firefox, Chrome, Safari and Opera on Windows and Mac.
  • Edit images: Crop, Change Image Size, Rotate, Zoom, Erase and more.
  • Upload images to various locations – local folder, FTP site, web server, database, SharePoint library and more.
  • Enhanced security: supports SSL and Windows/Forms/Basic Authentication.
  • Supported image formats include BMP, JPEG, PNG, TIFF (both single and multi-page) and PDF (both single and multi-page).

Using the Code

Cross platform & cross browser support

ImageCapture Suite comes with 3 editions for different users:

  • ActiveX Edition – works with 32-bit or 64-bit Internet Explorer
  • Plugin Edition – works with Chrome, Firefox, Safari, Opera on Windows
  • Mac Edition – works with Chrome, Firefox, Safari, Opera on Mac

To make your web application work with all the mainstream browsers on Windows or Mac, you can detect the OS and browser type in JavaScript and load the corresponding edition of ImageCapture Suite.

function DW_PageonloadInner() {
    // Get User Agent Value
    ua = (navigator.userAgent.toLowerCase());
    if (ua.indexOf("wow64") == -1) {
        var samplesource32bitCtr = document.getElementById("samplesource32bit");
        if (samplesource32bitCtr)
            samplesource32bitCtr.href = "http://www.dynamsoft.com/demo/DWT/Sources/twainkit.exe";
    }
    // Set the Explorer Type
    if (ua.indexOf("msie") != -1)
        DW_InIE = true;
    else
        DW_InIE = false;
    // Set the Operating System Type
    if (ua.indexOf("macintosh") != -1)
        DW_InWindows = false;
    else
        DW_InWindows = true;
    // Set the x86 and x64 type
    if (ua.indexOf("win64") != -1 && ua.indexOf("x64") != -1)
        DW_InWindowsX86 = false;
    else
        DW_InWindowsX86 = true;

    var varShowDetail = document.getElementById("showDetail");
    if (varShowDetail && DW_InIE == true) {
        varShowDetail.style.display = "";
    }
    InitMenum();
}

function DW_CreateControl(bNeebBack) {
    var objString = "";
    var DWTContainer;

    // For IE, render the ActiveX Object
    if (DW_InIE) {
        objString = "<object id='" + DW_ObjectName + "' style='width:" + DW_Width + "px;height:" + DW_Height + "px'";

        if (DW_InWindowsX86)
            objString += "codebase='" + DW_CABX86Path + "#version=" + DW_VersionCode + "' ";
        else
            objString += "codebase='" + DW_CABX64Path + "#version=" + DW_VersionCode + "' ";

        var temp = DW_IsTrial ? DW_TRAILCLASSID : DW_FULLCLASSID;
        objString += " classid='clsid:" + temp + "' viewastext>";
        objString += " <param name='Manufacturer' value='DynamSoft Corporation' />";
        objString += " <param name='ProductFamily' value='" + DW_ProductName + "' />";
        objString += " <param name='ProductName' value='" + DW_ProductName + "' />";
        //objString += " <param name='wmode' value='transparent'/>  ";
        objString += " </object>";
    }   
    // For non-IE, render the embed object
    else {
        objString = " <embed id='" + DW_ObjectName + "'style='display: inline; width:" + DW_Width + "px;height:" + DW_Height + "px' id='" + DW_ObjectName + "' type='" + DW_MIMETYPE + "'";
        objString += " OnPostTransfer='Dynamsoft_OnPostTransfer' OnPostAllTransfers='Dynamsoft_OnPostAllTransfers'";
        objString += " OnMouseClick='Dynamsoft_OnMouseClick'  OnPostLoad='Dynamsoft_OnPostLoadfunction'";
        objString += " OnImageAreaSelected = 'Dynamsoft_OnImageAreaSelected'";
        objString += " OnImageAreaDeSelected = 'Dynamsoft_OnImageAreaDeselected'";
        objString += " OnMouseDoubleClick = 'Dynamsoft_OnMouseDoubleClick'";
        objString += " OnMouseRightClick = 'Dynamsoft_OnMouseRightClick'";
        objString += " OnTopImageInTheViewChanged = 'Dynamsoft_OnTopImageInTheViewChanged'";
        objString += " OnGetFilePath='Dynamsoft_OnGetFilePath'";
        if (DW_InWindows)
            objString += " pluginspage='" + DW_MSIPath + "'></embed>";
        else
            objString += " pluginspage='" + DW_PKGPath + "'></embed>";
    }  

    DWTContainer = document.getElementById(DW_DWTContainerID);
    DWTContainer.innerHTML = objString;
    DWObject = document.getElementById(DW_ObjectName);
}

Acquire Images from scanners or webcams

In relative terms, TWAIN and Webcam protocols are both special areas which would cost a lot of time to become familiar with. It might take months to code your application from scratch.

However, with ImageCapture Suite, you would be able to complete the document scanning or image capture coding in hours.

You can customize the scanning properties, such as Resolution, pixel type, Brightness, Contrast etc. in your code. You can also set the scanning settings by showing the user interface of the source.

function AcquireImageInner(){
    if (DW_DWTSourceContainerID == "")
        DWObject.SelectSource(); //show the available imaging devices
    else
        DWObject.SelectSourceByIndex(document.getElementById(DW_DWTSourceContainerID).selectedIndex);
    DWObject.CloseSource();
    DWObject.OpenSource();
    var iSelectedIndex = document.getElementById(DW_DWTSourceContainerID).selectedIndex;
    var iTwainType = DWObject.GetSourceType(iSelectedIndex); //check the device type, TWAIN devices or webcam

    if(iTwainType == 0)
    {
        DWObject.IfShowUI = document.getElementById("ShowUI").checked;

        var i;
        for(i=0;i<3;i++)
        {
            if(document.getElementsByName("PixelType").item(i).checked==true)
            DWObject.PixelType = i;
        } 
        DWObject.Resolution = Resolution.value;
        DWObject.IfFeederEnabled = document.getElementById("ADF").checked ;
        DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked ;
        AppendMessage("Pixel Type: " + DWObject.PixelType + "<br />Resolution: " + DWObject.Resolution + "<br />");
    }
    else
    {
        DWObject.IfShowUI = document.getElementById("ShowUIForWebcam").checked;

        if (DW_InWindows) {
//some webcam features only for Windows currently
        DWObject.SelectMediaTypeByIndex(document.getElementById("MediaType").selectedIndex);
        DWObject.SelectResolutionForCamByIndex(document.getElementById("ResolutionWebcam").selectedIndex);

    }
    DWObject.IfDisableSourceAfterAcquire = true;
    DWObject.AcquireImage(); //acquire image from the device
}

Run or deploy the application on web server

The complete source code can be downloaded from the article. The sample includes features, such as capture images from scanner or webcam, edit and save the images to local disk.

To run the sample application with a valid trial license or customize the web application, you can download ImageCapture Suite from Dynamsoft’s website.
ImageCapture Suite 30-Day Free Trial Download

To test the document imaging features from different client machines, you can simply copy the sample code to your web server (IIS, Apache or Tomcat). Users will only need to download and install the ActiveX/Plugin in the browser on the first visit of the web page. More details

The online demo is also available for your reference.
ImageCapture Suite Online Demo

Dynamic Web TWAIN Showcase – an Open Source Online Automated Fingerprint Identification System

Came across a demonstration video of an online automated fingerprint identifaction system (OnlineAFIS), which uses Dynamic Web TWAIN, on Yutube. It looks pretty cool and we want to share with you.

OnlineAFIS is a web-based user-interface developed for capturing Live Scan and enrollment of Fingerprints. The web browser accesses the fingerprint sensor installed on client’s machine using Dynamic Web TWAIN, which is a browser-based TWAIN interface compatible with all mainstream browsers on Windows and Mac.

If you are looking at interracting with TWAIN devices, such as scanners, digital cameras, from your website, you can check out Dynamic Web TWAIN here.

You can also play with the online demo of Web TWAIN SDK – scan documents, edit and upload them to your file system, database or cloud.

 

Get 30-day free trial 

 

Develop a document scanning application using Dynamic Web TWAIN

Are you getting too many paper documents and want to develop a document scanning application to better manage them? Dynamic Web TWAIN will make the job easy for you. With the TWAIN interface, you can enable users scan documents, edit and upload them to local disk, web server, database or cloud via your website.

Have a quick play with the online demo of Dynamic Web TWAIN to see how it works.

How to develop my document scanning application using Dynamic Web TWAIN?

Dynamic Web TWAIN is a client-side scanner programming SDK. You can use JavaScript to call its methods/properties. It comes with three editions – ActiveX, Plugin and Mac – that work with all mainstream browsers on Windows and Mac.

Download free trial

If you don’t have Dynamic Web TWAIN on your machine, please download the 30-day free trial first. 

Create the scan page

First, let’s create a scan page – say “Scan.htm”. You can open it in your preferred text editor.

Add two divs:

“dwtcontrolContainer”  – To contain the Dynamic Web TWAIN object
“DWTNonInstallContainerID” – To show messages for end users when Dynamic Web TWAIN is not installed

<!--This is where Dynamic Web TWAIN control will be rendered.--> 
	<div id="dwtcontrolContainer"> 
	<div id="DWTContainerID" style="position: relative;" class='divcontrol'> 
	</div> 
	<div id="DWTNonInstallContainerID" style="width: 580px"> 
	</div> 
	</div>

Check the OS/Browser Type and Load Corresponding Web TWAIN Edition

To work with different browsers – IE (x86 or x64), Chrome, Firefox, Safari – on Windows and Mac, we need to check the current browser and OS type which the end users is currently using and load the corresponding Dynamic Web TWAIN edition.

To save you time, we have worked out this part of code. You can just download DWT_BasicPageInitiate.js  and reference to it  in your Scan page like this:

<script type="text/javascript" language="javascript" src="Scripts/DWT_BasicPageInitiate.js"></script>

Acquire image from scanner

Now we can call the AcquireImage method of Dynamic Web TWAIN to acquire images from your scanner with TWAIN driver.

You can do that with several lines of code.

In Scan.htm, add a Scan button and trigger scanning on buttonclick.

<input id="btnScan" type="button" value="Scan" onclick="DW_AcquireImage();" />

And you can acquire an image using the following JavaScript code:

<script language="javascript" type="text/javascript"> 
function DW_AcquireImage() {
    DWObject.SelectSource(); //show the available TWAIN sources
    DWObject.CloseSource();
    DWObject.OpenSource();
    DWObject.IfShowUI = false; //hide the user interface of the source
    DWObject.PixelType = 1; //scan the document in gray
    DWObject.Resolution = 200; //scan at resolution 200
    DWObject.IfDisableSourceAfterAcquire = true;
    DWObject.AcquireImage(); 
}
</script>

Test scanning

You have now built a web page for scanning now.  Pretty easy right?

To test it, you can open Scan.htm in your browser (IE, Chrome, Firefox or Safari). Click Scan button and it will show all the available TWAIN devices on your machine and you can choose one to scan a document.

You can also test with the online demo here with basic scanning feature.

To get more samples of Dynamic Web TWAIN, including image edit, upload to web server/database in C#, VB.NET, PHP, JSP and more, please visit the sample page.

Copyright © 2013 Dynamsoft. All Rights Reserved. Privacy Statement | Site Map


Programming library for TWAIN scanners | Document imaging API

The leading provider of version control solutions and TWAIN SDKs
  • Sign into hosted web portal
  • Contact us
Dynamic Web TWAIN - TWAIN ActiveX | Web Scanning Control