Nicomsoft OCR: Developer's Guide


Blk_GetCharCnt


Syntax

C++:int Blk_GetCharCnt(HBLK BlkObj, int LineIndex, int WordIndex)
C#:int Blk_GetCharCnt(int BlkObj, int LineIndex, int WordIndex)
Visual Basic:Function Blk_GetCharCnt(ByVal BlkObj As Integer, ByVal LineIndex As Integer, ByVal WordIndex As Integer) As Integer
Java:int Blk_GetCharCnt(HBLK BlkObj, int LineIndex, int WordIndex)
Delphi:function Blk_GetCharCnt(BlkObj:HBLK; LineIndex:integer; WordIndex:integer):integer


Description

Retrieves the total number of characters in the specified word after performing OCR with the Img_OCR function. Use the Blk_GetLineCnt function to get the total number of text lines in the block. Use the Blk_GetWordCnt function to get the total number of words in the specified line.


Parameters

BlkObj [IN] – the Block object. You can also specify the Image object if you want to work with the global list of text lines for the entire image.
LineIndex [IN] – the index of the text line; 0 – the first line.
WordIndex [IN] – the index of the word in the specified text line; 0 - the first word.


Return value

If success, the total number of characters in the specified word; otherwise, an error code.


Remarks

The OCRSTEP_OCR step must be executed before calling this function.


Example

The following code recognizes an image and gets the properties of every character:

C++
int CfgObj, OcrObj, ImgObj, res, lcnt, wcnt, ccnt, i, j, k, n, Xpos, Ypos, 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
res = Img_OCR(ImgObj, OCRSTEP_FIRST, OCRSTEP_LAST, OCRFLAG_NONE); //perform OCR 
if (res > ERROR_FIRST) {}; //insert error handler here
lcnt = Blk_GetLineCnt(ImgObj); //get number of text lines for entire image
for (i = 0; i < lcnt; i++)
{
  wcnt = Blk_GetWordCnt(ImgObj, i); //get number of words of "i"-th text line
  for (j = 0; j < wcnt; j++)
  {
    ccnt = Blk_GetCharCnt(ImgObj, i, j); //get number of characters of "j"-th word of "i"-th text line
    for (k = 0; k < ccnt; k++)
    {   
      Blk_GetCharRect(ImgObj, i, j, k, &Xpos, &Ypos, &Width, &Height); //get character position and size
      res = Blk_GetCharQual(ImgObj, i, j, k, 0); //get character quality
      n = Blk_GetCharText(ImgObj, i, j, k, 0, NULL, 0) + 1; //get buffer size plus terminating NULL character
      txt = (wchar_t*) malloc(sizeof(wchar_t) * n); //allocate memory for text
      Blk_GetCharText(ImgObj, i, j, k, 0, txt, n); //get character text
      //...use "txt" value now
      free(txt); //free memory
    }
  }
}
Engine_Uninitialize(); //release all created objects and uninitialize OCR engine


C#
//assume reference to NSOCR COM was added
using NSOCR_NameSpace; //Add NSOCR namespace from "NSOCR.cs" file
//...
int CfgObj, OcrObj, ImgObj, res, lcnt, wcnt, ccnt, i, j, k, Xpos, Ypos, 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
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
lcnt = NsOCR.Blk_GetLineCnt(ImgObj); //get number of text lines for entire image
for (i = 0; i < lcnt; i++)
{
  wcnt = NsOCR.Blk_GetWordCnt(ImgObj, i); //get number of words of "i"-th text line
  for (j = 0; j < wcnt; j++)
  {
    ccnt = NsOCR.Blk_GetCharCnt(ImgObj, i, j); //get number of characters of "j"-th word of "i"-th text line
    for (k = 0; k < ccnt; k++)
    {   
      NsOCR.Blk_GetCharRect(ImgObj, i, j, k, out Xpos, out Ypos, out Width, out Height); //get character position and size
      res = NsOCR.Blk_GetCharQual(ImgObj, i, j, k, 0); //get character quality
      NsOCR.Blk_GetCharText(ImgObj, i, j, k, 0, out txt); //get character 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, lcnt, wcnt, ccnt, i, j, k, Xpos, Ypos, 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
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
lcnt = NsOCR.Blk_GetLineCnt(ImgObj) 'get number of text lines for entire image
For i = 0 To lcnt - 1
  wcnt = NsOCR.Blk_GetWordCnt(ImgObj, i) 'get number of words of "i"-th text line
  For j = 0 To wcnt - 1
    ccnt = NsOCR.Blk_GetCharCnt(ImgObj, i, j) 'get number of characters of "j"-th word of "i"-th text line
    For k = 0 To ccnt - 1
      NsOCR.Blk_GetCharRect(ImgObj, i, j, k, Xpos, Ypos, Width, Height) 'get character position and size
      res = NsOCR.Blk_GetCharQual(ImgObj, i, j, k, 0) 'get character quality
      NsOCR.Blk_GetCharText(ImgObj, i, j, k, 0, txt) 'get character text
    Next k
  Next j
Next i
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, cnt, i, j, k, lcnt, wcnt, ccnt; 
NSOCR.NSInt Xpos, Ypos, Width, Height;
Xpos = new NSOCR.NSInt(0);
Ypos = new NSOCR.NSInt(0);        
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
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.HBLK BlkObj = new NSOCR.HBLK(); //create temporary BLK object because we cannot pass ImgObj to Blk_GetBarcodeCnt directly
BlkObj.SetValue(ImgObj.GetValue()); //it is just a copy of ImgObj
lcnt = NSOCR.Engine.Blk_GetLineCnt(BlkObj); //get number of text lines for entire image
for (i = 0; i < lcnt; i++)
{
  wcnt = NSOCR.Engine.Blk_GetWordCnt(BlkObj, i); //get number of words of "i"-th text line
  for (j = 0; j < wcnt; j++)
  {
    ccnt = NSOCR.Engine.Blk_GetCharCnt(BlkObj, i, j); //get number of characters of "j"-th word of "i"-th text line
    for (k = 0; k < ccnt; k++)
    {   
      NSOCR.Engine.Blk_GetCharRect(BlkObj, i, j, k, Xpos, Ypos, Width, Height); //get character position and size
      res = NSOCR.Engine.Blk_GetCharQual(BlkObj, i, j, k, 0); //get character quality
      NSOCR.Engine.Blk_GetCharText(BlkObj, i, j, k, 0, txt); //get character text
      System.out.println(txt.toString());
    }
  }
}
NSOCR.Engine.Engine_Uninitialize(); //release all created objects and uninitialize OCR engine