Using the 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 a new project as follows:

 

Run Microsoft Visual C++ 5.0, select the File -> New menu option, and do the following:

 

a.

Click the Projects tab.

 

b.

Select MFC AppWizard (exe) as the project type.

 

c.

In the Project name text box, specify tutor.

 

d.

In the Location text box, specify the path of the project.

 

e

Click the OK button.

2.

In the Step 1 dialog box, do the following:

 

a.

Select Dialog based.

 

b.

Click the Next button.

3.

In the Step 2 of 4 dialog box, do the following:

 

a.

Ensure that About Box is selected.

 

b.

Ensure that 3D Controls is selected.

 

c.

Select ActiveX Controls.

 

d.

Click the Next button.

4.

In the Step 3 of 4 dialog box, do the following:

 

a.

For comments, ensure that Yes, Please is selected.

 

b.

For how to use the MFC library, select Use MFC in a Shared DLL.

 

c.

Click the Next button.

5.

In the Step 4 of 4 dialog box, just click Finish.

6.

Read New Project Information, and click OK. (The AppWizard creates the project files and opens the project.)

7.

Add #include statements to your program so you can access the LEAD Twain DLL functions:

 

a.

In the Project Workspace, click the FileView tab.

 

b.

Double-click the tutor files folder to open it.

 

c.

Double-click the Header Files folder to open it.

 

d.

Double-click the StdAfx.h file to edit it.

 

Add the following lines to the end of the file (keep in mind, you may have to change the path to where the dll's reside):

 

#include "..\..\..\include\l_bitmap.h"
#include "..\..\..\include\lttw2.h"

8.

Add the LEAD Twain LIB file to your program:

 

a.

In the Project menu, select Add to Project-> Files menu.

 

b.

Go to the path that contains the lttw2_n.lib.

 

c.

Select the lttw2_n.lib and press OK.

9.

Add a HTWAINSESSION member to the class and set it to NULL in the constructor.

 

a.

In the Project Workspace, click the Class View tab.

 

b.

Select the CTutorDlg class.

 

c.

Right-click and choose "Add Member Variable".

 

d.

For Variable Type enter HTWAINSESSION.

 

e.

For Variable Declaration enter m_hSession.

 

f.

Leave Access as Public.

 

g.

Click the OK button.

 

h.

In the Class View tab, double-click the CTutorDlg constructor function.

 

i.

Add the following line to the end of the constructor:

 

m_hSession = NULL;

10.

Go to the Project WorkSpace and click the Resource View tab. Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box.

11.

Add 1 check box. To do this, select the check box control on the Controls toolbar. Then, click and drag to position the button on the dialog box. Then double-click the check box to change its properties. Set the properties as follows:

 

ID

Caption

 

IDC_USEBUFFERSIZE

Use Buffer Size

12.

Add two Radio buttons in addition to the above buttons.

 

ID

Caption

 

IDC_NATIVE

Native Mode

 

IDC_MEMORY

Memory Mode

13.

Add the following button:

 

ID

Caption

 

IDC_ACQUIRE

Fast Acquire

14.

Add the following two text boxes and name them as follows:

 

ID

 

IDC_BUFFERSIZE

 

IDC_FILENAME

15.

Add the following code to the end of OnInitDialog before the return statement:

 

APPLICATIONDATA AppData;
AppData.uStructSize = sizeof(APPLICATIONDATA);
AppData.hWnd = GetSafeHwnd(); // Window handle of an application, may not be NULL
lstrcpy (AppData.szManufacturerName, TEXT("LEAD Technologies, Inc.")); // Application manufacturer name
lstrcpy (AppData.szAppProductFamily, TEXT("LEAD Test Applications")); // Application product family
lstrcpy (AppData.szVersionInfo, TEXT("Version 14.0")); // Application version info
lstrcpy (AppData.szAppName, TEXT("TWAIN Utility Application")); // Application Name

// initialize the twain session to use other lead twain functions
L_INT nRet = L_TwainInitSession (&m_hSession, &AppData);
if (nRet != SUCCESS)
   return FALSE;

16.

To end the Twain session and free memory allocated by the Twain DLL, add the following code in the destructor of the CTutorDlg. To do so, press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

In the Class name combo box, select CTutorDlg.

 

b.

In the Object IDs list box, select CTutorDlg.

 

c.

In the Messages list box, select WM_DESTROY.

 

d.

Click the Add Function button. Choose OK for the default function name (OnDestroy).

 

e.

Click the Edit Code button to enter the following code before CDialog::OnDestroy();

 

if (m_hSession)
   L_TwainEndSession (&m_hSession);

17.

Add the following member variables for the following controls. To do so, press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Select Member variables tab.

 

b.

In the Class name combo box, select CTutorDlg.

 

c.

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

 

 

ID

Member variable

Type

 

IDC_USEBUFFERSIZE

m_bUseBufferSize

BOOL

 

IDC_BUFFERSIZE

m_nBufferSize

Int

 

IDC_FILENAME

m_csFileName

Cstring

 

 

d.

Press Ok.

18.

Add code for the select source button (IDC_ACQUIRE). To do so, press Ctrl-W to go to the MFC Class Wizard; then do the following:

 

a.

Click the Message Maps tab.

 

b.

In the Class Name combo box, select CTutorDlg.

 

c.

In the Object IDs list box, select IDC_ACQUIRE.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

Click the Add Function button. Choose OK for the default function name (OnAcquire).

 

f.

Click the Edit Code button to start entering the code.

 

void CTutorDlg::OnAcquire()
{
   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 = L_TwainAcquireMulti (m_hSession,
                                    (L_TCHAR L_FAR *)(LPCTSTR)m_csFileName,
                                    LTWAIN_SHOW_USER_INTERFACE | LTWAIN_USE_THREAD_MODE,
                                    uTransferMode,
                                    nFormat,
                                    1,
                                    TRUE,
                                    m_nBufferSize,
                                    !m_bUseBufferSize);

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

19.

Compile and run your program to test it.