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.
Take the following steps to start a new project:
Run Microsoft Visual Studio 2005, select the File -> New -> Project menu option, and do the following:
Select the Other Languages -> Visual C++ -> MFC.
Select MFC Application as the project type.
In the Project name text box, specify tutor.
In the Location text box, specify the path of the project.
Click the OK button, then click the Next button
In the next dialog box, do the following:
Select Dialog-based.
Ensure Use MFC in shared DLL is selected
Click the Finish button.
Add #include statements to your program so you can access the LEAD Twain DLL functions:
In the Project Workspace, click the FileView tab.
Double-click the tutor files folder to open it.
Double-click the Header Files folder to open it.
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 DLLs reside):
\#include "..\..\..\include\l_bitmap.h"
\#include "..\..\..\include\lttwn.h"
Add the LEAD Twain LIB file to your program. Open tutorDlg.cpp, and add the following line after the #include statements:
#pragma comment(lib, "..\\..\\..\\Lib"L_VER_DESIGNATOR"\\CDLL\\Win32\\Lttwn_u.lib")
Add a HTWAINSESSION member to the class and set it to NULL in the constructor.
Open tutorDlg.h file and add the following line after HICON m_hIcon;
HTWAINSESSION m_hSession;
Open tutorDlg.cpp file and add the following line inside CtutorDlg::CtutorDlg
m_hSession = NULL;
Go to the Solution Explorer and click the Resource Files, and double-click the .rc file. Double-click Dialog and double-click IDD_TUTOR_DIALOG to open the application's dialog box.
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 |
Add two Radio buttons in addition to the check box.
ID | Caption |
---|---|
IDC_NATIVE | Native Mode |
IDC_MEMORY | Memory Mode |
Add the following button:
ID | Caption |
---|---|
IDC_ACQUIRE | Fast Acquire |
Add the following two edit boxes and name them as follows:
ID |
---|
IDC_BUFFERSIZE |
IDC_FILENAME |
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;
To end the TWAIN session and free memory allocated by the TWAIN DLL, add the following code in the WM_DESTROY of the CTutorDlg. Take the following steps:
Double-click the dialog IDD_TUTOR_DIALOG, and then go to View -> Properties Window, and then press the Messages button.
Select WM_DESTROY, and then add OnDestroy from the list box.
if (m_hSession)
L_TwainEndSession (&m_hSession);
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:
Check Control variable check box.
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 |
Double-click the dialog IDD_TUTOR_DIALOG, and then double-click the Fast Acquire button to add the 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);
}
}
Compile and run your program to test it.
Enter a value (for example, 1000) for the buffer size and a file name (include the full path) for the scanned image.