Acquiring an Image (C++ 5.0 and later)
The following steps are to help you create an application that will use the ILEADRasterTwain Object Library.
1. |
Start with the project that you created in Loading and Displaying an Image. |
|
2. |
Add the #import and #include statements to your program so you can access LEAD RasterTwain Object Library: |
|
|
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. |
|
e. |
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): |
#import "c:\\winnt\\system32\\ltrvr14n.dll" no_namespace, named_guids
#import "c:\\winnt\\system32\\ltr14n.dll" no_namespace, named_guids
#import "c:\\winnt\\system32\\ltrtn14n.dll" no_namespace, named_guids
3. |
Add an ILEADRasterTwain pointer 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 ILEADRasterTwain. |
|
e. |
For Variable Declaration enter *m_pRasterTwain. |
|
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_pRasterTwain = NULL;
4. |
Add an ILEADRaster pointer 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 ILEADRaster. |
|
e. |
For Variable Declaration enter *m_pRaster. |
|
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_pRaster = NULL;
5. |
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. |
|
6. |
Add four new buttons at the top of the form. To do this for each button, select the button control on the Controls toolbar. Then, click and drag to position the button on the dialog box. Then double-click the button to change its properties. Set the properties as follows: |
|
|
ID |
Caption |
|
IDC_SELECTSOURCE |
Select Source |
|
IDC_ACQUIRE |
Acquire |
|
IDC_SAVE_TEMPLATE |
Save Template File |
|
IDC_LOAD_TEMPLATE |
Load Template File |
7. |
Add three Radio buttons in addition to the above buttons. |
|
|
ID |
Caption |
|
IDC_NATIVE |
Native Transfer |
|
IDC_MEMORY |
Memory Transfer |
|
IDC_FILE |
File Transfer |
8. |
Add the following code to the end of OnInitDialog before the return statement: |
if (m_pRasterTwain == NULL)
{
CoCreateInstance(
CLSID_LEADRasterTwain,
NULL,
CLSCTX_ALL,
IID_ILEADRasterTwain,
(void**)&m_pRasterTwain);
}
if (m_pRasterTwain == NULL)
return FALSE;
m_pRasterTwain->InitSession ((long)GetSafeHwnd());
m_pRasterTwain->put_EnableMethodErrors(TRUE);
9. |
To end the Twain session and free memory allocated by the COM object, 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_pRasterTwain)
{
m_pRasterTwain->EndSession ();
m_pRaster->Release ();
}
10. |
Add code for the select source button (IDC_SELECTSOURCE). 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_SELECTSOURCE. |
|
d. |
In the Messages list box, select BN_CLICKED. |
|
e. |
Click the Add Function button. Choose OK for the default function name (OnSelectSource). |
|
f. |
Click the Edit Code button to start entering the code. |
void CTutorDlg::OnSelectSource()
{
if (m_pRasterTwain)
m_pRasterTwain->SelectSource ();
}
11. |
Add code for the acquire 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 enter the following code in the OnAcquire function. |
if (m_pRasterTwain)
m_pRasterTwain->Acquire (L_LTWAIN_SHOW_USER_INTERFACE);
12. |
Add code for the save template button (IDC_SAVE_TEMPLATE). 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_SAVE_TEMPLATE. |
|
d. |
In the Messages list box, select BN_CLICKED. |
|
e. |
Click the Add function button. Choose OK for the default function name (OnSaveTemplate). |
|
f. |
Click the Edit Code button to enter the following code in the OnSaveTemplate function. |
if (m_pRasterTwain)
m_pRasterTwain->SaveTemplate ("c:\\test.ltt");
13. |
Add code for the load template button (IDC_LOAD_TEMPLATE). 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_LOAD_TEMPLATE. |
|
d. |
In the Messages list box, select BN_CLICKED. |
|
e. |
Click the Add function button. Choose OK for the default function name (OnLoadTemplate). |
|
f. |
Click the Edit Code button to enter the following code in the OnLoadTemplate function. |
if (m_pRasterTwain)
m_pRasterTwain->LoadTemplate("c:\\test.ltt");
14. |
Add a variable in the CTutorDlg class for the Transfer Mode. To do so, do the following: |
|
|
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 unsigned short. |
|
e. |
For Variable Declaration enter m_XferMode. |
|
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 construtor |
m_XferMode = L_TWSX_NATIVE;
15. |
Add code for the transfer mode buttons (IDC_NATIVE, IDC_MEMORY, IDC_FILE). 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_NATIVE. |
|
d. |
In the Messages list box, select BN_CLICKED. |
|
e. |
Click the Add Function button. Choose OK for the default function name (OnNative). |
|
f. |
Click the Edit Code button to start entering the code. |
|
g. |
Repeat the above steps for IDC_MEMORY and IDC_FILE buttons. |
16. |
Add the following code in the (OnNative) function. |
ILEADRasterVariant * CapVal = NULL;
CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL,
IID_ILEADRasterVariant, (void **)& CapVal);
m_XferMode = L_TWSX_NATIVE;
CapVal->Type = VALUE_USHORT;
CapVal->LongValue = m_XferMode;
TwainSetCapability (L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16);
CapVal->Release();
17. |
Add the following code in the (OnMemory) function. |
ILEADRasterVariant * CapVal = NULL;
CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL,
IID_ILEADRasterVariant, (void **)& CapVal);
m_XferMode = L_TWSX_MEMORY;
CapVal->Type = VALUE_USHORT;
CapVal->LongValue = m_XferMode;
TwainSetCapability (L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16);
CapVal->Release();
18. |
Add the following code in the (OnFile) function. |
ILEADRasterVariant * CapVal = NULL;
CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL,
IID_ILEADRasterVariant, (void **)& CapVal);
m_XferMode = L_TWSX_FILE;
CapVal->Type = VALUE_USHORT;
CapVal->LongValue = m_XferMode;
TwainSetCapability (L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16);
m_pRasterTwain->put_FileTransferName (CString("c:\\twain.bmp").AllocSysString());
CapVal->Release();
19. |
To define the TwainSetCapability function used above do the following: |
|
|
a. |
In the Project Workspace, click the Class View tab. |
|
b. |
Select the CTutorDlg class |
|
c. |
Right-click and choose "Add Member Function" |
|
d. |
For Function Type enter int. |
|
e. |
For Function Declaration enter TwainSetCapability(UINT uCapability, ILEADRasterVariant * CapVal, UINT uItemType). |
|
f. |
Leave Access as Public |
|
g. |
Click the OK button. |
|
h. |
Add the following code to the CTutorDlg::TwainSetCapability function: |
short iRet;
HRESULT hr;
ICapability * pCapability = NULL;
ICapabilityOneValue * pCapOV = NULL;
hr = ::CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pCapability);
if (FAILED (hr) || pCapability == NULL)
return 1;
pCapability->QueryInterface (IID_ICapabilityOneValue, (void **)&pCapOV);
if (!pCapOV)
{
pCapability->Release ();
return 1;
}
pCapability->put_Capability (uCapability);
pCapability->put_ConType (L_TWON_ONEVALUE);
pCapOV->put_OneValItemType (uItemType);
pCapOV->put_OneValCapValue (CapVal);
iRet = m_pRasterTwain->SetCapability (pCapability, L_LTWAIN_CAPABILITY_SET);
pCapOV->Release ();
pCapability->Release ();
return iRet;
20. |
To add the code for the twain events, edit the TUTORDLG.H file and change the definition of CTutorDlg : CDialog by inserting the following lines after DECLARE_MESSAGE_MAP() |
public:
CRasterTwainSink *m_pRasterTwainSink;
IConnectionPoint *m_pCP;
DWORD m_dwCookie;
21. |
Edit the TUTORDLG.CPP file and add the following code to the end of the OnInitDialog function: |
m_pRasterTwainSink = new CRasterTwainSink;
m_pRasterTwainSink->m_pDlg = this;
//Attach the twain object to the raster view control
IDispatch *pDispatch=NULL;
(m_LEADRasterView1.GetControlUnknown())->QueryInterface(IID_IDispatch, (void**)&pDispatch);
if(pDispatch)
pDispatch->Release();
//Establish a connection between source and sink.
LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE);
AfxConnectionAdvise(m_pRasterTwain, DIID__LEADRasterTwainEvents, pUnkSink, FALSE, &m_dwCookie);
22. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
|
a. |
Click the Add Class button. |
|
b. |
Click New.... |
|
c. |
Type CRasterTwainSink for the name of the class |
|
d. |
Select CCmdTarget for the base class of the new class. |
|
e. |
Under Automation, click the Automation Radio button. |
|
f. |
Click OK to create the class. |
|
g. |
In the RasterTwainSink.h file, move the destructor so that it is public: |
// Implementation
virtual ~CRasterTwainSink();
protected:
|
h. |
In the RasterTwainSink.h file, add the following to the CRasterTwainSink class in the //Attributes public section: |
// Attributes
public:
CTutorDlg *m_pDlg;
|
i. |
In the RasterTwainSink.cpp file, add the following to the top of the file (after the #include "RasterTwainSink.h") |
#include "tutorDlg.h"
#include "leadraster.h"
|
j. |
In the RasterTwainSink.h file, add the following line |
class CTutorDlg;
before class CRasterTwainSink : public CCmdTarget
23. |
Add #include statements so you can access the new class: |
|
|
a. |
In the Project Workspace, click the File View 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. |
|
e. |
Add the following lines to the end of the file: |
#include <AFXCTL.H>
#include "RasterTwainSink.h"
24. |
Edit the header for the Sink class: |
|
|
a. |
In the Project Workspace, click the File View 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 RasterTwainSink.h file to edit it. |
|
e. |
Add the following just before //}}AFX_MSG: |
afx_msg void OnAcquire(long pBitmap);
afx_msg void OnSaveTemplate(IUnknown * pCapability);
afx_msg void OnLoadTemplate(IUnknown * pCapability);
25. |
Edit the source for the Sink class: |
|
|
a. |
In the Project Workspace, click the File View tab. |
|
b. |
Double-click the tutor files folder to open it. |
|
c. |
Double-click the Source Files folder. |
|
d. |
Double-click the RasterTwainSink.cpp file to edit it. |
|
e. |
Add the following code in between the BEGIN_DISPATCH_MAP(CRasterTwainSink, CCmdTarget) and END_DISPATCH_MAP(): |
DISP_FUNCTION_ID(CRasterTwainSink, "AcquirePageEvent", 1, OnAcquire, VT_EMPTY,VTS_I4)
DISP_FUNCTION_ID(CRasterTwainSink, "SaveTemplateEvent", 2, OnSaveTemplate, VT_EMPTY, VTS_UNKNOWN)
DISP_FUNCTION_ID(CRasterTwainSink, "LoadTemplateEvent", 3, OnLoadTemplate, VT_EMPTY, VTS_UNKNOWN)
|
f. |
Inside the BEGIN_INTERFACE_MAP section, change the INTERFACE_PART to the following: |
INTERFACE_PART(CRasterTwainSink, DIID__LEADRasterTwainEvents, Dispatch)
|
g. |
Add the following to the end of the file: |
26. |
In the OnDestroy function add the following code to the beginning of it: |
LPUNKNOWN pUnkSink = m_pRasterTwainSink->GetIDispatch(FALSE);
AfxConnectionUnadvise(m_pRasterTwain, DIID__LEADRasterTwainEvents, pUnkSink, FALSE, m_dwCookie);
delete m_pRasterTwainSink;
27. |
Add code in the RasterTwainSink.cpp: |
void CRasterTwainSink::OnAcquire (long pBitmap)
{
// Do Code To Notify Getting a Page.
m_pDlg->m_LEADRasterView1.GetRaster().InsertBitmapListItem (-1, pBitmap);
}
void CRasterTwainSink::OnSaveTemplate(IUnknown * pCapability)
{
//Do Code To Modify Capability.
}
void CRasterTwainSink::OnLoadTemplate(IUnknown * pCapability)
{
//Do Code To Modify Capability.
}
28. |
Compile and run your program to test it. |