Acquire Images Using Fast TWAIN Features

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 acquires images using the LEADTOOLS Fast TWAIN features:

 

1.

Start a new project as follows:

 

Run Microsoft Visual Studio 2005, select the File -> New -> Project menu option, and do the following:

 

a.

Select the “Other Languages” -> “Visual C++” -> “MFC”.

 

b.

Select MFC Application 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, then Click Next button

2.

In the next dialog box, do the following:

 

a.

Select Dialog based.

 

b.

Ensure “Use MFC in shared DLL” is selected

 

c.

Click the Finish button.

3.

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\lttwn.h"

4.

Add the LEAD Twain LIB file to your program:

 

Open “tutorDlg.cpp”, and add the following line after #include statements:

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

 

 

5.

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

 

a.

Open “tutorDlg.h” file and add the following line after “HICON m_hIcon;”

HTWAINSESSION m_hSession;

 

b.

Open “tutorDlg.cpp” file and add the following line inside “CtutorDlg::CtutorDlg”

 m_hSession = NULL;

 

6.

Go to the Solution Explorer and click the Resource Files, double click the .rc file. Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box.

7.

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

8.

Add two Radio buttons in addition to the above buttons.

 

ID

Caption

 

IDC_NATIVE

Native Mode

 

IDC_MEMORY

Memory Mode

9.

Add the following button:

 

ID

Caption

 

IDC_ACQUIRE

Fast Acquire

10.

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

 

ID

 

IDC_BUFFERSIZE

 

IDC_FILENAME

11.

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;

12.

To end the Twain session and free memory allocated by the Twain DLL, add the following code in the WM_DESTROY of the CTutorDlg. Do the following:

 

a.

Double click the dialog “IDD_TUTOR_DIALOG”, and then go to View -> Properties Window, and then press “Messages” button.

 

b.

Select WM_DESTROY, and then add OnDestroy from the list box.

 

if (m_hSession)
   L_TwainEndSession (&m_hSession);

 

13.

Add the following member variables for the following controls. To do so, go to Class View from Solution Explorer, select CtutorDlg, 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 Finish variable button.

 

 

ID

Member variable

Type

Category

 

IDC_USEBUFFERSIZE

m_bUseBufferSize

BOOL

Value

 

IDC_BUFFERSIZE

m_nBufferSize

int

Value

 

IDC_FILENAME

m_csFileName

CString

Value

 

14.

Double click the dialog “IDD_TUTOR_DIALOG”, and then double click on “Fast Acquire” button to add “OnBnClickedAcquire” handler function

 

Add the following code

 

void CTutorDlg:: OnBnClickedAcquire ()
{
   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 *)(LPCTSTR)m_csFileName,
                                    LTWAIN_SHOW_USER_INTERFACE | LTWAIN_USE_THREAD_MODE,
                                    uTransferMode,
                                    nFormat,
                                    1,
                                    TRUE,
                                    m_nBufferSize,
                                    !m_bUseBufferSize , NULL, NULL);

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

 

 

15.

Compile and run your program to test it.

16.

Enter a value (say 1000) for the buffer size and a file name (include full path) for the scanned image.