Acquiring an Image (Delphi 4.0)
Take the following steps to start a project and to add some code that acquires an image from a TWAIN source:
1. |
Start Delphi 4.0. |
2. |
If you didn’t install LEAD RasterView Control, install it as follows: |
|
On the Component pull-down menu, use the Import ActiveX Control… and select the LEAD RasterView Control (14.5). Press install, and then compile and install the package dclusr40.dpk. |
3. |
Select the LEAD RasterView control and add it to Form1. Size and position the control as you want it to appear at run time. |
4. |
On the Project pull-down menu, use the Import Type library… and select the LEAD RasterTwain Object Library (14.5). Press OK. |
5. |
If you didn’t import LEAD Raster Variant before, import it as follows: |
6. |
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. |
7. |
If you didn’t install LEADEventSink Control, install it as follows: |
|
On the Component pull-down menu, use Install component …, and browse to LEADXX\Include\LEvntSnk.pas. Press OK, and then compile and install the package dclusr40.dpk. |
8. |
From the ActiveX controls, add the LEADEventSink control to Form1. |
9. |
Add the following code to the private section of Unit1: |
TwainCap: LEADTwainCapability;
RasterTwain: LEADRasterTwain;
nXferMode: Integer;
nRet: Integer;
Function TwainSetCapability ( uCapability: Integer; CapVal: LEADRasterVariant; uItemType: Integer): Integer;
10. |
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.InitSession ( Handle ) ;
LEADEventSink1.Connect ( RasterTwain, _LEADRasterTwainEvents );
end;
11. |
Handle the Form1 OnClose event, and code the FormClose procedure as follows: |
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
RasterTwain.EndSession();
end;
12. |
At the top of Form1, add six Buttons and three Option Buttons and name them as follows: |
|
|
Name |
Caption |
|
btnOK |
OK |
|
btnCancel |
Cancel |
|
btnAcquire |
Acquire |
|
btnSelectSource |
Select Source |
|
btnNative |
Native |
|
btnMemBuf |
Memory Buffered |
|
btnFile |
File |
|
btnSaveTemplate |
Save Template File |
|
btnLoadTemplate |
Load Template File |
13. |
Handle the btnOK OnClick event, and code the btnOKClick procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code. |
procedure TForm1.btnOKClick(Sender: TObject);
begin
Close ( ) ;
end;
14. |
Handle the btnCancel OnClick event, and code the btnCancelClick procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code: |
procedure TForm1.btnCancelClick(Sender: TObject);
begin
Close ( ) ;
end;
15. |
Handle the btnAcquire OnClick event, and code the btnAcquireClick procedure as follows: |
procedure TForm1.btnAcquireClick(Sender: TObject);
begin
RasterTwain.Acquire ( L_LTWAIN_SHOW_USER_INTERFACE ) ;
end;
16. |
Handle the btnSelectSource OnClick event, and code the btnSelectSourceClick procedure as follows: |
procedure TForm1.btnSelectSourceClick(Sender: TObject);
begin
RasterTwain.SelectSource ( ) ;
end;
17. |
Handle the btnSaveTemplate OnClick event, and code the btnSaveTemplateClick procedure as follows: |
procedure TForm1.btnSaveTemplateClick(Sender: TObject);
begin
nRet:= RasterTwain.SaveTemplate ('c:\test.ltt');
end;
18. |
Handle the btnLoadTemplate OnClick event, and code the btnLoadTemplateClick procedure as follows: |
procedure TForm1.btnLoadTemplateClick(Sender: TObject);
begin
nRet:= RasterTwain.LoadTemplate ( 'c:\test.ltt' ) ;
end;
19. |
Handle the btnNative option button’s OnClick event, and code the btnNativeClick procedure as Follows: |
procedure TForm1.btnNativeClick(Sender: TObject);
var
CapVal: LEADRasterVariant;
begin
nXferMode:= L_TWSX_NATIVE;
CapVal:= coLEADRasterVariant.Create ( );
CapVal.Type_:= VALUE_USHORT;
CapVal.LongValue:= nXferMode;
TwainSetCapability ( L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16 );
end;
20. |
Handle the btnMemBuf option button’s OnClick event, and code the btnMemBufClick procedure as follows: |
procedure TForm1.btnMemBufClick(Sender: TObject);
var
CapVal: LEADRasterVariant;
begin
nXferMode:= L_TWSX_MEMORY;
CapVal:= coLEADRasterVariant.Create ( );
CapVal.Type_:= VALUE_USHORT;
CapVal.LongValue:= nXferMode;
TwainSetCapability ( L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16 );
end;
21. |
Handle the btnFile option button’s OnClick event, and code the btnFileClick procedure as follows: |
procedure TForm1.btnFileClick(Sender: TObject);
var
CapVal: LEADRasterVariant;
begin
nXferMode:= L_TWSX_FILE;
CapVal:= coLEADRasterVariant.Create ( );
CapVal.Type_:= VALUE_USHORT;
CapVal.LongValue:= nXferMode;
TwainSetCapability ( L_ICAP_XFERMECH, CapVal, L_TWTY_UINT16 );
RasterTwain.FileTransferName:= 'c:\twain.bmp';
end;
22. |
Handle the LEADEventSink1 OnInvoke event, and code the LEADEventSink1Invoke procedure as follows: |
procedure TForm1.LEADEventSink1Invoke(Sender: TObject; DispID: Integer;
const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
varResult, ExcepInfo, ArgErr: Pointer);
var
pBitmap: Integer;
begin
Case (DispID) of
LEADRASTER_TWAIN_EVENTS_ACQUIREPAGEEVENT:
begin
pBitmap:= OleVariant ( Params.rgvarg[0] ) ;
ShowMessage ( 'Acquisition of Image Done' ) ;
LEADRasterView1.Raster.InsertBitmapListItem ( -1, pBitmap ) ;
end;
end;
end;
23. |
Add the TwainSetCapability function body to the Unit1 file. |
Function TForm1.TwainSetCapability ( uCapability: Integer; CapVal: LEADRasterVariant; uItemType: Integer): Integer;
var
iRet: Integer;
begin
TwainCap.EnableMethodErrors:= False;
TwainCap.CapInfo.Capability:= uCapability;
TwainCap.CapInfo.ConType:= L_TWON_ONEVALUE;
TwainCap.CapOneValue.OneValItemType:= uItemType;
TwainCap.CapOneValue.OneValCapValue:= CapVal;
iRet:= RasterTwain.SetCapability2(TwainCap, L_LTWAIN_CAPABILITY_SET);
TwainSetCapability:= iRet;
end;
24. |
At the beginning of the Unit1 file, add ComObj, LTRASTERVARIANTLib_TLB, LTRASTERTWAINLib_TLB, ActiveX to the uses section. This lets you handle the Com objects, the Raster Twain object library, and use ActiveX Controls. |
25. |
Run your program to test it. |