Set/Get a Twain Capability

Take the following steps to get or set a twain capability:

1.

Start Microsoft Visual Studio 2005

2.

Select the File->New menu option, click the "Project" menu.

3.

From "Project Types" select "Other Languages" to expand it, then select "Visual C++" to expand it, then select "MFC". From the right window select "MFC Application".

4.

In the Project Name dialog box, enter "TwainTutor3".

5.

In the Location dialog box, use the "Examples\ClassLibrary\MSVC" directory of your LEAD installation. For example, if you installed LEADTOOLS in "C:\Program Files\LEAD Technologies\LEADTOOLS 16\", enter "C:\Program Files\LEAD Technologies\LEADTOOLS 16\ Examples\ClassLibrary\MSVC", then Click OK. Then click "Next"

6.

Choose "Dialog based" and click "Finish".

7.

Click on the "Solution Explorer" tab, and then click on the "TwainTutor3" project to expand it. Click on the Header files, then Open "TwainTutor3.h".

8.

Add the following line immediately before the class CTwainTutor3App declaration, (keep in mind, you may have to change the path to where the header files reside):

#include "..\..\..\..\include\ClassLib\ltwrappr.h"

9.

Click on the "Class View" tab.

10.

Click to open the "TwainTutor3" classes branch.

11.

Click "CTwainTutor3App", and then double click the CTwainTutor3App(void) constructor.

12.

Add the following lines after //TODO: add construction code here:

 

   LBase::LoadLibraries(LT_ALL_LEADLIB); 
   LSettings::UnlockSupport(L_SUPPORT_DOCUMENT, L_KEY_DOCUMENT); 

13.

Create a new file called Imports.cpp in place it beside your project files.

 

a.

In the Project Workspace, click the Solution Explorer tab.

 

b.

Double-click the TwainTutor3 folder to open it.

 

c.

Right-click the Source files folder and select Add à New item.

 

d.

Expand Visual C++ tree, if it is not already expanded.

 

e.

Select Code from the sub tree.

 

f.

Select C++ File (.cpp) from the right window.

 

g.

In the name text box, specify Imports.cpp

 

h.

Click the Add button.

 

i.

Double-click the imports.cpp in the solution Explorer and add the following lines:

#include "StdAfx.h"

#if defined(WIN64) 
   
#else
   #pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLL\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64) 

14.

Click on the "Solution Explorer" tab.

15.

Double-click the "TwainTutor3" folder to open it.

16.

Double-click the "Header Files" folder to open it. Then double click "TwainTutor3Dlg.h" file to open it.

17.

Add the following class declaration before CTwainTutor3Dlg class.

 

class LMyTwain : public LTwain
{
   LEAD_DECLAREOBJECT(LMyTwain); 

public: 
   LMyTwain();
   virtual ~LMyTwain();
   virtual L_INT BitmapCallBack(pBITMAPHANDLE pBitmap); 
};

18.

Click on the "Solution Explorer" tab.

19.

Double-click the "TwainTutor3" folder to open it.

20.

Double-click the "Source Files" folder to open it. Then double click "TwainTutor3Dlg.cpp" file to open it.

21.

Add the following class:

 

LEAD_IMPLEMENTOBJECT(LMyTwain); 
LMyTwain::LMyTwain()
{
   EnableCallBack(TRUE); 
}

LMyTwain::~LMyTwain()
{
}

L_INT LMyTwain::BitmapCallBack(pBITMAPHANDLE pBitmap) 
{
   //Copy the acquired bitmap here
   return SUCCESS; 
}

22.

Click on the "Class View" tab.

23.

Right click "CTwainTutor3Dlg" and select Add à "Add Variable..."

24.

For Variable Type enter LMyTwain, and for Variable Declaration put m_MyTwain. Leave Access as "Public" and click OK.

25.

Right click "CTwainTutor3Dlg" and select Add à "Add Variable..."

26.

For Variable Type enter TW_CAPABILITY, and for Variable Declaration put m_twCap. Leave Access as "Public" and click OK.

27.

Right click "CTwainTutor3Dlg" and select Add à "Add Variable..."

28.

For Variable Type enter L_UINT, and for Variable Declaration put m_uItemValue. Leave Access as "Public" and click OK.

29.

Click to open the CTwainTutor3Dlg branch. Double click the OnInitDialog() function and add the following code after line:

 

