Free Online OCR Service with Dynamsoft TWAIN SDKs

Dynamsoft provides a free online OCR tool for end-users. In this tutorial, I’d like to anatomize how to use Dynamic Web TWAIN SDK and Dynamic .NET TWAIN SDK to implement the online OCR solution. You can have fun with the tool before reading the next paragraph.

Free Online OCR

OCR_DWT

Prerequisites

1. Download the source code

2. The code project has already contained two language packages: English and Arabic. Other language packages for OCR can be downloaded here.

How to Implement the Online OCR Tool

While doing OCR online, there are four basic steps.

Step 1: load images

Using Dynamic Web TWAIN SDK, images can be loaded into an image view component with a few lines of JavaScript code:

function btnLoad_onclick() {
    _iHowManyImages = DWObject.HowManyImagesInBuffer;
    DWObject.IfShowFileDialog = true;
    DWObject.LoadImageEx("", 5, function() {
        g_DWT_PrintMsg("Loaded an image successfully.");
    }, function() {
    });

    _iErrorCode = DWObject.ErrorCode;
    _strErrorString = DWObject.ErrorString;
}

Step 2: select a language

To create language and result options, just use HTML5 element <select>:

<select id=" " style="width: 250px">
<option value=" "> </option>
<option value=" "> </option>
</select>

Step 3: do OCR

The OCR event is a little bit tricky. On the client side, send all relevant information to the remote server via the JavaScript method HTTPUploadThroughPostEx:

if (DWObject.CurrentImageIndexInBuffer >= 0) {

        DWObject.HTTPUploadThroughPostEx(
            strHTTPServer,
            DWObject.CurrentImageIndexInBuffer,
            "Demo\\DNT-OCRTest\\DoOCR.aspx",
            "OCR.png",
            3, sFun, fFun
        );
    }

The DoOCR.aspx, which is used to receive parameters, is deployed on the server side:

         try
            {
                string strImageName = "";
                int iFileLength;
                HttpFileCollection files = HttpContext.Current.Request.Files;
                HttpPostedFile uploadfile = files["RemoteFile"];
                if (uploadfile != null)
                {
                    strImageName = uploadfile.FileName;
                    iFileLength = uploadfile.ContentLength;

                    Byte[] inputBuffer = new Byte[iFileLength];
                    System.IO.Stream inputStream;

                    inputStream = uploadfile.InputStream;
                    inputStream.Read(inputBuffer, 0, iFileLength);

                    string strLanguage = "0";
                    try
                    {
                        strLanguage = HttpContext.Current.Request.Form["OCRLanguage"].ToString();
                    }
                    catch { }

                    string strFormat = "0";
                    try
                    {
                        strFormat = HttpContext.Current.Request.Form["FileFormat"].ToString();
                    }
                    catch { }

                    string strReturnValue = "";
                    try
                    {
                        string strFileName = OCRMode.OCR(inputBuffer, strLanguage, Convert.ToInt32(strFormat));
                        if (strFormat == "3")
                            strReturnValue = "OK;" + strFileName; 
                        else
                            strReturnValue = "OK;" + "DownLoadOCR.aspx?FileId=" + Server.UrlEncode(strFileName); 
                    }

                    catch (Exception exp)
                    {
                        strReturnValue = "EXP;" + exp.Message.ToString();
                    }
                    finally
                    {

                    }
                    Response.Write(strReturnValue);
                }
            }
            catch (Exception exp)
            {
                Response.Write("EXP;" + exp.Message.ToString() + "; 0;");
            }

­­The method OCRMode.OCR relies on Dynamic .NET TWAIN OCR add-on. Once getting the OCR result, we can use HTTP Response to send a successful notification, which includes the page DownLoadOCR.aspx, back to the Web client.

Step 4: return results

When the message arrives the client, it will trigger the callback function to download the OCR results:

fFun = function() {
        DynamLib.detect.hideMask();
        var responseText = DWObject.HTTPPostResponseString;
        if (responseText != "" && typeof (responseText) == 'string') {
            var tmpState = responseText.split(';');
            if (tmpState[0] == "OK") {
                if (vFileFormat == "3")
                    alert(tmpState[1]);
                else {
                    IshasDownLoadFile = "true";
                    DownLoadFileURL = tmpState[1];
                    DynamLib.detect.hideMask();
                    DownLoadFile();
                }
            }
            else {
                IshasDownLoadFile = "false";
                DownLoadFileURL = "";
                alert(tmpState[1]);
            }
        }
        else {
            alert(DWObject.ErrorString);
        }
    };

OCR_DWT_Result

  • Tom_Kent

    Very nice~