Nicomsoft OCR: Developer's Guide


Engine_Initialize


Syntax

C++: int Engine_Initialize()
C#: int Engine_Initialize()
Visual Basic: Function Engine_Initialize as Integer
Java:int Engine_Initialize()
Delphi: function Engine_Initialize():integer


Description

Initializes the NSOCR engine. This function (or Engine_InitializeAdvanced) must be called before using any other NSOCR functions.


Parameters

None.


Return value

Zero if success, otherwise an error code.


Remarks

1. Call this function only once. Any subsequent calls without calling the Engine_Uninitialize function will be ignored. Initialization takes some time, and processing of the first image takes more time than processing of any subsequent images. For the best performance, this function should be called only once while your application is running.
2. To reduce the code size, use the Engine_InitializeAdvanced function instead.


Example

The following code initializes the OCR engine, creates OCR-related objects, and recognizes an image:

C++
int CfgObj, OcrObj, ImgObj, res, n;
wchar_t* txt;
Engine_Initialize(); //initialize OCR engine
Cfg_Create(&CfgObj); //create CFG object
Cfg_LoadOptions(CfgObj, L"Config.dat"); //load configuration
Ocr_Create(CfgObj, &OcrObj); //create OCR object
Img_Create(OcrObj, &ImgObj); //create IMG object
res = Img_LoadFile(ImgObj, L"c:\\sample.bmp"); //load some image for OCR
if (res > ERROR_FIRST) {}; //insert error handler here
res = Img_OCR(ImgObj, OCRSTEP_FIRST, OCRSTEP_LAST, OCRFLAG_NONE); //perform OCR
if (res > ERROR_FIRST) {}; //insert error handler here
n = Img_GetImgText(ImgObj, NULL, 0, FMT_EXACTCOPY) + 1; //get length in unicode characters plus terminating NULL character
txt = (wchar_t*) malloc(2 * n); //allocate memory for text
Img_GetImgText(ImgObj, txt, n, FMT_EXACTCOPY); //get text
Engine_Uninitialize(); //release all created objects and uninitialize OCR engine
//use "txt" variable now
free(txt); //free memory


C#
//assume reference to NSOCR COM was added
using NSOCR_NameSpace; //Add NSOCR namespace from "NSOCR.cs" file
//...
int CfgObj, OcrObj, ImgObj, res;
string txt;
NSOCRLib.NSOCRClass NsOCR = new NSOCRLib.NSOCRClass(); //create NSOCR COM object instance
NsOCR.Engine_Initialize(); //initialize OCR engine
NsOCR.Cfg_Create(out CfgObj); //create CFG object
NsOCR.Cfg_LoadOptions(CfgObj, "Config.dat"); //load configuration
NsOCR.Ocr_Create(CfgObj, out OcrObj); //create OCR object
NsOCR.Img_Create(OcrObj, out ImgObj); //create IMG object
res = NsOCR.Img_LoadFile(ImgObj, "c:\\sample.bmp"); //load some image for OCR
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE); //perform OCR
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
NsOCR.Img_GetImgText(ImgObj, out txt, TNSOCR.FMT_EXACTCOPY); //get text
//use "txt" variable now
NsOCR.Engine_Uninitialize(); //release all created objects and uninitialize OCR engine


VB.NET
'assume reference to NSOCR COM was added
'assume "NSOCR.vb" file was added to project
Dim CfgObj, OcrObj, ImgObj, res As Integer
Dim txt As String = ""
Dim NsOCR As New NSOCRLib.NSOCRClass 'create NSOCR COM object instance
NsOCR.Engine_Initialize() 'initialize OCR engine
NsOCR.Cfg_Create(CfgObj) 'create CFG object
NsOCR.Cfg_LoadOptions(CfgObj, "Config.dat") 'load configuration
NsOCR.Ocr_Create(CfgObj, OcrObj) 'create OCR object
NsOCR.Img_Create(OcrObj, ImgObj) 'create IMG object
res = NsOCR.Img_LoadFile(ImgObj, "c:\sample.bmp") 'load some image for OCR
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE) 'perform OCR
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
NsOCR.Img_GetImgText(ImgObj, txt, TNSOCR.FMT_EXACTCOPY) 'get text
'use "txt" variable now
NsOCR.Engine_Uninitialize() 'release all created objects and uninitialize OCR engine
Java
//assume NSOCR package was included
//Java VM option "-Djava.library.path" must point to "Bin" folder (x86 platform) or to "Bin_64" folder (x64 platform)
//...
NSOCR.HCFG CfgObj = new NSOCR.HCFG();
NSOCR.HOCR OcrObj = new NSOCR.HOCR();
NSOCR.HIMG ImgObj = new NSOCR.HIMG();
int res;
StringBuffer txt = new StringBuffer();
NSOCR.Engine.Engine_Initialize(); //initialize OCR engine
NSOCR.Engine.Cfg_Create(CfgObj); //create CFG object
NSOCR.Engine.Cfg_LoadOptions(CfgObj, "Config.dat"); //load configuration
NSOCR.Engine.Ocr_Create(CfgObj, OcrObj); //create OCR object
NSOCR.Engine.Img_Create(OcrObj, ImgObj); //create IMG object
res = NSOCR.Engine.Img_LoadFile(ImgObj, "c:\\sample.bmp"); //load some image for OCR
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_FIRST, NSOCR.Constant.OCRSTEP_LAST, NSOCR.Constant.OCRFLAG_NONE); //perform OCR
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
NSOCR.Engine.Img_GetImgText(ImgObj, txt, NSOCR.Constant.FMT_EXACTCOPY); //get text
System.out.println(txt.toString());
NSOCR.Engine.Engine_Uninitialize(); //release all created objects and uninitialize OCR engine