Nicomsoft OCR: Developer's Guide


Scan_ScanToImg


Syntax

C++:int Scan_ScanToImg(HSCAN ScanObj, HIMG ImgObj, int ScannerIndex, int Flags)
C#:int Scan_ScanToImg(int ScanObj, int ImgObj, int ScannerIndex, int Flags)
Visual Basic:Function Scan_ScanToImg(ByVal ScanObj As Integer, ByVal ImgObj As Integer, ByVal ScannerIndex As Integer, ByVal Flags As Integer) As Integer
Java:int Scan_ScanToImg(HSCAN ScanObj, HIMG ImgObj, int ScannerIndex, int Flags)
Delphi:function Scan_ScanToImg(ScanObj:HSCAN; ImgObj:HIMG; ScannerIndex:integer; Flags:integer):integer


Description

Scans the document to the IMG object. The Scan_Enumerate function must be called before this function to enumerate available scanners.


Parameters

ScanObj [IN] – the Scan object.
ImgObj [IN] – the IMG object.
ScannerIndex [IN] – the scanner index.
Flags [IN] – the flags. See the SCAN_XXXXX constants for possible values.


Return value

Zero if success, otherwise an error code.


Remarks

None.


Example

The following code enumerates available scanners, scans an image from the first scanner, and recognizes it. Note that if the scanned image contains several pages, this code will recognize the first page only. See the Svr_Create function for a sample code that processes all pages in a multi-page image.

C++
int CfgObj, OcrObj, ImgObj, ScanObj, res, n;
Engine_InitializeAdvanced(&CfgObj, &OcrObj, &ImgObj); //initialize OCR engine, create objects and load configuration
Scan_Create(CfgObj, &ScanObj); //create Scan object
n = Scan_Enumerate(ScanObj, NULL, 0, 0) + 1; //get length in unicode characters plus terminating NULL character
if (n > ERROR_FIRST) {}; //insert error handler here
wchar_t* txt = (wchar_t*) malloc(2 * n); //allocate memory for text
n = Scan_Enumerate(ScanObj, txt, n, 0); //enumerate scanners
//use "txt" variable now, it contains the list of available TWAIN devices separated by comma
free(txt); //free memory
res = Scan_ScanToImg(ScanObj, ImgObj, 0, SCAN_NOUI); //first scanner: scan image to Image object
//or scan to a file: res = Scan_ScanToFile(ScanObj, L"c:\\scan.tiff", 0, SCAN_NOUI);
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
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, ScanObj, 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
NsOCR.Scan_Create(CfgObj, out ScanObj); //create Scan object
res = NsOCR.Scan_Enumerate(ScanObj, out txt, 0); //enumerate scanners
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
//use "txt" variable now, it contains the list of available TWAIN devices separated by comma
res = NsOCR.Scan_ScanToImg(ScanObj, ImgObj, 0, TNSOCR.SCAN_NOUI); //first scanner: scan image to Image object
//or scan to a file: res = NsOCR.Scan_ScanToFile(ScanObj, "c:\\scan.tiff", 0, TNSOCR.SCAN_NOUI);
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
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, ScanObj, 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
NsOCR.Scan_Create(CfgObj, ScanObj) 'create Scan object
res = NsOCR.Scan_Enumerate(ScanObj, txt, 0) 'enumerate scanners
If res > TNSOCR.ERROR_FIRST Then 'insert error handler here
End If
'use "txt" variable now, it contains the list of available TWAIN devices separated by comma
res = NsOCR.Scan_ScanToImg(ScanObj, ImgObj, 0, TNSOCR.SCAN_NOUI) 'first scanner: scan image to Image object
'or scan to a file: res = NsOCR.Scan_ScanToFile(ScanObj, "c:\scan.tiff", 0, TNSOCR.SCAN_NOUI)
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
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.HSCAN ScanObj = new NSOCR.HSCAN();
int res;
StringBuffer txt = new StringBuffer();
NSOCR.Engine.Engine_InitializeAdvanced(CfgObj, OcrObj, ImgObj); //initialize OCR engine, create objects and load configuration
NSOCR.Engine.Scan_Create(CfgObj, ScanObj); //create Scan object
res = NSOCR.Engine.Scan_Enumerate(ScanObj, txt, 0); //enumerate scanners
System.out.println(txt.toString());
if (res > NSOCR.Error.ERROR_FIRST) {}; //insert error handler here
res = NSOCR.Engine.Scan_ScanToImg(ScanObj, ImgObj, 0, NSOCR.Constant.SCAN_NOUI); //first scanner: scan image to Image object
//or scan to a file: res = NSOCR.Engine.Scan_ScanToFile(ScanObj, "c:\\scan.tiff", 0, NSOCR.Constant.SCAN_NOUI);
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.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