//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ZKFPEngXControl_OCX"
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
  FID = 1;
  FFingerName = new TStringList();
  fpcHandle = ZKFPEngX1->CreateFPCacheDB();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnInitClick(TObject *Sender)
{
  if (ZKFPEngX1->InitEngine() == 0){
     MessageDlg("初始化Sensor成功！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
     ClientWidth = ZKFPEngX1->ImageWidth + pnlCommand->Width;
     ClientHeight = ZKFPEngX1->ImageHeight;
     btnInit->Enabled = False;

     StatusBar->Panels->Items[0]->Text = "指纹仪连接";
     EDSensorNum->Text = IntToStr(ZKFPEngX1->SensorCount);
     EDSensorIndex->Text = IntToStr(ZKFPEngX1->SensorIndex);
     EDSensorSN->Text = ZKFPEngX1->SensorSN;

     FMatchType = 0;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormDestroy(TObject *Sender)
{
  delete FFingerName;
  ZKFPEngX1->FreeFPCacheDB(fpcHandle);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ZKFPEngX1ImageReceived(TObject *Sender,
      TOLEBOOL *AImageValid)
{
  ZKFPEngX1->PrintImageAt((unsigned int)this->Canvas->Handle, 0, 0, (long)ZKFPEngX1->ImageWidth, (long)ZKFPEngX1->ImageHeight);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnSaveImageClick(TObject *Sender)
{
  WideString s;

  if (RGFormat->ItemIndex == 0){
     s = "c:\\Fingerprint.bmp";
     ZKFPEngX1->SaveBitmap(s);
  }else if (RGFormat->ItemIndex == 1){
     s = "c:\\Fingerprint.jpg";
     ZKFPEngX1->SaveJPG(s);
  }
  MessageDlg("文件保存成功C:\Fingerprint.xxx", mtInformation, TMsgDlgButtons()<<mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ZKFPEngX1FeatureInfo(TObject *Sender,
      long AQuality)
{
  AnsiString sTemp;

  sTemp = "";

  if (ZKFPEngX1->IsRegister)
     sTemp = "登记状态: 还需要按压" + IntToStr(ZKFPEngX1->EnrollIndex) + "次手指!";
  sTemp = sTemp + " 指纹质量";
  if (AQuality != 0)
     sTemp = sTemp + "不合格";
  else
     sTemp = sTemp + "合格";
  StatusBar->Panels->Items[0]->Text = sTemp;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnRegClick(TObject *Sender)
{
  if (EDFingerName->Text == ""){
     MessageDlg("请输入指纹名称标识!", mtInformation, TMsgDlgButtons()<<mbOK, 0);
     return;
  }
  ZKFPEngX1->BeginEnroll();
  StatusBar->Panels->Items[0]->Text = "开始登记";
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnVerifyClick(TObject *Sender)
{
  if (ZKFPEngX1->IsRegister)
     ZKFPEngX1->CancelEnroll();
  StatusBar->Panels->Items[0]->Text = "开始比对(1:1)";
  FMatchType = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
  if (ZKFPEngX1->IsRegister)
     ZKFPEngX1->CancelEnroll();
  StatusBar->Panels->Items[0]->Text = "开始比对(1:N)";
  FMatchType = 2;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ZKFPEngX1Capture(TObject *Sender,
      TOLEBOOL ActionResult, TVariantInParam ATemplate)
{
  int ID, I;
  long Score, ProcessNum;
  AnsiString s;
  WideString s1;
  TVariantInParam p;
  TOLEBOOL RegChanged;

  p = (TVariantInParam)ZKFPEngX1->GetTemplate();

  StatusBar->Panels->Items[0]->Text = "取得指纹特征";
  if (FMatchType == 1){ //1:1
     if (ZKFPEngX1->VerFinger(FRegTemplate, p, False, &RegChanged))
        MessageDlg("指纹比对成功！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
     else
        MessageDlg("指纹比对失败！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
    s1 = "c:\\fingerprint.tpl";
    if (ZKFPEngX1->VerRegFingerFile(s1.c_bstr(), p, False, &RegChanged))
        MessageDlg("文件指纹比对成功！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
    else
        MessageDlg("文件指纹比对失败！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
  }else if (FMatchType == 2){ //1:N
     Score = 8;
     ID = ZKFPEngX1->IdentificationInFPCacheDB(fpcHandle, p, &Score, &ProcessNum);
     if (ID == -1)
        MessageDlg("指纹比对失败！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
     else{
        for(I=0;I<FFingerName->Count;I++)
          if (ID == (int)(FFingerName->Objects[I])){
             s.sprintf("指纹比对成功！Name= %s Score = %d Processed Number = %d", FFingerName->Strings[I], Score, ProcessNum);
             MessageDlg(s, mtInformation, TMsgDlgButtons()<<mbOK, 0);
          }
     }
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ZKFPEngX1Enroll(TObject *Sender,
      TOLEBOOL ActionResult, TVariantInParam ATemplate)
{
  WideString s;
  TVariantInParam p;

  if (ActionResult){
     MessageDlg("指纹登记成功！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
     FRegTemplate = (Variant)ZKFPEngX1->GetTemplate();
     s = "c:\\fingerprint.tpl";
     p = (TVariantInParam)FRegTemplate;
     ZKFPEngX1->SaveTemplate(s, p);
     if (FID % 2 == 1){
        ZKFPEngX1->AddRegTemplateToFPCacheDB(fpcHandle, FID, p);
     }else
        ZKFPEngX1->AddRegTemplateFileToFPCacheDB(fpcHandle, FID, s);
     FFingerName->AddObject(EDFingerName->Text, (TObject *)FID);
     FID++;
  }else
     MessageDlg("指纹登记失败！", mtInformation, TMsgDlgButtons()<<mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnDogReadClick(TObject *Sender)
{
  long p1, p2;
  TVariant v;
  Variant p;
  char s[25];
  void *ap;

  p1 = 0;
  p2 = 23;
  ZKFPEngX1->DongleMemRead(&p1, &p2, &v);
  p = Variant(v);
  ap = VarArrayLock(p);
  memcpy(s, ap, 24);
  VarArrayUnlock(p);
  s[23] = '\0';
  EDInfo1->Text = s;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::btnDogWriteClick(TObject *Sender)
{
  long p1, p2;
  Variant v;
  TVariant p;
  int i;
  char s[24];
  void *ap;

  p1 = 0;
  p2 = EDInfo->Text.Length();
  v = VarArrayCreate(OPENARRAY(int, (0, p2 - 1)), varByte);
  ap = VarArrayLock(v);
  strcpy(s, EDInfo->Text.c_str());
  memcpy(ap, s, p2);
  VarArrayUnlock(v);
  p = v;
  ZKFPEngX1->DongleMemWrite(&p1, &p2, &p);
}
//---------------------------------------------------------------------------
