Set/Get a Twain Capability (Delphi)

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 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 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 5 button controls and name them as follows:

 

Name

Caption

 

btnSelectSource

Select Source

 

btnGetCapability

Get Capability

 

btnSetCapability

Set Capability

 

btnSetBrightness

Set Brightness

 

btnAcquire

Acquire

6.

Add the following variables declaration to the Form1 private section.

   uItemValue: L_UINT;
   twCap: TW_CAPABILITY;

 

7.

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

procedure TForm1.FormCreate(Sender: TObject);
begin
   LEADTwain1.InitSession ( Handle );
   LEADTwain1.EnableMethodErrors:= False;

   { Unlock Document support.
      Note that this is a sample key, which will not work in your toolkit. }
   LEADTwain1.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
   LEADTwain1.EndSession ( );
end;

9.

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

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

10.

Handle the btnGetCapability OnClick event, and code the btnGetCapabilityClick procedure as follows.

procedure TForm1.btnGetCapabilityClick(Sender: TObject);
var
 nItemType: L_INT;
begin
   twCap.Cap:= ICAP_XFERMECH;
   twCap.ConType:= TWON_DONTCARE16;
   twCap.hContainer:= 0;
   LEADTwain1.GetCapability ( @twCap, LTWAIN_CAPABILITY_GETCURRENT );

   LEADTwain1.GetNumericContainerItemType ( @twCap, @nItemType );

   if ( nItemType = TWTY_UINT16 ) then
      LEADTwain1.GetNumericContainerUINTValue ( @twCap, 0, @uItemValue )
   else
      MessageBox ( Handle, 'Returned bad item type', 'Get Item Type', MB_OK );
end;

 

11.

Handle the btnSetCapability OnClick event, and code the btnSetCapabilityClick procedure as follows.

procedure TForm1.btnSetCapabilityClick(Sender: TObject);
var
 twSetCap: TW_CAPABILITY;
begin
   if ( uItemValue <> TWSX_NATIVE ) then
   begin
      twSetCap.Cap:= ICAP_XFERMECH;
      twSetCap.ConType:= TWON_ONEVALUE;

      LEADTwain1.CreateNumericContainerOneValue ( @twSetCap, TWAINNUMERICTYPE_TW_UINT16, TWSX_NATIVE );

      LEADTwain1.SetCapability ( @twSetCap, LTWAIN_CAPABILITY_SET );
      LEADTwain1.FreeContainer ( @twSetCap );
   end
   else
      MessageBox ( Handle, 'The Transfer mode is Native', 'Set Capability', MB_OK );
end;

 

12.

Handle the btnSetBrightness OnClick event, and code the btnSetBrightnessClick procedure as follows:

procedure TForm1.btnSetBrightnessClick(Sender: TObject);
var
 nRet: L_INT;
 twFix32: TW_FIX32;
 ptwOneVal: pTW_ONEVALUE;
 twBrightnessCap: TW_CAPABILITY;

begin
 twBrightnessCap.Cap:= ICAP_BRIGHTNESS;
 twBrightnessCap.ConType:= TWON_ONEVALUE;

 nRet:= LEADTwain1.GetCapability ( @twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT );
   if ( nRet = SUCCESS ) then
   begin

      ptwOneVal:= pTW_ONEVALUE(GlobalLock(twCap.hContainer));

      CopyMemory ( @twFix32, @ptwOneVal.Item, sizeof(TW_FIX32) );

      GlobalUnlock( twBrightnessCap.hContainer );
      GlobalFree( twBrightnessCap.hContainer );
      twBrightnessCap.hContainer:= 0;

      if ( twFix32.Whole <> 128 ) then
      begin
         twFix32.Whole:= 128;
         twFix32.Frac:= 0;
      end;

      twBrightnessCap.Cap:= ICAP_BRIGHTNESS;
      twBrightnessCap.ConType:= TWON_ONEVALUE;
      twBrightnessCap.hContainer:= GlobalAlloc ( GHND, sizeof(TW_ONEVALUE) + sizeof(TW_FIX32) );

      ptwOneVal:= pTW_ONEVALUE(GlobalLock(twCap.hContainer));

      ptwOneVal.ItemType:= TWTY_FIX32;
      CopyMemory ( @ptwOneVal.Item, @twFix32, sizeof(TW_FIX32) );
      GlobalUnlock ( twBrightnessCap.hContainer );

      LEADTwain1.SetCapability ( @twBrightnessCap, LTWAIN_CAPABILITY_SET );
   end;
end;

13.

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;

14.

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

function TForm1.LEADTwain1AcquirePageEvent(Bitmap: TBITMAPHANDLE): Integer;
begin
 LEADImage1.Bitmap:= Bitmap;
   Result:= SUCCESS;
end;

15.

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

16.

Run your program to test it.