Acquiring an Image (C++ Builder)

Take the following steps to start a project and to add some code that acquires an image from a TWAIN source:

1.

Start C++ Builder 4.0.

2.

If you didn’t install LEAD RasterView Control, install it as follows:

 

On the Component pull-down menu, use the Import ActiveX Control… and select the LEAD RasterView Control. Press install, and then compile and install the package dclusr40.bpk.

3.

Select the LEAD RasterView control and add it to Form1. Size and position the control as you want it to appear at run time.

4.

On the Project pull-down menu, use the Import Type library… and select the LEAD RasterTwain Object Library. Press OK.

6.

If you didn’t import LEAD Raster Variant before, import it as follows:

7.

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Variant Object Library. Press install, and then compile and install the package dclusr40.bpk.

5.

If you didn’t install LEADEventSink Control, install it as follows:

 

On the Component pull-down menu, use install Component …, and browse to LEADXX\Include\LEvntSnk.pas. Press OK, then compile and install the package dclusr40.bpk.

6.

Open the LEvntSnk.hpp file and delete those lines below from the TLEADAbstractEventSink class and save the file.

private:
   void *__IDispatch; /* IDispatch */

public:
   operator IDispatch*(void) { return (IDispatch*)&__IDispatch; }
   operator IUnknown*(void) { return (IUnknown*)&__IDispatch; }

7.

From the ActiveX controls, add the LEADEventSink control to Form1.

8.

Add the following code to the private section of Unit1.h:

   LEADTwainCapability * pTwainCap; 
   LEADRasterTwain * pRasterTwain; 
   int nXferMode; 
   int nRet; 
   int TwainSetCapability ( short iCapability, long lCapVal, short iItemType );

9.

Handle the Form1 OnCreate event, and code the FormCreate procedure as follows:

void __fastcall TForm1::FormCreate(TObject *Sender) 
{
   CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pTwainCap); 
   CoCreateInstance(CLSID_LEADRasterTwain, NULL, CLSCTX_ALL, IID_ILEADRasterTwain, (void**)&pRasterTwain); 

   pRasterTwain->InitSession ( (long)Handle ) ; 
   LEADEventSink1->Connect  ( pRasterTwain, DIID__LEADRasterTwainEvents ); 
}

10.

Handle the Form1 OnClose event, and code the FormClose procedure as follows:

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) 
{
pRasterTwain->EndSession ( ) ; 
if ( pTwainCap ) 
         pTwainCap->Release ( ) ; 
   
if ( pRasterTwain ) 
         pRasterTwain->Release ( ) ; 
}

11.

At the top of Form1, add six Buttons and three Option Buttons and name them as follows:

 

Name

Caption

 

btnOK

OK

 

btnCancel

Cancel

 

btnAcquire

Acquire

 

btnSelectSource

Select Source

 

btnNative

Native

 

btnMemBuf

Memory Buffered

 

btnFile

File

 

btnSaveTemplate

Save Template File

 

btnLoadTemplate

Load Template File

12.

Handle the btnOK OnClick event, and code the btnOKClick procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

void __fastcall TForm1::btnOKClick(TObject *Sender)
{
   Close ( ) ;
}

13.

Handle the btnCancel OnClick event, and code the btnCancelClick procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code:

void __fastcall TForm1::btnCancelClick(TObject *Sender)
{
   Close ( ) ; 
}

14.

Handle the btnAcquire OnClick event, and code the btnAcquireClick procedure as follows:

void __fastcall TForm1::btnAcquireClick(TObject *Sender) 
{
   pRasterTwain->Acquire ( L_LTWAIN_SHOW_USER_INTERFACE ) ; 
}

15.

Handle the btnSelectSource OnClick event, and code the btnSelectSourceClick procedure as follows:

void __fastcall TForm1::btnSelectSourceClick(TObject *Sender) 
{
   pRasterTwain->SelectSource ( ) ; 
}

16.

Handle the btnSaveTemplate OnClick event, and code the btnSaveTemplateClick procedure as follows:

void __fastcall TForm1::btnSaveTemplateClick(TObject *Sender) 
{
   nRet= pRasterTwain->SaveTemplate ( AnsiToOLESTR ( "c:\\test.ltt" ) ) ; 
}

17.

Handle btnLoadTemplate OnClick event, and code the btnLoadTemplateClick procedure as follows:

