Get a Fast Twain Scan from a Scanner (C++ Builder)

Take the following steps to start a project and to add some code to find the fastest configuration for your scanner using the LEADTOOLS Fast TWAIN feature:

1.

Start C++ Builder 4.0.

2.

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

3.

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

4.

Declare the following variables in the public section of "Unit1.h":

LEADRasterTwain * pRasterTwain;
int nRet;

5.

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

void __fastcall TForm1::FormCreate(TObject *Sender)
{
   CoInitialize ( NULL ) ;
   CoCreateInstance(CLSID_LEADRasterTwain, NULL, CLSCTX_ALL, IID_ILEADRasterTwain, (void**)&pRasterTwain);
   pRasterTwain->InitSession ( (long)Handle ) ;
   pRasterTwain->EnableMethodErrors = false;

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

6.

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

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

7.

At the top of Form1, add 3 button controls and name them as follows:

 

Name

Caption

 

btnSelectSource

Select Source

 

btnFindFastConfig

Find Fast Config.

 

btnAcquireFastConfig

Acquire Fast Config.

8.

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

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

9.

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

void __fastcall TForm1::btnFindFastConfigClick(TObject *Sender)
{
   pRasterTwain->UserFastConfigsCount = 0;
   pRasterTwain->EnableFastConfigEvent = false;
   nRet= pRasterTwain->FindFastConfig ( AnsiToOLESTR("c:\\temp"), L_LTWAIN_SHOW_USER_INTERFACE | L_LTWAIN_USE_THREAD_MODE, 8, 5);

   if ( nRet == 0 )
      ShowMessage ("Find Fast Configuration process completed" );
   else
      ShowMessage ("An error occurred during the Finding Fast Configuration process." ) ;
}

10.

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

void __fastcall TForm1::btnAcquireFastConfigClick(TObject *Sender)
{
   pRasterTwain->FastTransferMode = pRasterTwain->BestFastConfig->TransferMode;
   pRasterTwain->FastFormat = pRasterTwain->BestFastConfig->FileFormat;
   pRasterTwain->FastBitsPerPixel = pRasterTwain->BestFastConfig->BitsPerPixel;
   pRasterTwain->FastBufferSize = pRasterTwain->BestFastConfig->BufferSize;

   pRasterTwain->FastUsePreferredBufferSize = true;
   pRasterTwain->EnableAcquireMultiEvent = false;

   nRet= pRasterTwain->AcquireMulti ( AnsiToOLESTR("c:\\temp\\test"), L_LTWAIN_SHOW_USER_INTERFACE, true);

   if ( nRet == 0 )
      ShowMessage ( "Image acquisition using the Fast TWAIN Configuration is completed." );
   else
      ShowMessage ( "An error occurred while acquiring an image using the Fast TWAIN Configuration." ) ;
}

11.

In the Unit1.h file include these files.

#include "LTRASTERTWAINLib_TLB.h"
#include "LTRASTERLib_TLB.h"

12.

Save your project and run your program to test it.