Set/Get a Twain Capability (Visual C++ 5.0)

Take the following steps to start a project and to add some code to use Get/Set capabuilty methods and then acquire your image using new set capability:

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 #import and #include statements to your program so you can access the LEAD COM constants and classes:

 

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):

#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
#import "c:\\winnt\\system32\\ltrio14n.dll" no_namespace, named_guids

8.

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;

9.

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.

10.

Add 4 buttons. 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

 

ID_SELECT_SOURCE

Select Source

 

ID_USE_CAPABILITY

Use Capability

 

ID_SET_BRIGHTNESS

Set Brightness

 

ID_ACQUIRE

Acquire

11.

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

   CoInitialize(NULL);

   HRESULT hr = CoCreateInstance (CLSID_LEADRasterTwain, NULL, CLSCTX_ALL, IID_ILEADRasterTwain, (void **)&m_pRasterTwain);
   if (SUCCEEDED(hr))
   {
      m_pRasterTwain->put_ManName (CString("LEAD Technologies, Inc.").AllocSysString());
      m_pRasterTwain->put_ProdFamily (CString("LEAD Twain COM Object").AllocSysString());
      m_pRasterTwain->put_Version (CString("Version 14.5").AllocSysString());
      m_pRasterTwain->put_AppName (CString("LEADTools Twain test sample").AllocSysString());

      short iRet = m_pRasterTwain->InitSession ((long)m_hWnd);
      if (iRet != 0)
         AfxMessageBox(TEXT("Error Initializing Twain"));

      m_pRasterTwain->EnableMethodErrors = FALSE;

      /* Unlock Document support.
      Note that this is a sample key, which will not work in your toolkit. */
      m_pRasterTwain->UnlockSupport (L_SUPPORT_DOCUMENT, "TestKey");
   }

12.

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 ();
   }

   CoUninitialize();

13.

Add code for the select source button (ID_SELECT_SOURCE). 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 ID_SELECT_SOURCE.

 

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 ();
}

14.

Add code for the select source button (ID_USE_CAPABILITY). 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 ID_USE_CAPABILITY.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

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

 

f.

Click the Edit Code button to start entering the code.

   void CTutorDlg::OnUseCapability()
{
   ICapability * pGetCap = NULL; 
   ICapabilityOneValue * pGetCapOV = NULL; 
   ILEADRasterVariant * CapVal = NULL; 

   CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL, 
                                   IID_ILEADRasterVariant, (void **)&CapVal); 

   HRESULT hr = ::CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pGetCap); 
   if (FAILED (hr) || pGetCap == NULL) 
      return; 

   pGetCap->
EnableMethodErrors = VARIANT_FALSE; 
   pGetCap->
ConType = L_TWON_DONTCARE16; 
   pGetCap->
Capability = L_ICAP_XFERMECH; 

   short iRet = m_pRasterTwain->GetCapability (pGetCap, L_LTWAIN_CAPABILITY_GETCURRENT); 
   if (iRet == 0) 
   {
      pGetCap->QueryInterface(IID_ICapabilityOneValue, (void **)&pGetCapOV); 
      if (!pGetCapOV) 
      {
         pGetCap->Release ();
         return; 
      }
      

      CapVal = pGetCapOV->
OneValCapValue

      if (CapVal->LongValue != L_TWSX_FILE) 
      {
         ICapability * pSetCap = NULL; 
         ICapabilityOneValue * pSetCapOV = NULL; 
         ILEADRasterVariant * SetCap = NULL; 

         CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL, 
                                   IID_ILEADRasterVariant, (void **)&SetCap);         

         ::CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pSetCap); 

         pSetCap->
EnableMethodErrors = VARIANT_FALSE; 
         pSetCap->
Capability = L_ICAP_XFERMECH; 
         pSetCap->
ConType = L_TWON_ONEVALUE; 

         pSetCap->QueryInterface(IID_ICapabilityOneValue, (void **)&pSetCapOV); 

         SetCap->Type  = VALUE_USHORT; 
         SetCap->LongValue = L_TWSX_FILE;         

         pSetCapOV->
OneValItemType = L_TWTY_UINT16; 
         pSetCapOV->
OneValCapValue = SetCap; 

         short iRet = m_pRasterTwain->
SetCapability (pSetCap, L_LTWAIN_CAPABILITY_SET); 

         pSetCapOV->Release ();
         pSetCap->Release ();
         SetCap->Release();
      }
      else
         MessageBox(
TEXT("The Transfer mode is File."), TEXT("Set Capability"), MB_OK); 

      pGetCapOV->Release ();
      pGetCap->Release ();
   }
   else
      MessageBox(
TEXT("Error occurred in the GetCapability method"), TEXT("Error!"), MB_OK); 

   CapVal->Release ();
}

15.

Add code for the select source button (ID_SET_BRIGHTNESS). 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 ID_SELECT_SOURCE.

 

d.

In the Messages list box, select BN_CLICKED.

 

e.

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

 

f.

Click the Edit Code button to start entering the code.

void CTutorDlg::OnSetBrightness() 
{
   ICapability * pCap = NULL; 
   ICapabilityOneValue * pCapOV = NULL; 
   ILEADRasterVariant * CapVal = NULL; 
   CoCreateInstance(CLSID_LEADRasterVariant, NULL, CLSCTX_ALL, 
                                   IID_ILEADRasterVariant, (void **)&CapVal); 
   

   HRESULT hr = ::CoCreateInstance(CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pCap); 
   if (FAILED (hr) || pCap == NULL) 
      return; 

   pCap->
EnableMethodErrors = VARIANT_FALSE; 
   pCap->
ConType = L_TWON_ONEVALUE; 
   pCap->
Capability = L_ICAP_BRIGHTNESS; 

   pCap->QueryInterface(IID_ICapabilityOneValue, (void **)&pCapOV); 

   CapVal->Type = VALUE_FLOAT; 
   CapVal->FloatValue = 50;   

   pCapOV->
OneValItemType = L_TWTY_FIX32; 
   pCapOV->
OneValCapValue = CapVal; 

   short iRet = m_pRasterTwain->
SetCapability (pCap, L_LTWAIN_CAPABILITY_SET); 

   pCapOV->Release ();
   pCap->Release ();
   CapVal->Release ();
}

16.

Add code for the select source button (ID_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 ID_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()
{
   m_pRasterTwain->FileTransferName = CString("c:\\twain.bmp").AllocSysString();
   short iRet = m_pRasterTwain->Acquire (L_LTWAIN_SHOW_USER_INTERFACE);
   if (iRet != 0)
      MessageBox (TEXT("Error Acquiring From Source"));
}

17.

Compile and test the program.