Set/Get a Twain Capability

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 Get or Set a TWAIN capability using LEADTOOLS:

 

1.

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

2.

Copy everything in the SimpleLoad directory into the TwainCap 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 TwainCap directory:

 

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

 

6.

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

 

HTWAINSESSION hSession;
TW_CAPABILITY twCap;
L_UINT uItemValue;

 

Also, add the following defines:

 

#define IDM_SELECT_SOURCE  200

#define IDM_GET_CAPABILITY 201

#define IDM_SET_CAPABILITY 202

#define IDM_SET_BRIGHTNESS 203

#define IDM_ACQUIRE        204

 

 

7.

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

 

#include "EZFUNC.H"

 

MAIN_MENU   MENU

BEGIN

   MENUITEM "Select Source"   IDM_SELECT_SOURCE

   MENUITEM "Get Capability"  IDM_GET_CAPABILITY 

   MENUITEM "Set Capability"  IDM_SET_CAPABILITY

   MENUITEM "Set Brightness"  IDM_SET_BRIGHTNESS

   MENUITEM "Acquire"         IDM_ACQUIRE

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, 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_GET_CAPABILITY: 
            {
            }
            break; 
         case IDM_SET_CAPABILITY: 
            {
            }
            break; 
         case IDM_SET_BRIGHTNESS:
            {
            }
            break; 
         case IDM_ACQUIRE: 
            {
            }
            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);

if(nRet != SUCCESS)

  return FALSE;

 

/* Unlock Document support.
Note that this is a sample key, which will not work in your toolkit. */
L_UnlockSupport
(L_SUPPORT_DOCUMENT, TEXT("TestKey"));

 

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_GET_CAPABILITY menu handler add the following code:

case IDM_GET_CAPABILITY:
   {
      L_INT  nItemType;

      twCap.Cap = ICAP_XFERMECH;
      twCap.ConType = TWON_DONTCARE16;
      twCap.hContainer = NULL;
      L_TwainGetCapability(hSession, &twCap, LTWAIN_CAPABILITY_GETCURRENT);

      L_TwainGetNumericContainerItemType(&twCap, &nItemType);

      if (nItemType == TWTY_UINT16)
         L_TwainGetNumericContainerUINTValue(&twCap, 0, &uItemValue);
      else
         MessageBox(hWnd, TEXT("Returned bad item type"), TEXT("Get Item Type"), MB_OK);
   }
   break;

 

14.

In the IDM_SET_CAPABILITY menu handle add the following code:

case IDM_SET_CAPABILITY: 
   {
      if (uItemValue != TWSX_NATIVE)
      {
         TW_CAPABILITY twSetCap;

         twSetCap.Cap = ICAP_XFERMECH; 
         twSetCap.ConType = TWON_ONEVALUE;

         L_TwainCreateNumericContainerOneValue(&twSetCap, TWAINNUMERICTYPE_TW_UINT16, TWSX_NATIVE);

         L_TwainSetCapability(hSession, &twSetCap, LTWAIN_CAPABILITY_SET);
         L_TwainFreeContainer(&twSetCap);
      }
      else
         MessageBox(hWnd, TEXT("The Transfer mode is Native"), TEXT("Set Capability"), MB_OK);
   }
   break;

 

15.

In the IDM_SET_BRIGHTNESS menu handle add the following code:

case IDM_SET_BRIGHTNESS:
   {
      TW_CAPABILITY twBrightnessCap;
      TW_FIX32      twFix32;
      pTW_ONEVALUE  ptwOneVal;

      twBrightnessCap.Cap = ICAP_BRIGHTNESS;
      twBrightnessCap.ConType = TWON_ONEVALUE;

      nRet = L_TwainGetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT);
      if (nRet == SUCCESS)

      {
      ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);

      memcpy(&twFix32, (void *)&ptwOneVal->Item, sizeof(TW_FIX32));

      GlobalUnlock(twBrightnessCap.hContainer);
      GlobalFree(twBrightnessCap.hContainer);
      twBrightnessCap.hContainer = NULL;

      if (twFix32.Whole != 128)
      {
         twFix32.Whole = 128;
         twFix32.Frac = 0;
      }

      twBrightnessCap.Cap = ICAP_BRIGHTNESS;
      twBrightnessCap.ConType = TWON_ONEVALUE;
      twBrightnessCap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE) + sizeof(TW_FIX32));

      ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);

      ptwOneVal->ItemType = TWTY_FIX32;
      memcpy((void *)&ptwOneVal->Item, &twFix32, sizeof(TW_FIX32));
      GlobalUnlock(twBrightnessCap.hContainer);

      L_TwainSetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_SET);

}

   }
   break;

 

16.

In the IDM_ACQUIRE menu handler add the following code:

case IDM_ACQUIRE: 
   {
      L_TwainEndCapsNeg(hSession);
      if (LeadBitmap.Flags.Allocated)
      {
         L_FreeBitmap(&LeadBitmap);
         L_InitBitmap(&LeadBitmap, sizeof(BITMAPHANDLE), 0, 0, 0);

         L_TwainAcquire(hSession, &LeadBitmap, sizeof(BITMAPHANDLE), NULL, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL);
      }
   }
   break;

 

17.

Compile and test the program.