Acquiring an Image (Delphi)

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 Delphi.

2.

On the Delphi toolbar, click the LEADTOOLS tab. If you have used a LEAD VCL control before, the icon appears on the toolbar. Otherwise, refer to Installing VCLbefore 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

 

btnFile

File

6.

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

   procedure TForm1.FormCreate(Sender: TObject);
begin
 LEADTwain1.InitSession ( Handle ) ;
end;

7.

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

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 LEADTwain1.EndSession ( );
end;

 

8.

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

procedure TForm1.btnOKClick(Sender: TObject);
begin
   Close ( );
end;

9.

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

procedure TForm1.btnCancelClick(Sender: TObject);
begin
   Close ( );
end;

10.

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

procedure TForm1.btnAcquireClick(Sender: TObject);
begin
 LEADTwain1.Acquire ( LTWAIN_SHOW_USER_INTERFACE );
end;

11.

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

procedure TForm1.btnSelectSourceClick(Sender: TObject);
begin
 LEADTwain1.SelectSource ( );
end;

 

12.

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

procedure TForm1.btnNativeClick(Sender: TObject);
var
 nRet: L_INT;
 twProps: LTWAINPROPERTIES;
begin
 FillMemory ( @twProps, Sizeof(LTWAINPROPERTIES), 0 );
 nRet:= LEADTwain1.GetProperties ( @twProps, LTWAIN_PROPERTIES_GETCURRENT );
 if ( nRet <> SUCCESS ) then
  Exit;
 twProps.DataTransfer.nTransferMode:= TWSX_NATIVE;

 LEADTwain1.SetProperties ( @twProps, LTWAIN_PROPERTIES_SET );
end;

 

13.

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

procedure TForm1.btnMemBufClick(Sender: TObject);
var

 nRet: L_INT;
 twProps: LTWAINPROPERTIES;
begin
 FillMemory ( @twProps, Sizeof(LTWAINPROPERTIES), 0 );
 nRet:= LEADTwain1.GetProperties ( @twProps, LTWAIN_PROPERTIES_GETCURRENT );
 if ( nRet <> SUCCESS ) then
  Exit;
 twProps.DataTransfer.nTransferMode:= TWSX_MEMORY;
 twProps.DataTransfer.nBufMemCompression:= TWCP_NONE;
 LEADTwain1.SetProperties ( @twProps, LTWAIN_PROPERTIES_SET );
end;

14.

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

procedure TForm1.btnFileClick(Sender: TObject);
var
 nRet: L_INT;
 twProps: LTWAINPROPERTIES;
begin
 FillMemory ( @twProps, Sizeof(LTWAINPROPERTIES), 0 );
 nRet:= LEADTwain1.GetProperties ( @twProps, LTWAIN_PROPERTIES_GETCURRENT );
 if ( nRet <> SUCCESS ) then
  Exit;
 twProps.DataTransfer.nTransferMode:= TWSX_NATIVE;
 StrCopy ( twProps.DataTransfer.szFileName, 'c:\twain.bmp' );
 LEADTwain1.SetProperties ( @twProps, LTWAIN_PROPERTIES_SET );
end;

15.

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

function TForm1.LEADTwain1AcquirePageEvent(Bitmap: TBITMAPHANDLE): Integer;
begin
 ShowMessage ( 'Acquisition of Image Done' );
   LEADImage1.InsertBitmapListItem ( LST_APPEND, Bitmap );
 Result:= SUCCESS;
end;

16.

At the beginning of the Unit1 file, add LEADDef, and LEADTyp to the uses section.

17.

Run your program to test it.