Working with Pages Using LEADTOOLS OCR Module - LEAD Engine

To create and run a program that demonstrates how to add pages to an OCR document using Ltocr.h. This program will allow page insertion and to save the inserted page:

  1. Create a new directory in the <INSTALLDIR>\Examples\OCR\CDLL directory called OCR\_Ltocr.
  2. Copy everything in the \Examples\FileFormats\CDLL\SimpleLoad directory (copy only folders content) into the OCR_Ltocr 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\\CDLL\\x64\\Ltocr_x.lib")   
    #else   
       #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltocr_u.lib")   
    #endif // #if defined(WIN64) 

  5. Add the following line in StdAfx.h in the OCR_Ltocr directory:

    /* LEADTOOLS main OCR document header file */   
    #include "..\..\..\include\ltocr.h"   
    #include "..\..\..\include\ltdlg.h" 

  6. Define the following global variables in Ezfunc.h in the OCR_Ltocr directory:

    #define IDM_INSERT_PAGE 200   
    #define IDM_SAVE_ADDED_PAGE 300   
    L_OcrEngine hEngine;                  /* OCR Engine handle */   
    L_OcrPage hPage;                      /* OCR Page handle */   
    L_OcrDocumentManager hDocumentManager; /* OCR Document Manager handle */   
    L_OcrDocument hDocument;              /* OCR Document handle */ 

  7. Add the following statements in Ezfunc.cpp in the WinMain function as follows:

    L_SetLicenseFile(TEXT("MyLicenseFile"), TEXT("DeveloperKey"));   
    L_INT nRet = L_OcrEngineManager_CreateEngine(L_OcrEngineType_LEAD, &hEngine);   
    if(nRet != SUCCESS)   
       return 0;   
    nRet = L_OcrEngine_Startup(hEngine, NULL, NULL);   
    if(nRet != SUCCESS)   
       return 0;   
    nRet = L_OcrEngine_GetDocumentManager(hEngine, &hDocumentManager);   
    if(nRet != SUCCESS)   
       return 0;   
    nRet = L_OcrDocumentManager_CreateDocument(hDocumentManager, &hDocument, L_OcrCreateDocumentOptions_AutoDeleteFile, NULL);   
    if(nRet != SUCCESS)   
       return 0; 

  8. Add the following after the GetMessage() loop before return statement:

    L_OcrDocument_Destroy(hDocument);   
    L_OcrEngine_Destroy(hEngine); 

  9. Edit EZFUNC.RC file in the OCR_Ltocr 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 

  10. In Ezfunc.cpp in InitApplication, change the line:

    wcWindowClass.lpszMenuName = NULL; 

    to:

    wcWindowClass.lpszMenuName = TEXT("MAIN_MENU"); 

  11. 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)); 

  12. 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*/ 

  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: 
       { 
          // Create the OCR page from the loaded bitmap 
          nRet = L_OcrPage_FromBitmap(hEngine, &hPage, &LeadBitmap, L_OcrBitmapSharingMode_AutoFree, NULL, NULL); 
          if (nRet != SUCCESS) 
          { 
             MessageBox (NULL, TEXT("Error creating OCR page"), TEXT("Error"), MB_OK); 
             /* We have an error, so post WM_DESTROY */ 
             PostMessage (hWnd, WM_DESTROY, 0, 0); 
             return (FALSE); 
          } 
          // Add the created OCR page into the file-based OCR document 
          nRet = L_OcrDocument_AddPage(hDocument, hPage); 
          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); 
          } 
          /* 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\n"), 
          LeadBitmap.Width, 
          LeadBitmap.Height, 
          LeadBitmap.BitsPerPixel); 
          MessageBox (NULL, achBuff, TEXT("Page Info"), MB_OK); 
       } 
       break; 
       case IDM_SAVE_ADDED_PAGE: 
       { 
          BITMAPHANDLE ExportBmp; 
          L_InitBitmap(&ExportBmp, sizeof(BITMAPHANDLE), 0, 0, 1); 
          nRet = L_OcrPage_GetBitmap(hPage, L_OcrPageBitmapType_Original, &ExportBmp, sizeof(BITMAPHANDLE)); 
          if (nRet != SUCCESS) 
          { 
             MessageBox(NULL, TEXT("Cannot retrieve page bitmap to save"), TEXT("Error!"), MB_OK); 
             return 0; 
          } 
          L_TCHAR szFileName[MAX_PATH] = {0}; 
          lstrcpy(szFileName, TEXT("c:\\test.tif")); 
          nRet = L_SaveBitmap(szFileName, ExportBmp, FILE_TIF, ExportBmp.BitsPerPixel, 0, NULL); 
          if(nRet != SUCCESS) 
          { 
             wsprintf (achBuff, TEXT("Error %d saving OCR page bitmap to %s"), nRet, szFileName); 
             MessageBox(NULL, achBuff, TEXT("Error!"), MB_OK); 
             return 0; 
          } 
          else 
          { 
             wsprintf (achBuff, TEXT("OCR page bitmap was saved to %s"), szFileName); 
             MessageBox(NULL, achBuff, TEXT("Error!"), MB_OK); 
             return 0; 
          } 
       } 
       break; 
    } 
    return 0; 
    /*Add code above*/ 

  14. Delete or comment out the following lines from WM_DESTROY message since L_OcrPage_Destroy call will free the bitmap since the OCR page owns the bitmap handle and you don't own it any more and this is because we passed L_OcrBitmapSharingMode_AutoFree for the sharingMode parameter of the L_OcrPage_FromBitmap function.

    /* Free the image, if there is one */   
    if (LeadBitmap.Flags.Allocated)   
       L_FreeBitmap (&LeadBitmap); 

  15. On the Build menu, select Build SimpleLoad.exe.

  16. 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.
  17. Save this project to use it for testing other code samples.
Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS OCR Module - LEAD Engine C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.