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:

Take the following steps to create and run a program that implement LEADTOOLS TWAIN features. Remember, the purpose of the 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.

1.

Start C++ Builder.

2.

On the C++ Builder toolbar, click the LEADTOOLS tab. If you have used a LEAD VCL control before, the icon appears on the toolbar. Otherwise, refer to Installing VCL before continuing with this tutorial.

3.

Select the LEAD Main control on the VCL toolbar. Size and position the control, as you want it to appear at run time.

4.

Select the LEAD Twain control on the LEADTOOLS toolbar. Place the control anywhere on the form.

5.

At the top of your form, add 7 Buttons and name them as follows:

 

Name

Caption

 

btnOK

OK

 

btnCancel

Cancel

 

btnAcquire

Acquire

 

btnSelectSource

Select Source

 

btnNative

Native

 

btnMemBuf

Memory Buffered

 

tnFile

File

6.

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

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 LEADTwain1->InitSession ( (L_HANDLE)Handle ) ; 
}

7.

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

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

8.

Handle the btnOK OnClick event, and code the btnOKClick procedure as follows.

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

9.

Handle the btnCancel OnClick event, and code the btnCancelClick procedure as follows.

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

10.

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

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

11.

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

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

12.

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

void __fastcall TForm1::btnNativeClick(TObject *Sender) 
{
 L_INT nRet; 
 LTWAINPROPERTIES twProps; 

 FillMemory ( &twProps, sizeof(LTWAINPROPERTIES), 0 ); 
 nRet= LEADTwain1->GetProperties ( &twProps, LTWAIN_PROPERTIES_GETCURRENT ); 
 if ( nRet != SUCCESS ) 
  return; 
 twProps.DataTransfer.nTransferMode= TWSX_NATIVE; 

 LEADTwain1->SetProperties ( &twProps, LTWAIN_PROPERTIES_SET ); 
}

13.

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

void __fastcall TForm1::btnMemBufClick(TObject *Sender) 
{
 L_INT nRet; 
 LTWAINPROPERTIES twProps; 

 FillMemory ( &twProps, sizeof(LTWAINPROPERTIES), 0 ); 
 nRet= LEADTwain1->GetProperties ( &twProps, LTWAIN_PROPERTIES_GETCURRENT ); 
 if ( nRet != SUCCESS ) 
  return; 
 twProps.DataTransfer.nTransferMode= TWSX_MEMORY; 
 twProps.DataTransfer.nBufMemCompression= TWCP_NONE; 
 LEADTwain1->SetProperties ( &twProps, LTWAIN_PROPERTIES_SET );

}

14.

Handle the btnFile OnClick event, and code the btnFileClick procedure as follows:

void __fastcall TForm1::btnFileClick(TObject *Sender) 
{
 L_INT nRet; 
 LTWAINPROPERTIES twProps; 

 FillMemory ( &twProps, sizeof(LTWAINPROPERTIES), 0 ); 
 nRet= LEADTwain1->GetProperties ( &twProps, LTWAIN_PROPERTIES_GETCURRENT ); 
 if ( nRet != SUCCESS ) 
  return; 
 twProps.DataTransfer.nTransferMode= TWSX_NATIVE; 
 StrCopy ( twProps.DataTransfer.szFileName, "c:\twain.bmp" ); 
 LEADTwain1->SetProperties ( &twProps, LTWAIN_PROPERTIES_SET ); 
}

15.

Handle the LEADTwain1 OnAcquirePageEvent event, and code the LEADTwain1AcquirePageEvent Function as follows:

int __fastcall TForm1::LEADTwain1AcquirePageEvent(TBITMAPHANDLE Bitmap) 
{
 ShowMessage ( "Acquisition of Image Done" ); 
   LEADImage1->InsertBitmapListItem ( LST_APPEND, Bitmap ); 
 return SUCCESS; 
}

16.

Run your program to test it.