Set/Get a Twain Capability (Delphi 4.0)

Take the following steps to start a project and to add some code to use the Get/Set capability methods and then acquire your image using the newly set capability:

1.

Start Delphi.

2.

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

3.

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

4.

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

5.

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

6.

Declare the following variables in the public section of Unit1:

 TwainCap1: LEADTwainCapability; 
 TwainCap2: LEADTwainCapability; 
 TwainCap3: LEADTwainCapability; 
 RasterTwain: LEADRasterTwain; 
 nRet: Integer;

7.

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

procedure TForm1.FormCreate(Sender: TObject);
begin
   RasterTwain:= coLEADRasterTwain.Create ( ); 
   TwainCap1:= coLEADTwainCapability.Create ( ); 
   TwainCap2:= coLEADTwainCapability.Create ( ); 
   TwainCap3:= coLEADTwainCapability.Create ( ); 

   RasterTwain.InitSession ( Handle ) ; 
   RasterTwain.
EnableMethodErrors:= False; 

   { Unlock Document support. 
      Note that this is a sample key, which will not work in your toolkit. }
   RasterTwain.
UnlockSupport( L_SUPPORT_DOCUMENT, 'TestKey' ) ;
end;

8.

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

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

9.

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

 

Name

Caption

 

btnSelectSource

Select Source

 

btnUseCapability

Use Capability

 

btnSetBrightness

Set Brightness

 

btnAcquire

Acquire

10.

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.

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

11.

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

procedure TForm1.btnUseCapabilityClick(Sender: TObject);
var
 Xfer: LEADRasterVariant; 
begin
   TwainCap1.
EnableMethodErrors:= False; 
   TwainCap1.CapInfo.
ConType:= L_TWON_ONEVALUE; 
   TwainCap1.CapInfo.
Capability:= L_ICAP_XFERMECH; 

   nRet:= RasterTwain.
GetCapability2( TwainCap1, L_LTWAIN_CAPABILITY_GETCURRENT ) ; 
   if ( nRet = 0 ) then
   begin
      Xfer:= TwainCap1.CapOneValue.
OneValCapValue
      if ( Xfer.LongValue <> L_TWSX_FILE ) then
      begin
         TwainCap2.
EnableMethodErrors:= False; 
         TwainCap2.CapInfo.
Capability:= L_ICAP_XFERMECH; 
         TwainCap2.CapInfo.
ConType:= L_TWON_ONEVALUE; 

         TwainCap2.CapOneValue.
OneValItemType:= L_TWTY_UINT16; 
         TwainCap2.CapOneValue.
OneValCapValue:= Xfer; 

         nRet:= RasterTwain.
SetCapability2( TwainCap2, L_LTWAIN_CAPABILITY_SET ); 
      end
      else
         ShowMessage ( 'The Transfer mode is File' ) ; 
   end
   else
      ShowMessage ( 'Error occurred in GetCapability method' ) ; 
end;

 

12.

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

procedure TForm1.btnSetBrightnessClick(Sender: TObject); 
var
   iRet: Integer; 
   CapVal: LEADRasterVariant; 
begin
   TwainCap3.CapInfo.
ConType:= L_TWON_ONEVALUE; 
   TwainCap3.CapInfo.
Capability:= L_ICAP_BRIGHTNESS; 
   TwainCap3.
EnableMethodErrors:= False; 

 CapVal:= coLEADRasterVariant.Create ( ); 
   CapVal.type_:= VALUE_USHORT; 
   CapVal.LongValue:= 50; 

   TwainCap3.CapOneValue.
OneValItemType:= L_TWTY_FIX32; 
   TwainCap3.CapOneValue.
OneValCapValue:= CapVal; 

   iRet:= RasterTwain.
SetCapability2(TwainCap3, L_LTWAIN_CAPABILITY_SET); 
   if (iRet <> 0) then
      ShowMessage ( 'Error Setting Capability' ); 
end;

13.

Handle the btnAcquire 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
   RasterTwain.FileTransferName:= 'c:\twain.bmp';
   nRet:= RasterTwain.Acquire ( L_LTWAIN_SHOW_USER_INTERFACE ) ;
   if ( nRet <> 0 ) then
     ShowMessage ( 'Error acquiring from source' ) ;
end;

14.

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

15.

Save your project and run your program to test it.