OCR Pro

For Server Side OCR, check out Server-Side OCR

Client-Side

Methods
Download() IsModuleInstalled() Recognize()
RecognizeFile() RecognizeRect() RecognizeSelectedImages()
Properties
Settings
OCR Pro Result Object
OCRResult PageResult
LetterResult ErrorInfo

Code example

The following code example demonstrates how to use the APIs above to perform basic scanning.

function DoOCR() {
    if (DWObject) {
        if (DWObject.HowManyImagesInBuffer == 0) {
            alert("Please scan or load an image first.");
            return;
        }
        var settings = Dynamsoft.WebTwain.Addon.OCRPro.NewSettings();
        var bMultipage = false;
        settings.RecognitionModule = EnumDWT_OCRProRecognitionModule.OCRPM_AUTO;
        settings.Languages = "eng";
        settings.OutputFormat = EnumDWT_OCRProOutputFormat.OCRPFT_TXTS;
        settings.LicenseChecker = "LicenseChecker.aspx";
        DWObject.Addon.OCRPro.Settings = settings;
        //Get ocr result.
        var i, nCount = DWObject.HowManyImagesInBuffer;
        DWObject.SelectedImagesCount = nCount;
        for (i = 0; i < nCount; i++) {
            DWObject.SetSelectedImageIndex(i, i);
        }
        DWObject.Addon.OCRPro.RecognizeSelectedImages(function(result) {
                if (result == null)
                    return null;
                var bRet = "", pageCount = result.GetPageCount();
                if (pageCount == 0) {
                    alert("OCR result is Null.");
                    return;
                } else {
                    for (i = 0; i < pageCount; i++) {
                        var page = result.GetPageContent(i);
                        var letterCount = page.GetLettersCount();
                        for (var n = 0; n < letterCount; n++) {
                            var letter = page.GetLetterContent(n);
                            bRet += letter.GetText();
                        }
                    }
                    console.log(bRet);
                }
            }, function(errorcode, errorstring, result) {
                if (errorcode != -2600 && errorcode != -2601) //-2600:LicenseChecker cannot be empty.  -2601:Cannot connect to the LicenseChecker, please check and make sure it's set correctly.
                    alert(errorstring);
                var strErrorDetail = "";
                var aryErrorDetailList = result.GetErrorDetailList();
                for (var i = 0; i < aryErrorDetailList.length; i++) {
                    if (i > 0)
                        strErrorDetail += ";";
                    strErrorDetail += aryErrorDetailList[i].GetMessage();
                }
                if (strErrorDetail.length > 0 && errorstring != strErrorDetail)
                    alert(strErrorDetail);
            }
        );
    }
}

Methods

