To create and run a program that demonstrates how to add pages to an OCR document using Ltdoc2.h. This program will allow page insertion and to save the inserted page:
1. |
Create a new directory in the \Examples\CDLL directory called OCR_Ltdoc2 |
2. |
Copy everything in the SimpleLoad directory (copy only folders content) into the OCR_Ltdoc2 directory. |
3. |
Compile the project as it is and run SimpleLoad.exe to familiarize yourself with the basic program. |
4. |
In the Imports.cpp, add the following lines: |
#if defined(WIN64)
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\x64\\Ltdoc2_x.lib")
#else
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Ltdoc2_u.lib")
#endif // #if defined(WIN64)
/* L_VER_DESIGNATOR is a macro that defines the version. For example for version 16 uses LTVXX_CONFIG. To set it in Visual Studio 2005, go to the main menu. Add in Project->Property->Configuration Properties->C/C++->Preprocessor: WIN32;_DEBUG;LTVXX_CONFIG */
5. |
Add the following line in StdAfx.h in the OCR_Ltdoc2 directory: |
/* LEADTOOLS main OCR document header file */
#include "..\..\..\include\ltdoc2.h"
#include "..\..\..\include\ltdlg.h"
6. |
Define the following global variables in Ezfunc.h in the OCR_Ltdoc2 directory: |
L_HDOC2 hDoc; /* OCR Document handle */
PAGEINFO2 PageInfo;
DOC2_LANGIDS LangId[1];
L_BOOL bPageAdded;
L_INT nSId;
#define IDM_INSERT_PAGE 200
#define IDM_SAVE_ADDED_PAGE 300
7. | Add the following statements in Ezfunc.cpp in the WinMain function as follows: | |
a. | Add the following before calling InitInstance() statement |
/* Note that this is a sample key, which will not work in your toolkit. You should replace the sample key */
L_SetLicenseFile (TEXT("MyLicenseFile", TEXT("TestKey"));
if (L_Doc2StartUp(&hDoc, NULL) != SUCCESS)
return 0;
L_Doc2CreateSettingsCollection(hDoc, -1, &nSId);
L_Doc2SetActiveSettingsCollection(hDoc, nSId);
|
b. |
Add the following after the GetMessage() loop before return statement |
L_Doc2DeleteSettingsCollection(hDoc, nSId);
L_Doc2ShutDown(&hDoc);
8. |
Edit EZFUNC.RC file in the OCR_Ltdoc2 directory and add the following lines: |
#include "EZFUNC.H"
MAIN_MENU MENU
BEGIN
MENUITEM "Insert Page" IDM_INSERT_PAGE
MENUITEM "Save Added Page" IDM_SAVE_ADDED_PAGE
END
9. |
In Ezfunc.cpp in InitApplication, change the line: |
wcWindowClass.lpszMenuName = NULL;
to:
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");
10. |
In Ezfunc.cpp in MainWndProc, change the line: |
Demos_CombinePath(szImagesDirectory, L_TEXT("Image1.cmp"), szFilename, _countof(szFilename));
To:
Demos_CombinePath(szImagesDirectory, L_TEXT("ocr1.tif"), szFilename, _countof(szFilename));
11. |
In Ezfunc.cpp in InitInstance, change the menu parameter (tagged as /* Use the window class menu */) in CreateWindow from NULL to LoadMenu(hInstance, MAKEINTRESOURCE("MAIN_MENU")), /*Main Menu*/ |
12. |
In Ezfunc.cpp under WM_CREATE, add the following code after "L_LoadBitmap() if(){}" : |
/*After code below*/
nRet = L_LoadBitmap (szFilename, &LeadBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGR, NULL, NULL);
if (nRet != SUCCESS)
{
wsprintf (achBuff, TEXT("Error %d loading %s"), nRet, (LPTSTR) szFilename);
MessageBox (NULL, achBuff, TEXT("Error"), MB_OK);
/* We have an error, so post WM_DESTROY */
PostMessage (hWnd, WM_DESTROY, 0, 0);
return (FALSE);
}
/*After code above*/
/*Add code below*/
bPageAdded = FALSE;
13. |
In Ezfunc.cpp after WM_PAINT and before WM_DESTROY, add a new switch statement called WM_COMMAND: |
/*After code below*/
case WM_PAINT:
/* Get the handle to the device context */
hdc = BeginPaint (hWnd, &ps);
if (LeadBitmap.Flags.Allocated) /* Do we have an image? */
{
if (hpalPaint) /* If we have a paint palette, select it */
{
hPalette = SelectPalette (hdc, hpalPaint, TRUE);
/* Uncomment this if you do not process WM_QUERYNEWPALETTE */
/* RealizePalette (hdc); */
}
/* Paint the image */
L_PaintDC (hdc,
&LeadBitmap,
&rLeadSource, /* Source rectangle */
NULL, /* Default source clip area */
&rLeadDest, /* Destination rectangle */
&ps.rcPaint, /* Dest clip set by WM_PAINT */
SRCCOPY); /* Normal Paint */
if (hpalPaint) /* Return old palette */
SelectPalette (hdc, hPalette, TRUE);
}
EndPaint (hWnd, &ps); /* Return DC */
return (0);
/*After code Above*/
/*Add code below*/
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_INSERT_PAGE:
{
nRet = L_Doc2AddPage(hDoc, &LeadBitmap, 1);
if (nRet != SUCCESS)
{
wsprintf (achBuff, TEXT("Error %d adding page at index 0"), nRet);
MessageBox (NULL, achBuff, TEXT("Error"), MB_OK);
/* We have an error, so post WM_DESTROY */
PostMessage (hWnd, WM_DESTROY, 0, 0);
return (FALSE);
}
bPageAdded = TRUE;
L_Doc2SetActivePage(hDoc, 0);
memset(&PageInfo, 0, sizeof(PAGEINFO2));
nRet = L_Doc2GetPageInfo (hDoc, 0, &PageInfo, sizeof(PAGEINFO2));
if (nRet == SUCCESS)
{
L_INT nPageCount = 0;
L_Doc2GetPageCount(hDoc, &nPageCount);
L_Doc2SetActivePage(hDoc, nPageCount - 1);
/* Force paint palette creation */
SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);
RedrawWindow(hWnd,0,0,256);
wsprintf (achBuff, TEXT("Page Width = %d\nPage Height = %d\nPage Bits Per Pixel = %d\nPages Inserted = %d"),
PageInfo.nWidth,
PageInfo.nHeight,
PageInfo.nBitsPerPixel,
nPageCount);
MessageBox (NULL, achBuff, TEXT("Pages Info"), MB_OK);
}
LangId[0] = DOC2_LANG_ID_ENGLISH;
nRet = L_Doc2SelectLanguages(hDoc, LangId, 1);
if (nRet != SUCCESS)
{
wsprintf (achBuff, TEXT("Error %d Setting English as the default language."), nRet);
MessageBox (NULL, achBuff, TEXT("Error"), MB_OK);
RemoveDocumentPages();
/* We have an error, so post WM_DESTROY */
PostMessage (hWnd, WM_DESTROY, 0, 0);
return (FALSE);
}
}
break;
case IDM_SAVE_ADDED_PAGE:
{
L_INT nPageCount=0;
L_INT nRet = L_Doc2GetPageCount(hDoc, &nPageCount);
if (nRet != SUCCESS)
{
MessageBox(NULL, TEXT("Cannot get pages count"), TEXT("Error!"), MB_OK);
return 0;
}
if (nPageCount <=0)
{
MessageBox(NULL, TEXT("No added pages, please add pages first"), TEXT("Error!"), MB_OK);
return 0;
}
BITMAPHANDLE ExportBmp;
L_InitBitmap(&ExportBmp, sizeof(BITMAPHANDLE), 0, 0, 1);
nRet = L_Doc2ExportPage(hDoc, &ExportBmp, sizeof(BITMAPHANDLE), 0);
if (nRet != SUCCESS)
{
MessageBox(NULL, TEXT("Cannot export page to save"), TEXT("Error!"), MB_OK);
return 0;
}
nRet = L_SaveBitmap(TEXT("c:\\test.tif"),
&ExportBmp,
FILE_TIF,
ExportBmp.BitsPerPixel,
0,
NULL);
L_FreeBitmap(&ExportBmp);
}
break;
}
return 0;
/*Add code above*/
14. |
Add the following function in EZFUNC.CPP in OCR_Ltdoc2 directory, add it at the end after "void CalcDisplay()" function: |
void RemoveDocumentPages()
{
L_INT nPageCount = 0;
L_INT i, nRet;
L_TCHAR szBuffer[1024];
nRet = L_Doc2GetPageCount(hDoc, &nPageCount);
if (nRet == SUCCESS)
{
for (i=0; i<nPageCount; i++)
{
L_Doc2CleanupPages(hDoc, TRUE);
nRet = L_Doc2RemovePage(hDoc, i);
if (nRet == SUCCESS)
{
wsprintf(szBuffer, TEXT("The page # %d was removed successfully"), i);
MessageBox(NULL, szBuffer, TEXT("Notice!"), MB_OK);
}
}
}
}
15. |
Also, add the following statement before the MainWndProc function |
void RemoveDocumentPages();
16. |
On the Build menu, select Build SimpleLoad.exe. |
17. |
On the Build menu, select Execute SimpleLoad.exe. (NOTE: Before building project, clean solution and project files. In Build, select Clean Solution and Clean SimpleLoad). |
18. |
Save this project to use it for testing other code samples. |