BSOne.SFC/EM.Lib/ImageEn_SRC/Demos/ImageAcquisition/AllAcquire/uMain.pas

392 lines
13 KiB
Plaintext

unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
ImgList, ieview, iemview, ComCtrls, hyieutils, iexBitmaps, hyiedefs, iesettings, iexLayers, iexRulers;
type
TfrmMain = class(TForm)
ImageEnMView1: TImageEnMView;
pnlMain: TPanel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
chkTwain: TCheckBox;
chkWIA: TCheckBox;
chkDCIM: TCheckBox;
btnSelectSource: TButton;
Label12: TLabel;
Label13: TLabel;
cmbSources: TComboBox;
Bevel1: TBevel;
Label14: TLabel;
grpGeneral: TGroupBox;
Label1: TLabel;
Label2: TLabel;
edtResY: TEdit;
edtResX: TEdit;
grpFrame: TGroupBox;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
chkFrameEnabled: TCheckBox;
btnCheckParameters: TButton;
btnAcquire: TButton;
ProgressBar1: TProgressBar;
memInfo: TMemo;
edtLeft: TEdit;
edtRight: TEdit;
edtTop: TEdit;
edtBottom: TEdit;
Label3: TLabel;
cmbColors: TComboBox;
Label15: TLabel;
Label16: TLabel;
cmbOrientation: TComboBox;
cmbRotation: TComboBox;
chkFeederEnabled: TCheckBox;
chkAutoFeed: TCheckBox;
chkDuplexEnabled: TCheckBox;
Label17: TLabel;
chkVisibleDialog: TCheckBox;
lblSaveToReg: TLabel;
lblLoadFromReg: TLabel;
btnAbort: TButton;
imlDevices: TImageList;
chkEnableLogging: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure btnAbortClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure cmbSourcesChange(Sender: TObject);
procedure btnCheckParametersClick(Sender: TObject);
procedure btnAcquireClick(Sender: TObject);
procedure btnSelectSourceClick(Sender: TObject);
procedure ImageEnIO1Progress(Sender: TObject; per: Integer);
procedure cmbSourcesDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure lblLoadFromRegClick(Sender: TObject);
procedure lblSaveToRegClick(Sender: TObject);
private
{ Private declarations }
fInternalChange: Boolean;
fAborted : Boolean;
procedure SaveProperties;
procedure UpdateControlStatus(bSelectDevice : Boolean = False);
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.DFM}
{$R WindowsTheme.res}
uses
iexAcquire, iexRegistryFunctions, iexWindowsFunctions;
const
Acquire_Orientation: array [ ieaoPortrait .. ieaoRotate180 ] of string = ( 'Portrait', 'Landscape', 'Rotate 90', 'Rotate 180' );
Acquire_Rotation: array [ iearNone .. iear270CW ] of string = ( 'None', '90 deg. clockwise', '180 deg. clockwise', '270 deg. clockwise' );
Acquire_PixelType: array [ ieapMonochrome .. ieapOther ] of string = ( 'Monochrome', '8 bit GrayScale', '16 bit GrayScale', 'Full Color', '16 bit Full Color', 'Other' );
procedure TfrmMain.FormCreate(Sender: TObject);
begin
ImageEnMView1.SetModernStyling( True, 120, 100 );
end;
// Abort acquisition
procedure TfrmMain.btnAbortClick(Sender: TObject);
begin
fAborted := True;
btnAbort.Enabled := False;
end;
// Initialize controls
procedure TfrmMain.FormActivate(Sender: TObject);
var
i: integer;
begin
cmbColors.Clear;
// In newer Delphi versions For.. In.. would be much simpler
for I := ord( Low( Acquire_PixelType )) to ord( High( Acquire_PixelType )) do
cmbColors.Items.Add( Acquire_PixelType[ TIEAcquirePixelType( i ) ] );
cmbOrientation.Clear;
for I := ord( Low( Acquire_Orientation )) to ord( High( Acquire_Orientation )) do
cmbOrientation.Items.Add( Acquire_Orientation[ TIEAcquireOrientation( i ) ] );
cmbRotation.Clear;
for I := ord( Low( Acquire_Rotation )) to ord( High( Acquire_Rotation )) do
cmbRotation.Items.Add( Acquire_Rotation[ TIEAcquireRotation( i ) ] );
// Fill sources
ImageEnMView1.MIO.AcquireParams.FillListWithSources( cmbSources.Items );
UpdateControlStatus( True );
end;
// User selects scanner
procedure TfrmMain.cmbSourcesChange(Sender: TObject);
begin
if fInternalChange then
exit;
if cmbSources.ItemIndex > -1 then
ImageEnMView1.MIO.AcquireParams.SetSourceByStr( cmbSources.Items[ cmbSources.ItemIndex ] );
UpdateControlStatus;
end;
// Read parameters again
procedure TfrmMain.btnCheckParametersClick(Sender: TObject);
begin
SaveProperties;
// Verify Twain properties...
if ImageEnMView1.MIO.SelectedAcquireSource.API = ieaTwain then
ImageEnMView1.MIO.TwainParams.Update;
// Read back: Some properties may have altered due to TwainParams.Update
UpdateControlStatus;
end;
// Set values to controls
procedure TfrmMain.UpdateControlStatus(bSelectDevice : Boolean = False);
const
Acquire_Apis : array [ ieaTwain .. ieaNone ] of string = ( 'Twain', 'WIA', 'DCIM', 'None' );
Device_Types : array [ ieadUnknown .. ieadDrive ] of string = ( 'Unknown', 'Scanner', 'Camera', 'Drive' );
Bool_Types : array [ False .. True ] of string = ( 'False', 'True' );
procedure _Add(const sName, sValue : string);
begin
memInfo.Lines.Add( sName + ': ' + sValue );
end;
procedure _EnableControls(AControl : TWinControl; bEnable : Boolean);
var
I: Integer;
begin
AControl.Enabled := bEnable;
for I := 0 to AControl.ControlCount - 1 do
AControl.Controls[I].Enabled := bEnable;
end;
var
bHaveTrueApiSource: Boolean;
begin
if bSelectDevice then
begin
fInternalChange := True;
cmbSources.ItemIndex := cmbSources.Items.IndexOf( AcquireSourceToStr( ImageEnMView1.MIO.AcquireParams.SelectedSource ));
fInternalChange := False;
end;
memInfo.Clear;
bHaveTrueApiSource := ImageEnMView1.MIO.AcquireParams.SelectedSource.API in [ ieaTwain, ieaWIA ];
_EnableControls( grpFrame, bHaveTrueApiSource );
_EnableControls( grpGeneral, bHaveTrueApiSource );
btnCheckParameters.Enabled := bHaveTrueApiSource;
if ImageEnMView1.MIO.AcquireParams.SelectedSource.API <> ieaNone then
with ImageEnMView1.MIO do
begin
_Add( 'Device Name', String( AcquireParams.SelectedSource.Name ));
_Add( 'API', Acquire_Apis[AcquireParams.SelectedSource.API ] );
_Add( 'Device Type', Device_Types[ AcquireParams.SelectedSource.DeviceType ] );
_Add( 'Bit Depth', IntToStr( AcquireParams.BitDepth ));
_Add( 'Physical Size', format( '%.2f" x %.2f" (%d x %d px)',
[ AcquireParams.PhysicalWidth,
AcquireParams.PhysicalHeight,
AcquireParams.PhysicalWidthPixels,
AcquireParams.PhysicalHeightPixels ] ));
_Add( 'Max Paper Size: ', IEPaperSizeToStr( IECalcPaperSize( AcquireParams.PhysicalWidth, AcquireParams.PhysicalHeight, True )) + '/' +
IEPaperSizeToStr( IECalcPaperSize( AcquireParams.PhysicalWidth, AcquireParams.PhysicalHeight, False )));
_Add( 'Feeder Loaded', Bool_Types[ AcquireParams.FeederLoaded ] );
_Add( 'Paper Detectable', Bool_Types[ AcquireParams.PaperDetectable ] );
_Add( 'Duplex Supported', Bool_Types[ AcquireParams.DuplexSupported ] );
_Add( 'Contrast', FloatToStr( AcquireParams.Contrast ));
_Add( 'Brightness', FloatToStr( AcquireParams.Brightness ));
_Add( 'Threshold', FloatToStr( AcquireParams.Threshold ));
memInfo.SelStart := 0;
memInfo.SelLength := 0;
edtResX.Text := FloatToStr( AcquireParams.XResolution );
edtResY.Text := FloatToStr( AcquireParams.YResolution );
cmbColors.ItemIndex := ord( AcquireParams.PixelType );
cmbOrientation.ItemIndex := ord( AcquireParams.Orientation );
cmbRotation.ItemIndex := ord( AcquireParams.Rotation );
chkFeederEnabled.Checked := AcquireParams.FeederEnabled;
chkAutoFeed.Checked := AcquireParams.AutoFeed;
chkDuplexEnabled.Checked := AcquireParams.DuplexEnabled;
// frame
edtLeft.Text := FloatToStr( AcquireParams.AcquireFrameLeft );
edtTop.Text := FloatToStr( AcquireParams.AcquireFrameTop );
edtRight.Text := FloatToStr( AcquireParams.AcquireFrameRight );
edtBottom.Text := FloatToStr( AcquireParams.AcquireFrameBottom );
chkFrameEnabled.checked := AcquireParams.AcquireFrameEnabled;
chkVisibleDialog.Checked := AcquireParams.VisibleDialog;
chkDuplexEnabled.Enabled := AcquireParams.DuplexSupported;
chkVisibleDialog.Enabled := AcquireParams.SelectedSource.API <> ieaDCIM;
if AcquireParams.SelectedSource.API = ieaWIA then
chkFrameEnabled.Enabled := False;
end;
end;
// Write control values to AcquireParams
procedure TfrmMain.SaveProperties;
begin
with ImageEnMView1.MIO do
begin
AcquireParams.XResolution := StrToFloat( edtResX.Text );
AcquireParams.YResolution := StrToFloat( edtResY.Text );
AcquireParams.PixelType := TIEAcquirePixelType( cmbColors.ItemIndex );
AcquireParams.Orientation := TIEAcquireOrientation( cmbOrientation.ItemIndex );
AcquireParams.Rotation := TIEAcquireRotation( cmbRotation.ItemIndex );
AcquireParams.FeederEnabled := chkFeederEnabled.Checked;
AcquireParams.AutoFeed := chkAutoFeed.Checked;
AcquireParams.DuplexEnabled := chkDuplexEnabled.Checked;
AcquireParams.AcquireFrameLeft := StrToFloat( edtLeft.Text );
AcquireParams.AcquireFrameTop := StrToFloat( edtTop.Text );
AcquireParams.AcquireFrameRight := StrToFloat( edtRight.Text );
AcquireParams.AcquireFrameBottom := StrToFloat( edtBottom.Text );
AcquireParams.AcquireFrameEnabled := chkFrameEnabled.checked;
AcquireParams.VisibleDialog := chkVisibleDialog.Checked;
end;
end;
// Acquire from selected device
procedure TfrmMain.btnAcquireClick(Sender: TObject);
begin
if ImageEnMView1.MIO.AcquireParams.SelectedSource.API = ieaNone then
begin
messagedlg( 'You have not specified an acquisition source', mtInformation, [mbOK], 0 );
exit;
end;
if (cmbColors.Enabled ) and
( not ImageEnMView1.MIO.AcquireParams.IsPixelTypeSupported( TIEAcquirePixelType( cmbColors.ItemIndex ))) then
begin
MessageDlg( 'The selected color is not supported by your device.', mtError, [mbOK], 0 );
exit;
end;
fAborted := False;
btnAbort.Enabled := True;
btnAbort.Visible := True;
btnAcquire.Enabled := False;
try
Screen.Cursor := crHourglass;
ImageEnMView1.Clear;
SaveProperties;
if chkEnableLogging.Checked then
ImageEnMView1.MIO.TwainParams.LogFile := WindowsTempFolder() + 'ietwainlog.txt'
else
ImageEnMView1.MIO.TwainParams.LogFile := '';
ImageEnMView1.MIO.Acquire;
finally
btnAbort.Visible := False;
btnAcquire.Enabled := True;
Screen.Cursor := crDefault;
end;
ProgressBar1.Position := 0;
end;
// Prompt the user for an acquire source
procedure TfrmMain.btnSelectSourceClick(Sender: TObject);
var
AcquireApis : TIEAcquireApis;
begin
AcquireApis := [];
if chkTwain.checked then
Include( AcquireApis, ieaTwain );
if chkWIA.checked then
Include( AcquireApis, ieaWIA );
if chkDCIM.checked then
Include( AcquireApis, ieaDCIM );
if AcquireApis = [] then
messagedlg( 'You have not specified any acquisition Apis', mtInformation, [mbOK], 0 )
else
begin
ImageEnMView1.MIO.SelectAcquireSource( AcquireApis );
UpdateControlStatus( True );
end;
end;
// Display progress during acquisition
procedure TfrmMain.ImageEnIO1Progress(Sender: TObject; per: Integer);
const
Show_Images_As_Acquired = True;
begin
ProgressBar1.Position := per;
if Show_Images_As_Acquired then
begin
ImageEnMView1.Update;
application.processmessages;
// User has clicked abort?
if fAborted then
ImageEnMView1.MIO.Aborting := True;
end;
end;
// Handle custom drawing of combobox
procedure TfrmMain.cmbSourcesDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
const
Scanner_Glyph_Index = 0;
Camera_Glyph_Index = 1;
Drive_Glyph_Index = 2;
Device_Glyph_Index = 3;
Unknown_Glyph_Index = Scanner_Glyph_Index; // Just assume it is a scanner
begin
// Convert values in cmbSources (which are a coded string) to something human-readable
DrawAcquireComboListBoxItem( Control, Rect, cmbSources.Items[Index], imlDevices,
Scanner_Glyph_Index, Camera_Glyph_Index, Drive_Glyph_Index, Device_Glyph_Index, Unknown_Glyph_Index );
end;
// Read settings from registry
procedure TfrmMain.lblLoadFromRegClick(Sender: TObject);
begin
ImageEnMView1.MIO.AcquireParams.LoadFromRegistry( 'Software\Xequte\AllAcquireDemo\' );
UpdateControlStatus( True );
end;
// Save current settings to registry
procedure TfrmMain.lblSaveToRegClick(Sender: TObject);
begin
// write changes to our AcquireParams object
btnCheckParametersClick( nil );
ImageEnMView1.MIO.AcquireParams.SaveToRegistry( 'Software\Xequte\AllAcquireDemo\' );
end;
end.