OCR Basic

For Server Side OCR, check out Server-Side OCR

Client-Side

Methods
Download() DownloadLangData() GetIfUseDetectedFont()
GetMinFontSizeforMoreAccurateResult() GetUnicodeFontName() IsModuleInstalled()
Recognize() RecognizeFile() RecognizeRect()
RecognizeSelectedImages SetIfUseDetectedFont() SetLanguage()
SetMinFontSizeforMoreAccurateResult() SetOutputFormat() SetPageSetMode()
SetUnicodeFontName()
OCR Basic Result Object
Result PageSetResult PageResult
LineResult WordResult

Code example

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

function DoOCR(inputType) {
    var inputFilePath = "D:\\OCRInput\\OCRInput.jpg",
        outputFilePath = "D:\\OCRResult\\OCResult.pdf",
        strHTTPServer = location.hostname,
        CurrentPathName = unescape(location.pathname),
        CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1),
        appPath = "";
    if (location.port == 80)
        appPath = "http://" + strHTTPServer + CurrentPath;
    else
        appPath = "http://" + strHTTPServer + ":" + location.port + CurrentPath;

    if (DWObject.Addon.OCR.IsModuleInstalled()) {
        DWObject.Addon.OCR.SetLanguage(EnumDWT_OCRLanguage.OCRL_ENG);
        DWObject.Addon.OCR.SetOutputFormat(EnumDWT_OCROutputFormat.OCROF_PDFIMAGEOVERTEXT_PDFX);
        DWObject.Addon.OCR.DownloadLangData(
            appPath + "OCRBResources/OCRBasicLanguages/English.zip",
            function () {
                switch (inputType) {
                    case 'SinglePage': //OCR only the current image
                        DWObject.Addon.OCR.Recognize(DWObject.CurrentImageIndexInBuffer,
                            function (index, result) {
                                result.Save(outputFilePath);
                            },
                            function (errorCode, errorString) { alert(errorString); }
                        );
                        break;
                    case 'Multi-Page': //OCR first 3 images
                        DWObject.Addon.OCR.RecognizeSelectedImages([0, 1, 2],
                            function (result) {
                                result.Save(outputFilePath);
                            },
                            function (errorCode, errorString) { alert(errorString); }
                        );
                        break;
                    case 'File': //OCR a local file
                        DWObject.Addon.OCR.RecognizeFile(inputFilePath,
                            function (file, result) {
                                result.Save(outputFilePath);
                            },
                            function (errorCode, errorString) { alert(errorString); }
                        );
                        break;
                    case 'Rect': //OCR only the top left corner (200*200) of an image
                        DWObject.Addon.OCR.RecognizeRect(
                            DWObject.CurrentImageIndexInBuffer, 0, 0, 200, 200,
                            function (index, left, top, right, bottom, result) {
                                result.Save(outputFilePath);
                            },
                            function (errorCode, errorString) { alert(errorString); }
                        );
                        break;
                }
            },
            function (errorCode, errorString) { alert(errorString); }
        );
    } else {
        DWObject.Addon.OCR.Download(appPath + "OCRBResources/OCR.zip",
            function () {
                DoOCR();
            },
            function (errorCode, errorString) {
                alert(errorString);
            }
        );
    }
}

Methods

