Nicomsoft OCR: Developer's Guide


Img_AddBlock


Syntax

C++:int Img_AddBlock(HIMG ImgObj, int Xpos, int Ypos, int Width, int Height, HBLK* BlkObj)
C#:int Img_AddBlock(int ImgObj, int Xpos, int Ypos, int Width, int Height, out int BlkObj)
Visual Basic:Function Img_AddBlock(ByVal ImgObj As Integer, ByVal Xpos As Integer, ByVal Ypos As Integer, ByVal Width As Integer, ByVal Height As Integer, ByRef BlkObj As Integer) As Integer
Java:int Img_AddBlock(HIMG ImgObj, int Xpos, int Ypos, int Width, int Height, HBLK BlkObj)
Delphi: function Img_AddBlock(ImgObj:HIMG; Xpos:integer; Ypos:integer; Width:integer; Height:integer; out BlkObj:HBLK):integer


Description

Creates a new Block object and adds it to the list of Block objects assigned to the specified Image object. Each Block object marks some area in the image. Only marked areas will be OCR’ed with the Img_OCR function. The borders of the block must be inside the image. New created block objects always have the BT_OCRTEXT type (see the Blk_SetType function for details about block types). It is recommended to create blocks after the OCRSTEP_PREFILTERS step. Otherwise, the positions/sizes of the blocks will be recalculated at the OCRSTEP_PREFILTERS step when the original image is rescaled/deskewed for the best recognition quality.


Parameters

ImgObj [IN] – the Image object.
Xpos [IN] – the X coordinate of the top-left corner of the block, in pixels.
Ypos [IN] – the Y coordinate of the top-left corner of the block, in pixels.
Width [IN] – the block width, in pixels.
Height [IN] – the block height, in pixels.
BlkObj [OUT] – the address of the HBLK variable that will get the new Block object.


Return value

Zero if success, otherwise an error code.


Remarks

If any blocks were added manually, the OCRSTEP_ZONING step will do nothing unless there are some BT_ZONING blocks. If no blocks were added manually, the OCRSTEP_ZONING step will perform autozoning; therefore, don’t create any blocks if you need autozoning. And vice versa, create blocks with this function before the OCRSTEP_ZONING step if you don’t need autozoning.


Example

1. See the sample code for the Blk_SetRect function.
2. The following code demonstrates how to skip autozoning and create one text zone that covers the entire image:

C++
int CfgObj, OcrObj, ImgObj, BlkObj, res, n, w, h;
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_ZONING - 1, OCRFLAG_NONE); //perform some OCR steps, stop before autozoning step
if (res > ERROR_FIRST) {}; //insert error handler here
Img_GetSize(ImgObj, &w, &h); //get image size. It is not original image size, it could be resized at OCRSTEP_PREFILTERS step
Img_AddBlock(ImgObj, 0, 0, w, h, &BlkObj); //create text zone that covers entire image
//now execute the rest of OCR steps. OCRSTEP_ZONING step will do nothing since we created block manually
res = Img_OCR(ImgObj, OCRSTEP_ZONING, OCRSTEP_LAST, OCRFLAG_NONE); 
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(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, BlkObj, w, h, 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_ZONING - 1, TNSOCR.OCRFLAG_NONE); //perform some OCR steps, stop before autozoning step
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
NsOCR.Img_GetSize(ImgObj, out w, out h); //get image size. It is not original image size, it could be resized at OCRSTEP_PREFILTERS step
NsOCR.Img_AddBlock(ImgObj, 0, 0, w, h, out BlkObj); //create text zone that covers entire image
//now execute the rest of OCR steps. OCRSTEP_ZONING step will do nothing since we created block manually
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_ZONING, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE); 
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, w, h, 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_ZONING - 1, TNSOCR.OCRFLAG_NONE) 'perform some OCR steps, stop before autozoning step
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
NsOCR.Img_GetSize(ImgObj, w, h) 'get image size. It is not original image size, it could be resized at OCRSTEP_PREFILTERS step
NsOCR.Img_AddBlock(ImgObj, 0, 0, w, h, BlkObj) 'create text zone that covers entire image
'now execute the rest of OCR steps. OCRSTEP_ZONING step will do nothing since we created block manually
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_ZONING, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE) 
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;
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.NSInt w = new NSOCR.NSInt(0);
NSOCR.NSInt h = new NSOCR.NSInt(0);
res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_FIRST, NSOCR.Constant.OCRSTEP_ZONING - 1, NSOCR.Constant.OCRFLAG_NONE); //perform some OCR steps, stop before autozoning step
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
NSOCR.Engine.Img_GetSize(ImgObj, w, h); //get image size. It is not original image size, it could be resized at OCRSTEP_PREFILTERS step
NSOCR.Engine.Img_AddBlock(ImgObj, 0, 0, w.GetValue(), h.GetValue(), BlkObj); //create text zone that covers entire image
//now execute the rest of OCR steps. OCRSTEP_ZONING step will do nothing since we created block manually
res = NSOCR.Engine.Img_OCR(ImgObj, NSOCR.Constant.OCRSTEP_ZONING, NSOCR.Constant.OCRSTEP_LAST, NSOCR.Constant.OCRFLAG_NONE); 
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