unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,fpengine,StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB,DBTables,
  XPMan, ComCtrls;

type
  TFormMain = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    Timer1: TTimer;
    XPManifest1: TXPManifest;
    GroupBox1: TGroupBox;
    Image1: TImage;
    GroupBox2: TGroupBox;
    lbStatus: TLabel;
    GroupBox3: TGroupBox;
    DBGrid1: TDBGrid;
    Button4: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    Button2: TButton;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
var
ConnStr:string;
dbfile:string;
ncap,nRes:Byte;
begin
  ConnStr := 'Provider=%s;User ID=Admin;Data Source=%s;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path=""';
  dbfile:=ExtractFilePath(Application.ExeName)+'.\fpdb.mdb';
  if FileExists(dbfile) then
  begin
    if ADOConnection1.Connected then
    begin
      ADOConnection1.Close;
    end;
    ADOConnection1.ConnectionString := Format (ConnStr, ['Microsoft.Jet.OLEDB.4.0',dbfile]);
    ADOConnection1.Open;
    ADOTable1.Open;
  end;

  if fpengine.OpenDevice(0,0,0)<>1 then
  begin
    lbStatus.Caption:='Open Device Fail';
  end else
  begin
    if fpengine.LinkDevice=1 then
      lbStatus.Caption:='Open Device Succeed'
    else
      lbStatus.Caption:='Link Device Fail'
  end;
end;

procedure TFormMain.Button2Click(Sender: TObject);
begin
  if Length(Edit1.Text)=0 then
  begin
    ShowMessage('Please Input ID');
    Exit;
  end;
  fpengine.EnrolFpChar;

  Timer1.Enabled:=True;
end;

procedure TFormMain.Button3Click(Sender: TObject);
begin
  if ADOTable1.RecordCount<=0 then
  begin
    lbStatus.Caption:='No Record';
    Exit;
  end;
  fpengine.GenFpChar;

  Timer1.Enabled:=True;
end;

procedure TFormMain.Button4Click(Sender: TObject);
begin
  if ADOTable1.RecordCount>0 then
  begin
    if ADOTable1.State=dsEdit then
    begin
      ADOTable1.Post;
    end;
    ADOTable1.Delete;
  end;
end;

procedure TFormMain.DBGrid1CellClick(Column: TColumn);
begin
  edit1.Text:=AdoTable1.fieldbyname('ID').AsString;  
end;

procedure TFormMain.Timer1Timer(Sender: TObject);
var
  fs:TFileStream;
  reftp:array[0..511] of BYTE;
  matchtp:array[0..255] of BYTE;
  refsize:integer;
  matchsize:integer;
  fdb:TBlobStream;
  n:integer;
  fpsmsg,fpsret:Integer;
  bmp:TBitmap;
begin
  fpsmsg:=fpengine.GetWorkMsg;
  fpsret:=fpengine.GetRetMsg;
  case fpsmsg of
    FPM_DEVICE: begin lbStatus.Caption:='设备未打开'; end;
    FPM_PLACE:  begin lbStatus.Caption:='请按手指'; end;
    FPM_LIFT:   begin lbStatus.Caption:='请抬起手指'; end;
   FPM_NEWIMAGE:
    begin
      bmp:=TBitmap.Create;
      bmp.Width:=256;
      bmp.Height:=288;
      bmp.PixelFormat:=pf8bit;
      fpengine.DrawImage(bmp.Canvas.Handle,0,0);
      Image1.Picture.Assign(bmp);
      bmp.Free;
    end;
   FPM_GENCHAR:
		begin
      Timer1.Enabled:=False;
      if fpsret =0 then
      begin
        lbStatus.Caption:='获取指纹特诊点失败';
        Exit;
      end;
			lbStatus.Caption:='请等待,比对中...';
      fpengine.GetFpCharByGen(matchtp[0],matchsize);
      ADOTable1.First;
      for n:=1 to ADOTable1.RecordCount do
      begin
          fdb:=TBlobStream(ADOTable1.CreateBlobStream(TBlobField(ADOTable1.FieldByName('Template')),bmRead));
          fdb.Position:=0;
          refsize:=fdb.Size;
          fdb.Read(reftp[0],refsize);
          fdb.Free;
          if fpengine.MatchTemplateOne(matchtp[0],reftp[0],512)>=100 then
          begin
            lbStatus.Caption:=('识别成功:'+ADOTable1.FieldByName('ID').AsString);
            Exit;
          end;
          ADOTable1.Next;
      end;
      lbStatus.Caption:='识别失败'
    end;
   FPM_ENROLL:
    begin
      Timer1.Enabled:=False;
      if fpsret =0 then
      begin
        lbStatus.Caption:='登记指纹模板失败';
        Exit;
      end;
			lbStatus.Caption:='登记指纹模板成功';
      fpengine.GetFpCharByEnl(reftp[0],refsize);
      if ADOTable1.Locate('ID',Edit1.Text,[]) then
      begin
        ADOTable1.Edit;
      end else
      begin
        ADOTable1.Append;
        ADOTable1.FieldByName('ID').AsString:=Edit1.Text;
      end;
      fdb:=TBlobStream(ADOTable1.CreateBlobStream(TBlobField(ADOTable1.FieldByName('Template')),bmWrite));
      fdb.Position:=0;
      fdb.Write(reftp[0],refsize);
      fdb.Free;
      ADOTable1.Post;
    end;
	FPM_TIMEOUT:
    begin
		 lbStatus.Caption:='等待超时';
    end;
  end;
end;

procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  fpengine.CloseDevice;
end;

end.
