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 create and run a program that implements LEADTOOLS TWAIN features.
TwainTutor
.Add the following line immediately before the class CTwainTutorApp
declaration (keep in mind, you may have to change the path to where the header files reside):
#include "..\..\..\..\include\ClassLib\ltwrappr.h"
Click the Class View tab.
CTwainTutorApp(void)
constructor.Add the following lines after //TODO: add construction code here
:
LBase::LoadLibraries(LT_ALL_LEADLIB);
L_TCHAR * pszLicenseFile = L"Replace this with the path to the LEADTOOLS license file";
L_TCHAR * pszDeveloperKey = L"Replace this with your developer key";
LSettings::SetLicenseFile(pszLicenseFile, pszDeveloperKey);
Create a new file called Imports.cpp and place it beside your project files.
#include "StdAfx.h"
#if defined(WIN64)
#else
#pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLL\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64)
Click the Solution Explorer tab.
Add the following class declaration before CTwainTutorDlg class.
class LMyTwain : public LTwain
{
LEAD_DECLAREOBJECT(LMyTwain);
public:
LMyTwain();
virtual ~LMyTwain();
virtual L_INT BitmapCallBack(pBITMAPHANDLE pBitmap);
};
Click the Solution Explorer tab.
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;
}
Click the Class View tab
LMyTwain
, and for Variable Declaration put m_MyTwain
. Leave Access as Public and click OK.OnInitDialog()
function and add the following code after the 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);
OnDestroy
.Add the following code after the opening bracket {
:
m_MyTwain.EndSession();
LBase::UnloadLibraries(LT_ALL_LEADLIB);
Click the Solution Explorer tab.
TwainTutor.rc
file to open it, then double-click Dialog, and then double-click IDD_TWAINTUTOR_DIALOG
.Now drag and drop 2 buttons and 3 radio buttons, and change their properties as follows:
Control Type | ID | Caption |
---|---|---|
Button1 | IDC_SELECT_SRC | Select Source |
Button2 | IDC_ACQUIRE | Acquire |
Radio1 | IDC_NATIVE | Native |
Radio2 | IDC_MEMORY | Memory |
Radio3 | IDC_FILE | File |
From the View menu, select the Other Windows menu, then select the Resource View menu, then select Dialog, and select IDD_TWAINTUTOR_DIALOG.
Double-click the "Select Source" button, and add the following code:
m_MyTwain.SelectSource(NULL);
From the View menu, select the Other Windows menu, then select the Resource View menu, then select Dialog, and select IDD_TWAINTUTOR_DIALOG.
Double-click the Acquire button, and add the following code:
pBITMAPHANDLE pBitmap = NULL;
m_MyTwain.Acquire(pBitmap, sizeof(BITMAPHANDLE), LTWAIN_SHOW_USER_INTERFACE, NULL);
From the View menu, select the Other Windows menu, then select the Resource View menu, then select Dialog, and select IDD_TWAINTUTOR_DIALOG.
Double-click the Native radio button, and add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
L_INT nRet = m_MyTwain.GetProperties(&twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT);
if (nRet != SUCCESS)
return;
twProps.DataTransfer.nTransferMode = TWSX_NATIVE;
nRet = m_MyTwain.SetProperties(&twProps, LTWAIN_PROPERTIES_SET);
if (nRet != SUCCESS)
return;
From the View menu, select the Other Windows menu, then select the Resource View menu, then select Dialog, and select IDD_TWAINTUTOR_DIALOG.
Double-click the Memory radio button, and add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
L_INT nRet = m_MyTwain.GetProperties(&twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT);
if (nRet != SUCCESS)
return;
twProps.DataTransfer.nTransferMode = TWSX_MEMORY;
twProps.DataTransfer.nBufMemCompression = TWCP_NONE;
nRet = m_MyTwain.SetProperties(&twProps, LTWAIN_PROPERTIES_SET);
if (nRet != SUCCESS)
return;
From the View menu, select the Other Windows menu, then select the Resource View menu, then select Dialog, and select IDD_TWAINTUTOR_DIALOG.
Double-click the File radio button, and add the following code:
LTWAINPROPERTIES twProps;
memset (&twProps, 0, LTWAINPROPERTIESSIZE);
L_INT nRet = m_MyTwain.GetProperties(&twProps, sizeof(LTWAINPROPERTIES), LTWAIN_PROPERTIES_GETCURRENT);
if (nRet != SUCCESS)
return;
twProps.DataTransfer.nTransferMode = TWSX_FILE;
lstrcpy (twProps.DataTransfer.szFileName, TEXT("c:\\twain.bmp"));
nRet = m_MyTwain.SetProperties(&twProps, LTWAIN_PROPERTIES_SET);
if (nRet != SUCCESS)
return;
Compile and test the program.