Available in the LEADTOOLS Imaging toolkit. |
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 4.0. |
2. |
On the Project pull-down menu, use Import Type library… and select the LEAD Raster Object Library. Press OK. |
3. |
On the Project pull-down menu, use Import Type library… and select the LEAD RasterTwain Object Library. 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. Press install, and then compile and install the package dclusr40.bpk. |
6. |
Declare the following variables in the public section of "Unit1.h": |
LEADTwainCapability* pTwainCap1;
LEADTwainCapability* pTwainCap2;
LEADTwainCapability* pTwainCap3;
LEADRasterTwain* pRasterTwain;
int nRet;
7. |
Handle the Form1 OnCreate event, and code the FormCreate procedure as follows: |
void __fastcall TForm1::FormCreate(TObject *Sender)
{
CoInitialize ( NULL );
CoCreateInstance (CLSID_LEADRasterTwain, NULL, CLSCTX_ALL, IID_ILEADRasterTwain, (void**)&pRasterTwain);
CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pTwainCap1);
CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pTwainCap2);
CoCreateInstance (CLSID_LEADRasterTwainCapability, NULL, CLSCTX_ALL, IID_ICapability, (void **)&pTwainCap3);
/* Unlock Document support.
Note that this is a sample key, which will not work in your toolkit. */
if ( pRasterTwain && pTwainCap1 && pTwainCap2 && pTwainCap3 )
{
pRasterTwain->InitSession ( (long)Handle );
pRasterTwain->EnableMethodErrors = false;
pRasterTwain->UnlockSupport ( L_SUPPORT_DOCUMENT, AnsiToOLESTR("TestKey") );
}
}
8. |
Handle the Form1 OnClose event, and code the FormClose procedure as follows: |
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if ( pRasterTwain )
{
pRasterTwain->EndSession ( );
pRasterTwain->Release( );
}
if ( pTwainCap1 )
pTwainCap1->Release( );
if ( pTwainCap1 )
pTwainCap1->Release( );
if ( pTwainCap2 )
pTwainCap2->Release( );
if ( pTwainCap3 )
pTwainCap3->Release( );
pRasterTwain= NULL;
pTwainCap1= NULL;
pTwainCap2= NULL;
pTwainCap3= NULL;
CoUninitialize ( );
}
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. |
void __fastcall TForm1::btnSelectSourceClick(TObject *Sender)
{
pRasterTwain->SelectSource ( ) ;
}
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. |
void __fastcall TForm1::btnUseCapabilityClick(TObject *Sender)
{
ILEADRasterVariant* pXfer= NULL;
pTwainCap1->set_EnableMethodErrors ( false );
pTwainCap1->CapInfo->ConType = L_TWON_ONEVALUE;
pTwainCap1->CapInfo->Capability = L_ICAP_XFERMECH;
nRet = pRasterTwain->GetCapability2 ( pTwainCap1, L_LTWAIN_CAPABILITY_GETCURRENT );
if ( nRet == 0 )
{
pXfer = pTwainCap1->CapOneValue->get_OneValCapValue();
if ( pXfer->LongValue != L_TWSX_FILE )
{
pTwainCap2->EnableMethodErrors = false;
pTwainCap2->CapInfo->Capability = L_ICAP_XFERMECH;
pTwainCap2->CapInfo->ConType = L_TWON_ONEVALUE;
pTwainCap2->CapOneValue->OneValItemType = L_TWTY_UINT16;
pTwainCap2->CapOneValue->OneValCapValue = pXfer;
nRet = pRasterTwain->SetCapability2 ( pTwainCap2, L_LTWAIN_CAPABILITY_SET );
}
else
ShowMessage ( "The Transfer mode is File" );
}
else
ShowMessage ( "Error occurred in the GetCapability method" );
}
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. |
void __fastcall TForm1::btnSetBrightnessClick(TObject *Sender)
{
int iRet;
ILEADRasterVariant* pCapVal;
pTwainCap3->CapInfo->ConType = L_TWON_ONEVALUE;
pTwainCap3->CapInfo->Capability = L_ICAP_BRIGHTNESS;
pTwainCap3->EnableMethodErrors = false;
CoCreateInstance(CLSID_LEADRasterVariant,
NULL,
CLSCTX_ALL,
IID_ILEADRasterVariant,
(void**)&pCapVal );
pCapVal->Type= VALUE_USHORT;
pCapVal->LongValue= 50;
pTwainCap3->CapOneValue->OneValItemType = L_TWTY_FIX32;
pTwainCap3->CapOneValue->set_OneValCapValue ( pCapVal );
iRet= pRasterTwain->SetCapability2 (pTwainCap3, L_LTWAIN_CAPABILITY_SET);
if (iRet != 0)
ShowMessage ( "Error Setting Capability" );
if (pCapVal)
pCapVal->Release();
}
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. |
void __fastcall TForm1::btnAcquireClick(TObject *Sender)
{
pRasterTwain->FileTransferName = AnsiToOLESTR("c:\\twain.bmp");
nRet= pRasterTwain->Acquire ( L_LTWAIN_SHOW_USER_INTERFACE ) ;
if ( nRet != 0 )
ShowMessage ( "Error acquiring from source" ) ;
}
14. |
In the Unit1.h file include these files. |
#include "LTRASTERTWAINLib_TLB.h"
#include "LTRASTERLib_TLB.h"
#include "LTRASTERVARIANTLib_TLB.h"
15. |
Save your project and run your program to test it. |