Acquire Images Using Fast TWAIN Features

Take the following steps to start a project and to add some code that acquires the images using the LEADTOOLS Fast TWAIN features:

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 "FastTwainTutor".

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 "FastTwainTutor" project to expand it. Click on the Header files, then Open "FastTwainTutor.h".

8.

Add the following line immediately before the class CFastTwainTutorApp 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 "FastTwainTutor" classes branch.

11.

Click "CFastTwainTutorApp", and then double click the CFastTwainTutorApp(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 FastTwainTutor 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 "FastTwainTutor" folder to open it.

16.

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

17.

Add the following class declaration before CFastTwainTutorDlg 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 "FastTwainTutor" folder to open it.

20.

Double-click the "Source Files" folder to open it. Then double click "FastTwainTutorDlg.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 "CFastTwainTutorDlg" 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.

Click to open the CFastTwainTutorDlg 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); 

26.

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

27.

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

28.

Add the following code after the opening bracket {:

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

29.

Click on the "Solution Explorer" tab.

30.

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

31.

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

32.

Now, drag and drop 2 radio buttons, 1 check box, 1 button, 2 text boxes, and change their properties as follows:

 

 

ID

Caption

 

Button1

IDC_ACQUIRE

Fast Acquire

 

Radio1

IDC_NATIVE

Native

 

Radio2

IDC_MEMORY

Memory

 

Check1

IDC_USE_BUFFER_SIZE

Use Buffer Size

 

 

IDC_EDIT1

IDC_BUFFERSIZE

 

 

IDC_EDIT2

IDC_FILENAME

 

33.

Add the following member variables for the following controls. To do so, go to Class View from Solution Explorer, select CFastTwainTutorDlg, and then go to Project -> Add Variable menu, then do the following:

 

a.

Check "Control variable" check box.

 

b.

Select each of the following controls and add the corresponding member variable by pressing the Add variable button.

 

 

ID

Member variable

Type

 

IDC_USE_BUFFER_SIZE

m_bUseBufferSize

BOOL

 

IDC_BUFFERSIZE

m_nBufferSize

int

 

IDC_FILENAME

m_csFileName

CString

 

34.

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

35.

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

 

   UpdateData();

   L_INT  nFormat; 
   L_UINT uTransferMode; 

   BOOL bMemory = IsDlgButtonChecked(IDC_MEMORY); 
   if (bMemory) 
   {
      uTransferMode = LTWAIN_BUFFER_MODE; 
      nFormat = FILE_CCITT_GROUP4; 
   }
   else
   {
      uTransferMode = LTWAIN_NATIVE_MODE; 
      nFormat = FILE_TIF; 
   }

   L_INT nRet = m_MyTwain.FastAcquire((L_TCHAR *)(LPCTSTR)m_csFileName, 
                                        LTWAIN_SHOW_USER_INTERFACE | LTWAIN_USE_THREAD_MODE, 
                                        uTransferMode, 
                                        nFormat, 
                                        1, 
                                        TRUE, 
                                        m_nBufferSize, 
                                        !m_bUseBufferSize); 

   if (nRet == 1) 
      AfxMessageBox(TEXT("Fast Scanning process done successfully...")); 
   else
   {
      CString csError; 
      csError.Format(TEXT("FastAcquire failed, Error = %d\n"), nRet); 
      AfxMessageBox(csError); 
   }

36.

Compile and test the program.