Set/Get a Twain Capability (C++ Builder)

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

    L_UINT uItemValue;
    TW_CAPABILITY twCap; 

7.

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

void __fastcall TForm1::FormCreate(TObject *Sender) 
{
   LEADTwain1->InitSession ( (L_HANDLE)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" ); 
}

8.

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

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

9.

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

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

10.

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

void __fastcall TForm1::btnGetCapabilityClick(TObject *Sender) 
{
 L_INT nItemType; 

   twCap.Cap= ICAP_XFERMECH; 
   twCap.ConType= TWON_DONTCARE16; 
   twCap.hContainer= NULL; 
   LEADTwain1->GetCapability ( &twCap, LTWAIN_CAPABILITY_GETCURRENT ); 

   LEADTwain1->GetNumericContainerItemType ( &twCap, &nItemType ); 

   if ( nItemType == TWTY_UINT16 ) 
      LEADTwain1->GetNumericContainerUINTValue ( &twCap, 0, &uItemValue ); 
   else
      MessageBox ( Handle, "Returned bad item type", "Get Item Type", MB_OK ); 
}

11.

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

void __fastcall TForm1::btnSetCapabilityClick(TObject *Sender) 
{
 TW_CAPABILITY twSetCap; 

   if ( uItemValue != TWSX_NATIVE ) 
   {
      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 ); 
   }
   else
      MessageBox ( Handle, "The Transfer mode is Native", "Set Capability", MB_OK ); 
}

12.

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

void __fastcall TForm1::btnSetBrightnessClick(TObject *Sender) 
{
 L_INT nRet; 
 TW_FIX32 twFix32; 
 pTW_ONEVALUE ptwOneVal; 
 TW_CAPABILITY twBrightnessCap; 

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

 nRet= LEADTwain1->GetCapability ( &twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT ); 
   if ( nRet == SUCCESS ) 
 {
      ptwOneVal= (pTW_ONEVALUE)(GlobalLock((void*)twCap.hContainer)); 

      CopyMemory ( &twFix32, &ptwOneVal->Item, sizeof(TW_FIX32) ); 

      GlobalUnlock( (void*)twBrightnessCap.hContainer ); 
      GlobalFree( (void*)twBrightnessCap.hContainer ); 
      twBrightnessCap.hContainer= NULL; 

      if ( twFix32.Whole != 128 ) 
      {
         twFix32.Whole= 128; 
         twFix32.Frac= 0; 
      }

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

      ptwOneVal= (pTW_ONEVALUE)(GlobalLock((void*)twCap.hContainer)); 

      ptwOneVal->ItemType= TWTY_FIX32; 
      CopyMemory ( &ptwOneVal->Item, &twFix32, sizeof(TW_FIX32) ); 
      GlobalUnlock ( (void*)twBrightnessCap.hContainer ); 

      LEADTwain1->SetCapability ( &twBrightnessCap, LTWAIN_CAPABILITY_SET ); 
   }
}

13.

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

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

14.

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

int __fastcall TForm1::LEADTwain1AcquirePageEvent(TBITMAPHANDLE Bitmap) 
{
 LEADImage1->Bitmap= Bitmap; 
   return SUCCESS; 
}

15.

Run your program to test it.