Nicomsoft OCR: Developer's Guide


Ocr_ProcessPages


Syntax

C++:int Ocr_ProcessPages(HIMG ImgObj, HSVR SvrObj, int PageIndexStart, int PageIndexEnd, int OcrObjCnt, int Flags)
C#:int Ocr_ProcessPages(int ImgObj, int SvrObj, int PageIndexStart, int PageIndexEnd, int OcrObjCnt, int Flags)
Visual Basic:Function Ocr_ProcessPages(ByVal ImgObj As Integer, ByVal SvrObj As Integer, ByVal PageIndexStart As Integer, ByVal PageIndexEnd As Integer, ByVal OcrObjCnt As Integer, ByVal Flags As Integer) As Integer
Java:int Ocr_ProcessPages(HIMG ImgObj, HSVR SvrObj, int PageIndexStart, int PageIndexEnd, int OcrObjCnt, int Flags)
Delphi:function Ocr_ProcessPages(ImgObj:HIMG; SvrObj:HSVR; PageIndexStart:integer; PageIndexEnd:integer; OcrObjCnt:integer; Flags:integer):integer


Description

Performs OCR of one or more pages of the Image object, and saves the results to the Saver object.


Parameters

ImgObj [IN] – the Image object with the image loaded for OCR.
SvrObj [IN] – the Saver object that will store OCR results.
PageIndexStart [IN] – the first page index for OCR.
PageIndexEnd [IN] – the last page index for OCR; to process all pages, specify -1 .
OcrObjCnt [IN] – the number of OCR objects that will be used for OCR. To use a number equal to the number of logical CPU cores in the system, specify 0.
Flags [IN] – the operation mode; see the OCRFLAG_XXXXX constants for possible values. You can also specify the FMT_XXXXX constants in the following format: (FMT_XXXXX << 8).


Return value

Zero if success, otherwise an error code.


Remarks

     It is better to use the Img_OCR function to perform OCR for one page of the image. It will probably take less time, and you will have more control over the OCR process. However, Ocr_ProcessPages can work faster if several pages are processed. If OcrObjCnt is greater than 1, multiple pages will be processed at once, and the CPU will be used more fully in multi-core systems.
     The Ocr_ProcessPages function always performs all OCR steps (see the OCRSTEP_XXXXX constants). Do not use it if some actions (creating your own blocks, managing the found blocks, etc.) must be done between the OCR steps. If you need to do such inter-step actions, use the Img_OCR function for every page instead.


Example

The following code processes a single or multi-page image and saves OCR results as a searchable PDF file:

C++
int CfgObj, OcrObj, ImgObj, SvrObj, res;
Engine_InitializeAdvanced(&CfgObj, &OcrObj, &ImgObj); //initialize OCR engine, create objects and load configuration
res = Img_LoadFile(ImgObj, L"c:\\sample.tiff"); //load some image for OCR (single or multipage)
if (res > ERROR_FIRST) {}; //insert error handler here
Svr_Create(CfgObj, SVR_FORMAT_PDF, &SvrObj); //create Saver object, output format is PDF
Svr_NewDocument(SvrObj); //start new document
res = Ocr_ProcessPages(ImgObj, SvrObj, 0, -1, 0, OCRFLAG_NONE | (FMT_EXACTCOPY << 8)); //perform OCR
if (res > ERROR_FIRST) {}; //insert error handler here
res = Svr_SaveToFile(SvrObj, L"c:\\out.pdf"); //save OCRed image to PDF file
if (res > ERROR_FIRST) {}; //insert error handler here
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, SvrObj, res;
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.Svr_Create(CfgObj, TNSOCR.SVR_FORMAT_PDF, out SvrObj); //create Saver object, output format is PDF NsOCR.Svr_NewDocument(SvrObj); //start new document res = NsOCR.Ocr_ProcessPages(ImgObj, SvrObj, 0, -1, 0, TNSOCR.OCRFLAG_NONE | (TNSOCR.FMT_EXACTCOPY << 8)); //perform OCR if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here res = NsOCR.Svr_SaveToFile(SvrObj, "c:\\out.pdf"); //save OCRed image to PDF file if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here 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, SvrObj, res As Integer
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.Svr_Create(CfgObj, TNSOCR.SVR_FORMAT_PDF, SvrObj) 'create Saver object, output format is PDF
NsOCR.Svr_NewDocument(SvrObj) 'start new document
res = NsOCR.Ocr_ProcessPages(ImgObj, SvrObj, 0, -1, 0, TNSOCR.OCRFLAG_NONE Or (TNSOCR.FMT_EXACTCOPY * 256)) 'perform OCR
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
res = NsOCR.Svr_SaveToFile(SvrObj, "c:\out.pdf") 'save OCRed image to PDF file
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
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.HSVR SvrObj = new NSOCR.HSVR();
int res;
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.Svr_Create(CfgObj, NSOCR.Constant.SVR_FORMAT_PDF, SvrObj); //create Saver object, output format is PDF
NSOCR.Engine.Svr_NewDocument(SvrObj); //start new document
res = NSOCR.Engine.Ocr_ProcessPages(ImgObj, SvrObj, 0, -1, 0, NSOCR.Constant.OCRFLAG_NONE | (NSOCR.Constant.FMT_EXACTCOPY << 8)); //perform OCR
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
res = NSOCR.Engine.Svr_SaveToFile(SvrObj, "c:\\out.pdf"); //save OCRed image to PDF file
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
NSOCR.Engine.Engine_Uninitialize(); //release all created objects and uninitialize OCR engine