void __fastcall TForm1::btnLoadTemplateClick(TObject *Sender) 
{
   nRet= pRasterTwain->LoadTemplate ( AnsiToOLESTR ( "c:\\test.ltt" ) ) ; 
}

18.

Handle the btnNative option button’s OnClick event, and code the btnNativeClick procedure as Follows:

void __fastcall TForm1::btnNativeClick(TObject *Sender) 
{
 ILEADRasterVariant* pCapVal= NULL;

 

 CoCreateInstance(CLSID_LEADRasterVariant,

    NULL,

    CLSCTX_ALL,

    IID_ILEADRasterVariant,

    (void**)&pCapVal );

 

 nXferMode= L_TWSX_NATIVE;

 

 pCapVal->Type= VALUE_USHORT;

 pCapVal->LongValue= nXferMode;

   TwainSetCapability ( L_ICAP_XFERMECH, pCapVal, L_TWTY_UINT16 );

 if ( pCapVal )

  pCapVal->Release ( );
}

19.

Handle the btnMemBuf Option button’s OnClick event, and code the btnMemBufClick procedure as follows:

void __fastcall TForm1::btnMemBufClick(TObject *Sender) 
{
 ILEADRasterVariant* pCapVal= NULL;

 

 CoCreateInstance(CLSID_LEADRasterVariant,

    NULL,

    CLSCTX_ALL,

    IID_ILEADRasterVariant,

    (void**)&pCapVal );

 

   nXferMode= L_TWSX_MEMORY;

 

 pCapVal->Type= VALUE_USHORT;

 pCapVal->LongValue= nXferMode;

 

   TwainSetCapability ( L_ICAP_XFERMECH, pCapVal, L_TWTY_UINT16 );

 if ( pCapVal )

  pCapVal->Release ( );
}

20.

Handle the btnFile Option button’s OnClick event, and code the btnFileClick procedure as follows:

void __fastcall TForm1::btnFileClick(TObject *Sender) 
{
   ILEADRasterVariant* pCapVal= NULL;

 

 CoCreateInstance(CLSID_LEADRasterVariant,

    NULL,

    CLSCTX_ALL,

    IID_ILEADRasterVariant,

    (void**)&pCapVal );

 

   nXferMode= L_TWSX_FILE;

 pCapVal->Type= VALUE_USHORT;

 pCapVal->LongValue= nXferMode;

 

   TwainSetCapability ( L_ICAP_XFERMECH, pCapVal, L_TWTY_UINT16 );

   pRasterTwain->FileTransferName = AnsiToOLESTR ( "c:\\twain.bmp" );

 if ( pCapVal )

  pCapVal->Release ( );
}

21.

Handle the LEADEventSink1 OnInvoke event, and code the LEADEventSink1Invoke procedure as follows:

void __fastcall TForm1::LEADEventSink1Invoke(TObject *Sender, int DispID, 
      const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params, 
      Pointer varResult, Pointer ExcepInfo, Pointer ArgErr) 
{
   switch (DispID) 
   {
      case LEADRASTER_TWAIN_EVENTS_ACQUIREPAGEEVENT: 
      {
         int pBitmap= OleVariant ( Params.rgvarg[0] ) ; 
         ShowMessage ( "Acquisition of Image Done" ) ; 
         LEADRasterView1->Raster->InsertBitmapListItem ( -1, pBitmap ) ; 
      }
      break; 
   }
}

22.

Add the TwainSetCapability function body to the Unit1 file.

int TForm1::TwainSetCapability ( short iCapability, ILEADRasterVariant* pCapVal, short iItemType ) 
{
   int iRet; 

   pTwainCap->EnableMethodErrors = False; 
   pTwainCap->CapInfo->Capability = iCapability; 
   pTwainCap->CapInfo->ConType = L_TWON_ONEVALUE; 
   pTwainCap->CapOneValue->OneValItemType = iItemType; 
   pTwainCap->CapOneValue->set_OneValCapValue ( pCapVal ) ; 

   iRet= pRasterTwain->SetCapability2 ( pTwainCap, L_LTWAIN_CAPABILITY_SET); 
   return iRet; 
}

23.

In Unit1.h file include these files.

#include "LTRASTERTWAINLib_TLB.h"

#include "LTRASTERVARIANTLib_TLB.h"

24.

Run your program to test it.