Nicomsoft OCR: Developer's Guide


Blk_SetRect


Syntax

C++:int Blk_SetRect(HBLK BlkObj, int Xpos, int Ypos, int Width, int Height)
C#:int Blk_SetRect(int BlkObj, int Xpos, int Ypos, int Width, int Height)
Visual Basic:Function Blk_SetRect(ByVal BlkObj As Integer, ByVal Xpos As Integer, ByVal Ypos As Integer, ByVal Width As Integer, ByVal Height As Integer) As Integer
Java:int Blk_SetRect(HBLK BlkObj, int Xpos, int Ypos, int Width, int Height)
Delphi:function Blk_SetRect(BlkObj:HBLK; Xpos:integer; Ypos:integer; Width:integer; Height:integer):integer


Description

Changes the position and size of the block. The borders of the block must be inside the image.


Parameters

BlkObj [IN] – the Block object.
Xpos [IN] – the new X coordinate of the top-left corner of the block, in pixels.
Ypos [IN] – the new Y coordinate of the top-left corner of the block, in pixels.
Width [IN] – the new width of the block, in pixels.
Height [IN] – the new height of the block, in pixels.


Return value

Zero if success, otherwise an error code.


Remarks

1. If the size or position of the inverted block was changed after OCRSTEP_BINARIZE, then OCRSTEP_BINARIZE will be executed again automatically at the OCRSTEP_OCR step.
2. If the block was created before the OCRSTEP_PREFILTERS step (though it is not recommended), its coordinates will be recalculated if the rescaling and deskewing algorithms are applied to the original image at this step.


Example

The following code defines one text zone for the top half of an image, and recognizes it using a regular expression:

C++
int CfgObj, OcrObj, ImgObj, BlkObj, res, n, Width, Height;
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
Img_GetSize(ImgObj, &Width, &Height); //get original image size
Img_AddBlock(ImgObj, 0, 0, Width, Height, &BlkObj); //create zone that covers entire image
Blk_SetRect(BlkObj, 0, 0, Width, Height / 2); //resize zone; just a sample, correct zone size could be specified at Img_AddBlock call
Blk_SetType(BlkObj, BT_OCRTEXT); //mark zone as machine-printed text; just a sample, blocks that were created with Img_AddBlock already BT_OCRTEXT
Blk_SetWordRegEx(BlkObj, -1, -1, L"*", REGEX_SET); //set regular expression for every word in zone
res = Img_OCR(ImgObj, OCRSTEP_FIRST, OCRSTEP_LAST, OCRFLAG_NONE); //perform OCR for top half of image
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, BlkObj, res, Width, Height;
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
NsOCR.Img_GetSize(ImgObj, out Width, out Height); //get original image size
NsOCR.Img_AddBlock(ImgObj, 0, 0, Width, Height, out BlkObj); //create zone that covers entire image
NsOCR.Blk_SetRect(BlkObj, 0, 0, Width, Height / 2); //resize zone; just a sample, correct zone size could be specified at Img_AddBlock call
NsOCR.Blk_SetType(BlkObj, TNSOCR.BT_OCRTEXT); //mark zone as machine-printed text; just a sample, blocks that were created with Img_AddBlock already BT_OCRTEXT
NsOCR.Blk_SetWordRegEx(BlkObj, -1, -1, "*", TNSOCR.REGEX_SET); //set regular expression for every word in zone
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
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, BlkObj, res, Width, Height 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
NsOCR.Img_GetSize(ImgObj, Width, Height) 'get original image size
NsOCR.Img_AddBlock(ImgObj, 0, 0, Width, Height, BlkObj) 'create zone that covers entire image
NsOCR.Blk_SetRect(BlkObj, 0, 0, Width, Height / 2) 'resize zone; just a sample, correct zone size could be specified at Img_AddBlock call
NsOCR.Blk_SetType(BlkObj, TNSOCR.BT_OCRTEXT) 'mark zone as machine-printed text; just a sample, blocks that were created with Img_AddBlock already BT_OCRTEXT
NsOCR.Blk_SetWordRegEx(BlkObj, -1, -1, "*", TNSOCR.REGEX_SET) 'set regular expression for every word in zone
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
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();
NSOCR.HBLK BlkObj = new NSOCR.HBLK();
int res; 
NSOCR.NSInt Width, Height;      
Width = new NSOCR.NSInt(0);
Height = new NSOCR.NSInt(0); 
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
NSOCR.Engine.Img_GetSize(ImgObj, Width, Height); //get original image size
NSOCR.Engine.Img_AddBlock(ImgObj, 0, 0, Width.GetValue(), Height.GetValue(), BlkObj); //create zone that covers entire image
NSOCR.Engine.Blk_SetRect(BlkObj, 0, 0, Width.GetValue(), Height.GetValue() / 2); //resize zone; just a sample, correct zone size could be specified at Img_AddBlock call
NSOCR.Engine.Blk_SetType(BlkObj, NSOCR.Constant.BT_OCRTEXT); //mark zone as machine-printed text; just a sample, blocks that were created with Img_AddBlock already BT_OCRTEXT
NSOCR.Engine.Blk_SetWordRegEx(BlkObj, -1, -1, "*", NSOCR.Constant.REGEX_SET); //set regular expression for every word in zone
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