Download()
Downloads and installs the OCR add-on (it's typically a zipped dll file) on the local system.
Syntax .Addon.OCR.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().
DownloadLangData()
Downloads an OCR language package from the server.
Syntax .Addon.OCR.DownloadLangData(sLanguagePackagePath, [optionalAsyncSuccessFunc, optionalAsyncFailureFunc]);
Parameters
  • string sLanguagePackagePath: specifies the url path of the language package on the HTTP server. E.g. "http://www.dynamsoft.com/DWT/Resources/OCRBasicLanguages/English.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 language package from the server.
GetIfUseDetectedFont()
Returns whether the output uses the fonts detected by the OCR system or the default/provided ones. Only used when the result format is PDF.
Syntax .Addon.OCR.GetIfUseDetectedFont();
Parameters
  • None
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes None
SetIfUseDetectedFont()
Sets whether the output uses the fonts detected by the OCR system or the default/provided ones. Only used when the result format is PDF.
Syntax .Addon.OCR.SetIfUseDetectedFont(bValue);
Parameters
  • boolean bValue: true or false.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes If you need to call the method Addon.OCR.SetUnicodeFontName(), you must first call this method with bValue set to true.
GetMinFontSizeforMoreAccurateResult()
Returns the font size base to apply higher-level regional accurate OCR. The font size base is set by the method Addon.OCR.SetMinFontSizeforMoreAccurateResult().
Syntax .Addon.OCR.GetMinFontSizeforMoreAccurateResult();
Parameters
  • None
Return value number
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes If the return value is 0, it indicates no regional accurate OCR will be performed.
SetMinFontSizeforMoreAccurateResult()
Sets the font size base. Once set, higher-level accurate OCR will be performed to areas of the image where the font size is bigger.
Syntax .Addon.OCR.SetMinFontSizeforMoreAccurateResult(nValue);
Parameters
  • number nValue: Specifies the font size base.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes The default font size base is 0 which means no regional accurate OCR will be performed.
GetUnicodeFontName()
Returns the detected OCR font name.
Syntax .Addon.OCR.GetUnicodeFontName();
Parameters
  • None
Return value string
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes None
SetUnicodeFontName()
Specifies a font to be used by OCR when Addon.OCR.SetIfUseDetectedFont(false) is called.
Syntax .Addon.OCR.SetUnicodeFontName(strName);
Parameters
  • string strName: Specifies a font by its name to be used by OCR.
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes strName should be the name of a font in the working directory or Windows font directory(C:\Windows\Fonts). The given name should not include the .font extension. The definition of a "unicode" font is loose. Any font can be provided here, however it needs to support the characters of the language to be used. Some fonts may support only some languages, but certain fonts such as ArialUni can support all common languages.
IsModuleInstalled()
Returns whether the OCR module is already installed on the local system.
Syntax .Addon.OCR.IsModuleInstalled();
Parameters
  • None
Return value boolean
Availability Versions: v14.1+
Editions: HTML5 for Windows only
Usage notes If the method returns false, you can use the method Download() to download and install the dll from the server.
SetLanguage()
Downloads and installs the OCR add-on (it's typically a zipped dll file) on the local system.
Syntax .Addon.OCR.SetLanguage(strOCRLanguage);
Parameters
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes The default value is "eng" which indicates English. To use a certain language, you must first have it ready locally, if it's not available yet, you can download it using the method Addon.OCR.DownloadLangData().
SetOutputFormat()
Sets the OCR result format.
Syntax .Addon.OCR.SetOutputFormat(OCRResultFormat);
Parameters
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes By default the value is EnumDWT_OCROutputFormat.OCROF_PDFIMAGEOVERTEXT indicating the output is an Image-over-Text PDF that looks just like the original image but with searchable text.
SetPageSetMode()
Sets the OCR page layout analysis mode which is used to determine how pages are separated.
Syntax .Addon.OCR.SetPageSetMode(OCRPageSetMode);
Parameters
Return value boolean
Availability
ActiveXH5(Windows)H5(macOS/TWAIN)H5(macOS/ICA)H5(Linux)
✓ | v11.3.2
Usage notes The default value is 3, which indicates automatic page detection. This is fine for most applications.
Recognize()
Performs OCR on a given image.
Syntax .Addon.OCR.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. This is the place to check the OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails.
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 result.
RecognizeFile()
Performs OCR on local files directly without loading them in the viewer.
Syntax .Addon.OCR.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. This is the place to check the OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails.
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 result.
RecognizeRect()
Peforms OCR on the given rectangle on a specified image.
Syntax .Addon.OCR.RecognizeRect(nImageIndex, nLeft, nTop, nRight, nBottom, asyncSuccessFunc, asyncFailureFunc);
Parameters
  • number nImageIndex: Specifies the index of the image in buffer. The index is 0-based.
  • number nLeft: Specifies the x-coordinate of the upper-left corner of the rectangle.
  • number nTop: Specifies the y-coordinate of the upper-left corner of the rectangle.
  • number nRight: Specifies the x-coordinate of the lower-right corner of the rectangle.
  • number nBottom: Specifies the y-coordinate of the lower-right corner of the rectangle.
  • <OnOCRSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. This is the place to check the OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails.
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 result.
RecognizeSelectedImages()
Performs OCR on the currently selected images in buffer.
Syntax .Addon.OCR.RecognizeSelectedImages(asyncSuccessFunc, asyncFailureFunc);
Parameters
  • <OnOCRSuccess function> asyncSuccessFunc: Callback function triggered when the OCR executed successfully. This is the place to check the OCR result.
  • <OnOCRFailure function> asyncFailureFunc: Callback function triggered when the OCR operation fails.
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 result.

Result

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

Name Description
Get() Returns a Unicode string that contains the result of the OCR operation. Newlines are represented by the single newline character: '\n'.
GetErrorCode() Returns the OCR error code.
GetErrorString() Returns the OCR error string.
GetFormat() Returns the OCR output format. Check out EnumDWT_OCROutputFormat.
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 .txt file or a .pdf file on the local disk.
GetPageSetCount() Returns how many pagesets are included in the OCR result.
GetPageSetContent(nPageSetIndex) Returns the content (PageSetResult) of a pageset in the OCR result. The pageset is specified by nPageSetIndex. Check out the following for more information.

PageSetResult

An object of the type PageSetResult is returned by GetPageSetCount(nPageSetIndex). Methods of the object are

Name Description
GetPageCount() Returns how many pages are included in the pageset.
GetPageContent(nPageIndex) Returns the content (PageResult) of the specified page.

PageResult

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

Name Description
GetLineCount() Returns how many lines are included in the specified page.
GetLineContent(nLineIndex) Returns the content (LineResult) of the specified line.

LineResult

An object of the type LineResult is returned by GetLineContent(nLineIndex). Methods of the object are

Name Description
GetWordCount() Returns how many words are included in the specified line.
GetLineRect() Returns the coordinates for the rectangle that contains a specified line.
GetWordContent(nWordIndex) Returns the content (WordResult) of the specified word.

WordResult

An object of the type WordResult is returned by GetWordContent(nWordIndex). Methods of the object are

Name Description
GetText() Returns the text of the specified word in the OCR result.
GetFontName() Returns the font name of a specified word in the OCR result.
GetFontSize() Returns the font size of a specified word in the OCR result.
GetWordRect() Returns the coordinates for the rectangle that contains a specified word.

Server-Side

The following are the JAVA APIs of the server-side OCR engine. Currently, only Windows server is supported.

API Description
String getOCRDllPath() Returns the path of the OCR library.
String getOCRLanguage() Returns the target language for the OCR.
int getOCRMinFontSizeDoMoreOCR() Returns the font size which determines whether the engine should perform OCR again on areas with bigger font size.
int getOCRPageSetMode() Returns the mode used to analyze the OCR input.
int getOCRPdfFontSize() Returns the font size for the output PDF file.
int getOCRResultFormat() Returns the result format for the OCR.
String getOCRTessDataPath() Returns the path of the language packages.
String getOCRUnicodeFontName() Returns the detected OCR font name.
boolean isOCRNumbericOnly() Returns whether the OCR engine will only OCR numbers.
boolean isOCRUseDetectedFont() Returns whether PDF output uses the fonts detected by the OCR engine.
byte array ocrFile(String strImagePath, out byte array aryDetailedOCRResult) Starts to OCR an image on the disk. aryDetailedOCRResult returns details of the OCR result which you can get by calling getValue on it.
byte array ocrImage(byte array aryImageBuffer, out byte array aryDetailedOCRResult) Starts to OCR an image in buffer. aryDetailedOCRResult returns details of the OCR result which you can get by calling getValue on it.
void setOCRDllPath(String strOCRDllPath) Specifies the path of the OCR library.
void setOCRIsNumbericOnly(boolean bNumbericOnly) Specifies whether the OCR engine should only OCR numbers.
void setOCRLanguage(String strOCRLanguage) Specifies the target language for the OCR.
void setOCRMinFontSizeDoMoreOCR(int iMinFontSizeDoMoreOCR) The engine will perform OCR again on areas where the font size is bigger than what's set by this API.
void setOCRPageSetMode(int iOCRPageSetMode) Specifies how pages are determined when analyzing the OCR input. More info.
void setOCRPdfFontSize(int iPdfFontSize) Specifies the font size for the output PDF file.
void setOCRResultFormat(int iOCRResultFormat) Specifies the result format for the OCR. The allowed values are the same as the client-side.
void setOCRTessDataPath(String strOCRTessDataPath) Specifies the path of the language packages.
void setOCRUnicodeFontName(String strOCRUnicodeFontName) Specifies a font to be used by OCR when isOCRUseDetectedFont returns false.
void setOCRUseDetectedFont(boolean bUseDetectedFont) Determines whether the PDF output uses the fonts detected by the OCR engine or the default/provided fonts.
void setProductKey(String strProductKey) Sets the ProductKey of the SDK.

results matching ""

    No results matching ""

    results matching ""

      No results matching ""