Using the Fast TWAIN Feature (Delphi 4.0)

Take the following steps to start a project and to add some code that acquires the images using Fast Twain:

1.

Start Delphi 4.0.

2.

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

3.

Add the following code to the private section of Unit1:

TwainCap: LEADTwainCapability; 
RasterTwain: LEADRasterTwain; 
nRet: Integer;

4.

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

procedure TForm1.FormCreate(Sender: TObject); 
begin
   TwainCap:= CreateComObject ( CLASS_LEADTwainCapability ) as LEADTwainCapability; 
   RasterTwain:= CreateComObject ( CLASS_LEADRasterTwain) as LEADRasterTwain; 
   RasterTwain.UnlockSupport ( L_SUPPORT_DOCUMENT, 'TestKey' ) ; 
   RasterTwain.InitSession ( Handle ) ; 
   RasterTwain.EnableMethodErrors:= False; 
   rbMemory.Checked:= True; 
end;

5.

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

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

6.

At the top of Form1, add a check box and name it as follows:

 

Name

Caption

 

chkUseBufferSize

Use Buffer Size

7.

Add 2 radio buttons, and name them as follows:

 

Name

Caption

 

rbNative

Native Mode

 

rbMemory

Memory Mode

8.

Add 1 button, and name it as follows:

 

Name

Caption

 

btnAcquire

Fast Acquire

9.

Add 2 Edit boxes, and name them as follows:

edtBufferSize
edtFName

10.

Handle the btnAcquire control's OnClick event, and code the btnAcquireClick procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

procedure TForm1.btnAcquireClick(Sender: TObject); 
begin
   if ( rbMemory.Checked = True ) then
   begin
      RasterTwain.FastTransferMode:= L_LTWAIN_BUFFER_MODE ; 
      RasterTwain.FastFormat:= FILE_CCITT_GROUP4 ; 
   end
   else
   begin
      RasterTwain.FastTransferMode:= L_LTWAIN_NATIVE_MODE ; 
      RasterTwain.FastFormat:= FILE_TIF; 
   end; 

   RasterTwain.FastBitsPerPixel:= 1; 
   RasterTwain.FastUsePreferredBufferSize:= chkUseBufferSize.Checked; 
   if ( RasterTwain.FastUsePreferredBufferSize ) then
      RasterTwain.FastBufferSize:= StrToInt(edtBufferSize.Text); 

   nRet:= RasterTwain.AcquireMulti (edtFName.Text, L_LTWAIN_SHOW_USER_INTERFACE Or L_LTWAIN_USE_THREAD_MODE, True); 
   if ( nRet = 0 ) then
      ShowMessage ( 'Fast Scanning process done successfully...' ) 
   else
      ShowMessage ( 'AcquireMulti failed, Error = ' + IntToStr ( nRet ) ) ; 
end;

11.

At the beginning of the Unit1 file, add ComObj, LTRASTERTWAINLib_TLB, LTDLLDef, ActiveX to the uses section. This lets you handle the Com objects, the Raster Twain object library, use the LEAD Defines in Delphi, and use the ActiveX Controls.

12.

Save your project and run your program to test it.