// TODO: Add extra initialization here

 

   APPLICATIONDATA AppData; 
   memset(&AppData, 0, sizeof(APPLICATIONDATA)); 

   AppData.hWnd = m_hWnd; 
   AppData.uStructSize = sizeof(AppData); 
   lstrcpy(AppData.szManufacturerName, _T("LEAD Technologies, Inc.")); 
   lstrcpy(AppData.szAppProductFamily, _T("LEAD Test Applications"));
   lstrcpy(AppData.szVersionInfo, _T("Version 1.0"));
   lstrcpy(AppData.szAppName, _T("TWAIN Test Application"));
   m_MyTwain.InitSession(&AppData); 

30.

Right click the CTwainTutor3Dlg branch, and choose "Properties".

31.

From the "Properties" window toolbar, click on the Messages icon. Then click on the empty area beside the item "WM_DESTROY" and choose OnDestroy.

32.

Add the following code after the opening bracket {:

   m_MyTwain.EndSession();
   LBase::UnloadLibraries(LT_ALL_LEADLIB); 

33.

Click on the "Solution Explorer" tab.

34.

Double-click the "TwainTutor3" folder to open it.

35.

Double-click the "Resource Files" folder to open it. Then double click "TwainTutor3.rc" file to open it, then double click "Dialog", and then double click "IDD_TWAINTUTOR3_DIALOG"

36.

Now, drag and drop 5 buttons, and change their properties as follows:

 

 

ID

Caption

 

Button1

IDC_SELECT_SOURCE

Select Source

 

Button2

IDC_ACQUIRE

Acquire

 

Button3

IDC_GET_CAPABILITY

Get Capability

 

Button4

IDC_SET_CAPABILITY

Set Capability

 

Button5

IDC_SET_BRIGHTNESS

Set Brightness

37.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_TWAINTUTOR3_DIALOG"

38.

Double click on "Select Source" button, and add the following code:

   m_MyTwain.SelectSource(NULL); 

39.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_TWAINTUTOR3_DIALOG"

40.

Double click on "Acquire" button, and add the following code:

 

   BITMAPHANDLE Bitamp; 
   m_MyTwain.Acquire(&Bitamp, sizeof(BITMAPHANDLE), LTWAIN_SHOW_USER_INTERFACE, NULL); 

41.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_TWAINTUTOR3_DIALOG"

42.

Double click on "Get Capability" button, and add the following code:

 

   L_INT  nItemType; 

   m_twCap.Cap = ICAP_XFERMECH; 
   m_twCap.ConType = TWON_DONTCARE16; 
   m_twCap.hContainer = NULL; 
   m_MyTwain.GetCapability(&m_twCap, LTWAIN_CAPABILITY_GETCURRENT); 

   m_MyTwain.GetNumericContainerItemType(&m_twCap, &nItemType); 

   if (nItemType == TWTY_UINT16) 
      m_MyTwain.GetNumericContainerUINTValue(&m_twCap, 0, &m_uItemValue); 
   else
      AfxMessageBox(TEXT("Returned bad item type"));

43.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_TWAINTUTOR3_DIALOG"

44.

Double click on "Set Capability" button, and add the following code:

 

   if (m_uItemValue != TWSX_NATIVE) 
   {
      TW_CAPABILITY twSetCap; 

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

      m_MyTwain.CreateNumericContainerOneValue(&twSetCap, TWAINNUMERICTYPE_TW_UINT16, TWSX_NATIVE); 

      m_MyTwain.SetCapability(&twSetCap, LTWAIN_CAPABILITY_SET); 
      m_MyTwain.FreeContainer(&twSetCap); 
   }
   else
      AfxMessageBox(TEXT("The Transfer mode is Native"));

45.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_TWAINTUTOR3_DIALOG"

46.

Double click on "Set Brightness" button, and add the following code:

 

   TW_CAPABILITY twBrightnessCap; 
   TW_FIX32      twFix32; 
   pTW_ONEVALUE  ptwOneVal; 

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

   L_INT nRet = m_MyTwain.GetCapability(&twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT); 
   if (nRet == SUCCESS) 
   {
      ptwOneVal = (pTW_ONEVALUE)GlobalLock(m_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(m_twCap.hContainer); 

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

      m_MyTwain.SetCapability(&twBrightnessCap, LTWAIN_CAPABILITY_SET); 
   }

47.

Compile and test the program.