Download()
Downloads and installs the OCRPro add-on (it's typically a zipped dll file) on the local system.
Syntax .Addon.OCRPro.Download(sDLLPath, [optionalAsyncSuccessFunc, optionalAsyncFailureFunc]);
Parameters
  • string sDLLPath: specifies the url path of the add-on. E.g. "http://www.dynamsoft.com/DWT/Resources/OCR.zip".
  • <OnSuccess function> optional optionalAsyncSuccessFunc: callback function triggered when the download succeeds.
  • <OnFailure function> optional optionalAsyncFailureFunc: callback function triggered when the download fails.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes This method will attempt to download and install the dll from the server if the dll doesn't exist already or the exising one is of a different version. The method IsModuleInstalled() can be used to determine whether it's necessary to call Download().
IsModuleInstalled()
Returns whether the OCRPro module is already installed on the local system.
Syntax .Addon.OCRPro.IsModuleInstalled();
Parameters
  • None
Return value boolean
Availability Versions: v14.1+
Usage notes If the method returns false, you can use the method Download() to download and install the dll from the server.
Recognize()
Performs OCR on a given image.
Syntax .Addon.OCRPro.Recognize(nImageIndex, asyncSuccessFunc, asyncFailureFunc);
Parameters
  • number nImageIndex: Specifies the index of the image in buffer. The index is 0-based.
  • <OnOCRSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. The arguments are

    number nImageIndexThe index of the image.
    OCRResult ResultThe OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails. The arguments are

    number nErrorCodeThe error code.
    string strErrorStringThe error string.
    OCRResult ResultThe OCR result for the image.
Return value None
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes Check out the sample code and the details for OCRResult.
RecognizeFile()
Performs OCR on local files directly without loading them in the viewer.
Syntax .Addon.OCRPro.RecognizeFile(strFileNames, asyncSuccessFunc, asyncFailureFunc);
Parameters
  • string strFileNames: Specifies the local paths of the target files. If multiple files are given, they should be separated by the '|' character.
  • <OnOCRSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. The arguments are

    string strFileNamesThe file paths.
    OCRResult ResultThe OCR result for the image.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails. The arguments are

    number nErrorCodeThe error code.
    string strErrorStringThe error string.
    OCRResult ResultThe OCR result for the image.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes Check out the sample code and the details for OCRResult.
RecognizeRect()
Peforms OCR on the given rectangle on a specified image.
Syntax .Addon.OCRPro.RecognizeRect(nImageIndex, aryZones, asyncSuccessFunc, asyncFailureFunc);
Parameters
  • number nImageIndex: Specifies the index of the image in buffer. The index is 0-based.
  • Array aryZones: An array of OCRZone's created by the method Dynamsoft.WebTwain.Addon.OCRPro.NewOCRZone to specify the coordinates of the rectangle(s) for OCR.
  • <OnOCRSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. The arguments are

    number nImageIndexThe index of the image.
    OCRResult ResultThe OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails. The arguments are

    number nErrorCodeThe error code.
    string strErrorStringThe error string.
    OCRResult ResultThe OCR result for the image.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes Check out the sample code and the details for OCRResult.
RecognizeSelectedImages()
Performs OCR on the currently selected images in buffer.
Syntax .Addon.OCRPro.RecognizeSelectedImages(asyncSuccessFunc, asyncFailureFunc);
Return value boolean
Parameters
  • <OnOCRSelectedImagesSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. The only argument is

    OCRResult ResultThe OCR result for the image.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails. The arguments are

    number nErrorCodeThe error code.
    string strErrorStringThe error string.
    OCRResult ResultThe OCR result for the image.
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes Check out the sample code and the details for OCRResult.

Properties

Settings
Returns or sets the parameters for OCR.
Type Dynamsoft.WebTwain.Addon.OCRPro.NewSettings
Accessors Get Set
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes Check out Settings

New Settings

Name Type Description
Languages EnumDWT_OCRLanguage The language to OCR. E.g., "eng" or OCRL_ENG
LicenseChecker string A string that specify the url for the license checker
OutputFormat EnumDWT_OCROutputFormat Specify the output format. E.g.,OCROF_PDFIMAGEOVERTEXT
PDFAVersion EnumDWT_OCRProPDFAVersion Specify the PDFA version. E.g., OCRPPDFAV_1A
PDFVersion EnumDWT_OCRProPDFVersion Specify the PDF version. E.g., OCRPPDFV_0
RecognitionModule EnumDWT_OCRProRecognitionModule Specify the recognition module. E.g., OCRPM_AUTO
Redaction Redaction An object that specifies the redaction

Redaction

Name Type Description
FindText string The text to find. E.g., "twain".
FindTextFlags EnumDWT_OCRFindTextFlags Specify how the finding works.
FindTextAction EnumDWT_OCRFindTextAction Specify the action for redaction.

OCRResult

An object of the type OCRResult is returned which contains the OCR result. Methods of the object are

Name Description
Get() Returns a base64 encoded string that contains the result of the OCR operation.
GetErrorCode() Returns the OCR error code.
GetErrorString() Returns the OCR error string.
GetErrorDetailList() Returns an array which contains detailed error information for each page that was OCR'd. Check out ErrorInfo.
GetInput() Returns the input information of the OCR processing methods. The input could be the indices of the images in buffer or the local file paths.
Save() Saves the OCR result as a file (.txt, .pdf, etc.) on the local disk.
GetOCRTotalCount() Returns how many pages are allowed to be OCR'd by the current license. E.g. 300000.
GetAlreadyOCRCount() Returns how many pages have been OCR'd.
GetPageCount() Returns how many pages there are in the OCR result.
GetPageContent(nPageIndex) Returns the content (PageResult) of the page specified by nPageIndex.

PageResult

An object of the type PageResult is returned by GetPageContent(nPageIndex). Methods of the object are

Name Description
GetLetterCount() Returns how many letters are recognized on the specified page.
GetLetterContent(nLetterIndex) Returns the content (LetterResult) of the specified letter.
GetZoneCount() Returns how many zones are recognized on the specified page.
GetZoneContent(nZoneIndex) Returns the base64-encoded content of the specified zone.

LetterResult

An object of the type LetterResult is returned by GetLetterContent(nLetterIndex). Methods of the object are

Name Description
GetText() Returns the text of the specified word in the OCR result.
GetLetterRect() Returns the coordinates for the rectangle that contains a specified letter. The coordinates string is in the format of "left,top,right,bottom".

ErrorInfo

The following are the methods in each ErrorInfo object.

Name Description
GetInput() Returns the file path or the index of the Input.
GetMessage() Returns the error message.
GetPage() Returns the number of the page on which the error was thrown. If GetInput() returns a file path, then this returns the index of the page in that file. If GetInput() returns an index, then GetPage() is always "0".

Server-Side

The OCR Pro engine runs as a service. The process of server-side OCR is

  1. The image(s) is uploaded to the server and saved
  2. The path(s) of the saved image(s) is sent to the OCR pro service as part of the OCRPro.ServerSide.Request
  3. The service returns the OCR result in a HTTP Response

The following demonstrates the structures of the Request and the Response

OCRPro.ServerSide.Request

{
    productKey: "***",
    inputFile: ["d:\\input\\1.tif"],
    outputFile: " d:\\temp\\ocrresult.pdf",
    zones: [[100, 100, 200, 300]],    
    settings:
    {
        recognitionModule: "auto", /*optional*/
        languages: "eng,arabic",
        recognitionMethod: "File",
        threadCount: "2", /*optional*/
        outputFormat: "IOTPDF",
        pdfVersion: "1.7", /*optional*/
        pdfAVersion: "pdf/a-2a", /*optional*/
        redaction:
            {
            "findText": "AAA",
            "findTextFlags": 1,
            "findTextAction": 0
            }
    }
}
API Description
productKey The product key which is generated from an OCR license.
inputFile Specifies the files to be OCR'd. This is an array of strings which are absolute paths of the files. The supported formats are BMP, JPG, TIF, PDF, PNG, JBIG2, JPEG2000, PCX, etc. Please note the use of '\\' instead of just '\'.
outputFile Specifies where the output file is saved. If the input includes more than one file, all of them will be merged into one file. Otherwise, the result will only be returned in the OCRPro.ServerSide.Response.
zones Specifies which zones are to be OCR'd on an image. There can be multiple zones but it works only when the recognitionMethod is Page. The coordinates are in the sequence of [[left, top, right, bottom]].
settings
.recognitionModule Specifies which Module is to be used for this OCR. Allowed values are:

mostaccurateMost accurate but time consuming
fastestTakes the least time but not very accurate
balancedMaintains a balance between accuracy and performance
autoAutomatically use one of the aboove 3 modules, this is the default value
.languages Specifies the language for this OCR. For example, English: "eng", Arabic :"arabic". You can also set multiple languages like this "eng,arabic". The supported languages are "ara / arabic; ces / czech; dan / danish; deu / german; ell / greek; eng / english; fra / french; fin / finnish; hun / hungar; ita / italian; nld / duntch; nor / norsk; por / port; pol / polish; rus / russian; swe / swedish; spa / spanish; tur / turkish";
.recognitionMethod Specifies how the OCR is performed. There are two methods: Page is the default value and it means the OCR is performed on one page at a time, the other method is File which means the OCR is performed on one file at a time. The method File is faster and it supports multiple threads. But only the method Page supports zonal OCR and returning detailed results like the coordinates for each recognized letter.
.threadCount Specifies the maximum number of threads to be used for this OCR. The default value is -1 which means all possible threads will be used. This setting is only valid when recognitionMethod is set to File.
.outputFormat Specifies the file type for outputting the OCR result. Allowed values are

FormatDescription
TXTSStandard text file.
TXTCSVCSV text file.
TXTFFormatted Text file.
XMLSimple XML file.
IOTPDFImage over text PDF file.
IOTPDF_MRCImage over text PDF with MRC technology.
.pdfVersion Specifies the version of the PDF file if the outputFormat is set to either IOTPDF or IOTPDF_MRC. The version number allowed are 1.0 to 1.7 and by default it is 1.5.
.pdfAVersion Specifies the version of the PDF file if the outputFormat is set to either IOTPDF or IOTPDF_MRC. The version number allowed are "pdf/a-1a","pdf/a-1b","pdf/a-2a "," pdf/a-2b "," pdf/a-2u ", " pdf/a-3a ","pdf/a-3b","pdf/a-3u".
.redaction Specifies how the redaction is done.

OptionDescription
findTextA string to specify what to find.
findTextFlagsSpecifies how the text is found. The allowed values are 1 (WHOLEWORD), 2 (MATCHCASE), 4 (FUZZYMATCH).
findTextActionSpecifies the action once the text is found. The allowed values are 0 (HIGHLIGHT), 1 (STRIKEOUT) or 2 (MARKFORREDACT).

OCRPro.ServerSide.Response

{
    "Request": {
        inputFile: ["d:\\input\\1.tif"],
        settings: {...},
        outputFile: ...
    }
    ocrTotalCount: 300000,
    alreadyOCRCount: 80,
    code: 0,
    message: "Recognize succeeded.",
    errorList: 
        [
            {
                "input": "d:\\input\\1.tif",
                "message": "Image file format error.",
                "page": "1"
            }
        ]
    resultFile: "***", //base64-encoded file content
    resultZoneDetail : //only available when recognitionMethod is set to Page
        [
            ["zone1 words", "zone2 words", "zone3 words"], // page 1
            ["zone1 words", "zone2 words"], // page 2
            ["zone1 words", "zone2 words"] // page 3
        ],
    resultDetail:
        [
            [//page 0
                {//letter 0
                    "letter": "Aa",
                    "boundary": [0,0,18,18]
                },
                {//letter 1
                    ...
                },
                ...
            ],
            [//page 1
                ...
            ],
            ...    
        ]  
}
API Description
inputFile Check out OCRPro.ServerSide.Request for more info.
settings Check out OCRPro.ServerSide.Request for more info.
outputFile Check out OCRPro.ServerSide.Request for more info.
ocrTotalCount Returns how many pages are allowed to be OCR'd by the current license.
alreadyOCRCount Returns how many pages have already been OCR'd.
code Returns the error code for the OCR. If it's not 0, check errorList for more details.
message Returns the overall error message.
errorList Returns the detailed error messages for each of the OCR'd files.
resultFile Returns the result file encoded as a base64 string. It only works when OCRPro.ServerSide.Request doesn't specify an output file path.
resultDetail Returns detailed OCR result down to each found letter in JSON format. This is only valid when the recognitionMethod is set to Page.

results matching ""

    No results matching ""

    results matching ""

      No results matching ""