Acquire Images from a TWAIN Source

Note: The purpose of this TWAIN tutorial is to provide you a quick and easy way to generate a TWAIN program. For more in depth TWAIN programming, refer to the TWAIN demo.

To create and run a program that implements LEADTOOLS TWAIN features:

 

1.

Create a new directory in the \Examples\API directory called TwainTutorial

2.

Copy everything in the SimpleLoad directory into the TwainTutorial 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)

   …

   …

#else

   …

   …

   #pragma comment(lib, "..\\..\\..\\Lib\\API\\Win32\\Lttwn_u.lib")

#endif // #if defined(WIN64)

 

5.

Add the following line in StdAfx.h in the TwainTutorial directory:

 

#include "..\..\..\include\lttwn.h"

 

6.

Define the following global variables in Ezfunc.h in the TwainTutorial directory:

HTWAINSESSION hSession = NULL;

L_INT nTransferMode;

 

Also, add the following defines:

 

#define IDM_SELECT_SOURCE        200

#define IDM_ACQUIRE              201

#define IDM_NATIVE_TRANSFER       202

#define IDM_MEMORY_TRANSFER       203

#define IDM_FILE_TRANSFER          204

 

7.

Edit EZFUNC.RC file in the TwainTutorial directory and add the following lines:

 

#include "EZFUNC.H"

 

MAIN_MENU   MENU

BEGIN

   MENUITEM "Select Source"   IDM_SELECT_SOURCE

   MENUITEM "Acquire"         IDM_ACQUIRE

   MENUITEM "Native Transfer" IDM_NATIVE_TRANSFER   

   MENUITEM "Memory Transfer" IDM_MEMORY_TRANSFER   

   MENUITEM "File Transfer"   IDM_FILE_TRANSFER   

END

 

8.

In the InitApplication function change the line:

 

wcWindowClass.lpszMenuName = NULL;  /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

 

9.

In the MainWndProc function and before the handling of the WM_PALETTECHANGED message add the following code:

 

   case WM_COMMAND: 
      {
         switch (LOWORD(wParam)) 
         {
         case IDM_SELECT_SOURCE: 
            {
            }
            break; 
         case IDM_ACQUIRE: 
            {
            }
            break; 
         case IDM_NATIVE_TRANSFER: 
            {
            }
            break; 
         case IDM_MEMORY_TRANSFER: 
            {
            }
            break; 
         case IDM_FILE_TRANSFER: 
            {
            }
            break; 
         }
      }
      break;

10.

In the WM_CREATE message handler add the following code at the end of it (after SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);):

 

APPLICATIONDATA AppData; 
AppData.uStructSize = sizeof(APPLICATIONDATA);
AppData.hWnd = hWnd; 
lstrcpy (AppData.szManufacturerName, TEXT("LEAD Technologies, Inc.")); 
lstrcpy (AppData.szAppProductFamily, TEXT("LEAD Test Applications"));
lstrcpy (AppData.szVersionInfo, TEXT("Version 1.0"));
lstrcpy (AppData.szAppName, TEXT("TWAIN Test Application"));
nRet = L_TwainInitSession(&hSession, &AppData); 
if (nRet != SUCCESS) 
   return FALSE; 
nRet = L_TwainStartCapsNeg(hSession); 

nTransferMode = TWSX_NATIVE;

11.

In the WM_DESTROY message handler add the following code at the end of it (before the PostQuitMessage (0);):

 

L_TwainEndCapsNeg(hSession); 
L_TwainEndSession
(&hSession); 

12.

In the IDM_SELECT_SOURCE menu handler add the following code:

 

case IDM_SELECT_SOURCE:
   {
      nRet = L_TwainSelectSource(hSession, NULL); 
   }
   break; 

13.

In the IDM_ACQUIRE menu handler add the following code:

 

case IDM_ACQUIRE: 
   {
      L_TwainEndCapsNeg(hSession); 
      nRet = L_TwainAcquire(hSession, NULL, sizeof(BITMAPHANDLE), TwainBitmapCB, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL); 
      if (nRet != SUCCESS) 
         return nRet; 
      L_TwainStartCapsNeg(hSession); 
      InvalidateRect (hWnd, NULL, FALSE); 
   }
   break; 

14.

In IDM_NATIVE_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_NATIVE; 
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet; 

nTransferMode = TWSX_NATIVE;

 

15.

In the IDM_MEMORY_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_MEMORY; 
twProps.DataTransfer.nBufMemCompression = TWCP_NONE; 
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet; 

nTransferMode = TWSX_MEMORY;

16.

In the IDM_FILE_TRANSFER menu handler add the following code:

 

LTWAINPROPERTIES twProps; 
memset (&twProps, 0, LTWAINPROPERTIESSIZE); 
nRet = L_TwainGetProperties(hSession, &twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT); 
if (nRet != SUCCESS) 
   return nRet; 
twProps.DataTransfer.nTransferMode = TWSX_FILE; 
lstrcpy (twProps.DataTransfer.szFileName, TEXT("c:\\twain.bmp"));
nRet = L_TwainSetProperties(hSession, &twProps, LTWAIN_PROPERTIES_SET, NULL, NULL); 
if (nRet != SUCCESS) 
   return nRet;

nTransferMode = TWSX_FILE;

 

17.

In Ezfunc.cpp file, add the following function before MainWndProc function:

 

L_INT EXT_CALLBACK TwainBitmapCB(HTWAINSESSION hSession, pBITMAPHANDLE pBitmap, L_VOID * pUserData)

{

   UNREFERENCED_PARAMETER(hSession);

   UNREFERENCED_PARAMETER(pUserData);

 

   if (LeadBitmap.Flags.Allocated)

      L_FreeBitmap(&LeadBitmap);

 

   if (nTransferMode != TWSX_FILE)

      L_CopyBitmap(&LeadBitmap, pBitmap, sizeof(BITMAPHANDLE));

 

   L_FreeBitmap(pBitmap);

   return SUCCESS;

}

 

18.

Compile and test the program.