Nicomsoft OCR: Developer's Guide


Img_LoadBlocks


Syntax

C++:int Img_LoadBlocks(HIMG ImgObj, UNICODECHAR* FileName)
C#:int Img_LoadBlocks(int ImgObj, string FileName)
Visual Basic:Function Img_LoadBlocks(ByVal ImgObj As Integer, ByVal FileName As String) As Integer
Java:int Img_LoadBlocks(HIMG ImgObj, String FileName)
Delphi:function Img_LoadBlocks(ImgObj:HIMG; FileName:PWCHAR):integer


Description

Imports blocks (zones) from a file. You can create a file containing the zones data with the Img_SaveBlocks function. When this function is called, the existing blocks are not removed. If you need to remove the existing blocks before loading new ones, use the Img_DeleteAllBlocks function.


Parameters

ImgObj [IN] – the Image object.
FileName [IN] – a Unicode, null-terminated string that contains the full path and filename of the file with blocks data. If an empty string is specified, a special inner-memory buffer will be used as a source; this mode is good for saving/loading blocks without using any files.


Return value

Zero if success, otherwise an error code.


Remarks

It is better to call the Img_LoadBlocks and Img_SaveBlocks functions after the OCRSTEP_PREFILTERS step, because an image can be resized/deskewed/rotated at that step. NSOCR takes into account such actions, so in many cases you can save zones as a template before the OCRSTEP_PREFILTERS step, and load them after that step. However, the recalculation of zone positions sometimes fails, so we recommend calling these functions after the OCRSTEP_PREFILTERS step.


Example

The following code loads an image and recognizes it using saved zones (you can create zones.xml with any Advanced Sample from the NSOCR SDK):

C++
int CfgObj, OcrObj, ImgObj, res, n;
wchar_t* txt;
Engine_InitializeAdvanced(&CfgObj, &OcrObj, &ImgObj); //initialize OCR engine, create objects and load configuration
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_PREFILTERS, OCRFLAG_NONE); //execute some OCR steps
if (res > ERROR_FIRST) {}; //insert error handler here
res = Img_LoadBlocks(ImgObj, L"c:\\zones.xml"); //load zones from file
if (res > ERROR_FIRST) {}; //insert error handler here
res = Img_OCR(ImgObj, OCRSTEP_PREFILTERS + 1, OCRSTEP_LAST, OCRFLAG_NONE); //execute rest of OCR steps, autozoning will be skipped because we have loaded zones
if (res > ERROR_FIRST) {}; //insert error handler here
n = Img_GetImgText(ImgObj, NULL, 0, FMT_EXACTCOPY) + 1; //get buffer size plus terminating NULL character
txt = (wchar_t*) malloc(sizeof(wchar_t) * 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" value 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_InitializeAdvanced(out CfgObj, out OcrObj, out ImgObj); //initialize OCR engine, create objects and load configuration
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_PREFILTERS, TNSOCR.OCRFLAG_NONE); //execute some OCR steps
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
res = NsOCR.Img_LoadBlocks(ImgObj, "c:\\zones.xml"); //load zones from file
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_PREFILTERS + 1, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE); //execute rest of OCR steps, autozoning will be skipped because we have loaded zones
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
NsOCR.Img_GetImgText(ImgObj, out txt, TNSOCR.FMT_EXACTCOPY); //get text
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_InitializeAdvanced(CfgObj, OcrObj, ImgObj) 'initialize OCR engine, create objects and load configuration
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_PREFILTERS, TNSOCR.OCRFLAG_NONE) 'execute some OCR steps
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
res = NsOCR.Img_LoadBlocks(ImgObj, "c:\zones.xml") 'load zones from file
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_PREFILTERS + 1, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE) 'execute rest of OCR steps, autozoning will be skipped because we have loaded zones
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
NsOCR.Img_GetImgText(ImgObj, txt, TNSOCR.FMT_EXACTCOPY) 'get text
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_InitializeAdvanced(CfgObj, OcrObj, ImgObj); //initialize OCR engine, create objects and load configuration
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_PREFILTERS, NSOCR.Constant.OCRFLAG_NONE); //execute some OCR steps
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
res = NSOCR.Engine.Img_LoadBlocks(ImgObj, "c:\\zones.xml"); //load zones from file
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_PREFILTERS + 1, NSOCR.Constant.OCRSTEP_LAST, NSOCR.Constant.OCRFLAG_NONE); //execute rest of OCR steps, autozoning will be skipped because we have loaded zones
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