[DELPHI]Control scanner in Delphi
---- When using Delphi to develop information management systems, images are often processed, which is also an inevitable trend in the development of information management systems. How to obtain the required image information through a scanner in Delphi? There are no more than three basic methods:
Through image processing software, such as photoshop, etc., through their function of scanning images, they can be processed into images in a certain format and then used in your own system;
Via the scanner interface Twain.DLL. This interface is provided when installing the scanner. It is actually a Plugin that controls the scanner through a series of functions;
Control the scanner through component technology.
---- Using the first method can reduce the complexity of programming, but the system integration is not high and it is inconvenient for users to use; the second method can directly control the scanner at the driver level, which can
Provides maximum flexibility and controllability in programming; the third method can make full use of Delphi's visual component technology to use the scanner safely, flexibly and freely. In fact, the third method is also
It is built on top of the scanner interface Twain.DLL. The method discussed in this article adopts the third method.
---- Windows9X provides an "image" option in its attachment, which uses OCX components to control the scanner. There are four imgEdit, imgAnntool, imgScan, and imgAdmin. With these four components, basically Have full control over the scanner. The usage in Delphi is as follows: Select the installation "image" in Windows9x. (Select "Add/Remove Programs" in Control Panel, then select "Install Windows", click the "Accessories" check box, and then find "Image" from the "Details" button)
Start Delphi and select "Import ActiveX Control" under the "Component" menu item.
At this time, you can see that there are "Wang Image Editing Control, Wang Image Scanning Control, Wang
Image Management Control, Wang Image Thumbnail Control", select these four items, and then click "Install"
button, then you can see the icons of these four components under the ActiveX label on the component panel.
---- The main attributes of ImgScan: FileType: the file type of the image (this component supports three
Species: TIFF, BMP, AWD);
ScanTo: Return or set the purpose of the scanned image;
Possible value meaning
0 (default) Display the scanned image.
1 Display the scanned image and write it to a file.
2 Write the image to a file
3 Write the image into the file in template mode and display it.
4. Write the image into the file as a template
5 Fax scanned image
Note: If you take the default value 0,
Must be used in conjunction with the Wang ImgEdit component.
DestImageControl: Wang ImgEdit component name;
Zoom: the zoom ratio of the image
The main methods of ImgScan: OpenScanner: open the scanner;
CloseScanner: close the scanner;
StartScan: Start scanning;
StopScan: Stop scanning
----The properties of ImgEdit mainly control the resolution of the scanned image, the width and height of the image, and whether
Allows partial selection of images, etc.; the main methods include copying and cutting images to the pasteboard,
Copy images from the clipboard, rotate, scale, mirror images; print, save, refresh images, etc.
---- Detailed instructions for the above four OCX components can be viewed in Delphi.
Help system.
---- Here are some examples of using these two components:
PRocedure Tscanf.N1Click(Sender: TObject);
begin
ImgScan1.ShowSelectScanner;
file://Select the scanner installed on the system
end;
procedure Tscanf.N2Click(Sender: TObject);
begin
imgedit1.zoom:=100; file://Set the zoom factor of the image
if not imgscan1.ScannerAvailable then
file://if the selected scanner is not available
imgscan1.OpenScanner;//Open the scanner
imgscan1.StartScan; file://Start scanning images
imgscan1.CloseScanner; file://Close scanner
end;
procedure Tscanf.N8Click(Sender: TObject);
begin
if (imgedit1.IsClipboardDataAvailable
and imgedit1.ImageDisplayed ) then
imgedit1.ClipboardPaste(selleft,seltop);
file://paste the image from the clipboard, its parameter is the starting coordinate of Imgedit's display area
end;
procedure Tscanf.N1801Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
imgedit1.Flip; file://mirror image
end;
procedure Tscanf.N901Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
imgedit1.RotateRight file://Rotate image to the right
end;
procedure Tscanf.N6Click(Sender: TObject);
begin
imgedit1.DrawSelectionRect(selleft,
seltop, selwidth, selheight); // perform partial selection of the image
imgedit1.ClipboardCut(selleft,seltop,
selwidth,selheight);//Cut the selected area
end;
---- Note: When using the copy and cut functions of the Imgedit component in DELPHI, there will be
The following questions:
---- 1. If you select the entire image, there will be no problem;
---- 2. If a local area is selected, the copied and cut areas will drift;
---- 3. If DELPHI is making the client interface of MS SQL back-end database, then
When the image content is written to the image field of the database, the BDE (database engine) will report an error. This is
DELPHI3.0 and 4.0 BUG, 4.0 upgrade package can solve this problem.
---- As for the second problem, it can be solved like this: onSelectionRectDrawn in Imgedit
Write a piece of code in the event to save the boundary value of the selected area to four global variables. After copying
Just use the previously saved variables to make another selection before cutting (of course, all this will happen later)
stage, see previous example).
---- Development environment: DELPHI4.0, HP ScanJet 4P scanner;
---- Source code attached.
unit scanu;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, Menus, ScanLib_TLB, ExtCtrls, AxCtrls,
OleCtrls, ImgeditLib_TLB;
type
Tscanf = class(TForm)
MainMenu1: TMainMenu;
F1: TMenuItem;
N15: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N16: TMenuItem;
N11: TMenuItem;
N5: TMenuItem;
C1: TMenuItem;
N4: TMenuItem;
E1: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N3: TMenuItem;
N10: TMenuItem;
N9: TMenuItem;
N1801: TMenuItem;
N901: TMenuItem;
N902: TMenuItem;
H1: TMenuItem;
N12: TMenuItem;
N13: TMenuItem;
ImgEdit1: TImgEdit;
Bevel1: TBevel;
ImgScan1: TImgScan;
Bevel2: TBevel;
procedure N4Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure C1Click(Sender: TObject);
procedure N8Click(Sender: TObject);
procedure N1801Click(Sender: TObject);
procedure N901Click(Sender: TObject);
procedure N902Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N7Click(Sender: TObject);
procedure ImgEdit1SelectionRectDrawn(Sender: TObject; Left,Top,
Width, Height: Integer);
procedure N11Click(Sender: TObject);
private
{Private declarations}
public
selleft,seltop,selwidth,selheight:integer;
{Public declarations}
end;
var
scanf: Tscanf;
implementation
{$R *.DFM}
procedure Tscanf.N4Click(Sender: TObject);
begin
imgedit1.ClearDisplay;
close;
end;
procedure Tscanf.N1Click(Sender: TObject);
begin
ImgScan1.ShowSelectScanner;
end;
procedure Tscanf.N2Click(Sender: TObject);
begin
imgedit1.zoom:=100;
if not imgscan1.ScannerAvailable then
imgscan1.OpenScanner;
imgscan1.StartScan;
imgscan1.CloseScanner;
end;
procedure Tscanf.C1Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
begin
imgedit1.ClearDisplay;
imgedit1.zoom:=100;
end;
end;
procedure Tscanf.N8Click(Sender: TObject);
begin
if (imgedit1.IsClipboardDataAvailable and imgedit1.ImageDisplayed )
then
imgedit1.ClipboardPaste(selleft,seltop);
end;
procedure Tscanf.N1801Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
imgedit1.Flip;
end;
procedure Tscanf.N901Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
imgedit1.RotateRight
end;
procedure Tscanf.N902Click(Sender: TObject);
begin
if imgedit1.ImageDisplayed then
imgedit1.RotateLeft;
end;
procedure Tscanf.N6Click(Sender: TObject);
begin
imgedit1.DrawSelectionRect(selleft,seltop,selwidth,selheight);
imgedit1.ClipboardCut(selleft,seltop,selwidth,selheight);
end;
procedure Tscanf.N7Click(Sender: TObject);
begin
imgedit1.DrawSelectionRect(selleft,seltop,selwidth,selheight);
imgedit1.ClipboardCopy(selleft,seltop,selwidth,selheight);
end;
procedure Tscanf.ImgEdit1SelectionRectDrawn(Sender: TObject; Left,
Top, Width, Height: Integer);
begin
if ((width=0) and (height=0))then
begin
n3.Enabled:=false;
n6.Enabled:=false;
n7.Enabled:=false;
sellleft:=left;
seltop:=top;
selwidth:=width;
selheight:=height;
end
else begin
n6.Enabled:=true;
n7.Enabled:=true;
sellleft:=left;
seltop:=top;
selwidth:=width;
selheight:=height;
end;
end;
procedure Tscanf.N11Click(Sender: TObject);
begin
if not imgedit1.IsClipboardDataAvailable then
if imgedit1.imagedisplayed then
imgedit1.ClipboardCopy(0,0,imgedit1.Width,imgedit1.Height);
end;
end