using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using EntityDataSet; using IntelligentControlForsx.Service.FingerSvc; using IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc; using libzkfpcsharp; namespace IntelligentControlForsx.ChildForms { public partial class FingerCheck : Form { #region 指纹读取常用量 bool bIsTimeToDie = false; IntPtr mDevHandle = IntPtr.Zero; IntPtr mDBHandle = IntPtr.Zero; IntPtr FormHandle = IntPtr.Zero; const int MESSAGE_CAPTURED_OK = 0x0400 + 6; byte[] FPBuffer;//返回图像 private byte[] fingerTemp = new byte[2048];//指纹模板 int fingerTempLength = 2048;//实际返回指纹模板长度 private int mfpWidth = 0;//图片宽度 private int mfpHeight = 0;//图片高度 #endregion private UserSvc userSvc; //指纹验证通过的用户 public static user checkSuccessUser = null; Bitmap fingerPic = null; private bool isNeedClose = false; private bool wrongUserbmp = false; public FingerCheck() { InitializeComponent(); } private void FingerCheck_Load(object sender, EventArgs e) { FormHandle = this.Handle; userSvc = UserSvc.GetUserSvc(); userSvc.InitUser(); FingerInit(); StartsScanning(); } private void DoCapture() { while (!bIsTimeToDie) { int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength); if (ret == zkfp.ZKFP_ERR_OK) { FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); } else { Thread.Sleep(1000); picFinger.Image = Image.FromFile(Application.StartupPath + @"\images\finger.gif"); } Thread.Sleep(200); } } private void FingerInit() { int ret = zkfperrdef.ZKFP_ERR_OK; if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK) { int nCount = zkfp2.GetDeviceCount(); if (nCount <= 0) { zkfp2.Terminate(); MessageBox.Show("找不到设备,请检查指纹刷是否连接"); } } else { MessageBox.Show("指纹输入设备初始化失败,请检查指纹刷是否连接"); } } /// /// 打开扫描 /// private void StartsScanning() { Console.WriteLine("设备初始化"); int ret = zkfp.ZKFP_ERR_OK; if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0))) { MessageBox.Show("打开扫描失败"); return; } if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit())) { MessageBox.Show("打开扫描失败"); zkfp2.CloseDevice(mDevHandle); mDevHandle = IntPtr.Zero; return; } byte[] paramValue = new byte[4]; int size = 4; zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size); zkfp2.ByteArray2Int(paramValue, ref mfpWidth); size = 4; zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size); zkfp2.ByteArray2Int(paramValue, ref mfpHeight); FPBuffer = new byte[mfpWidth * mfpHeight]; Thread captureThread = new Thread(new ThreadStart(DoCapture)); captureThread.IsBackground = true; captureThread.Start(); bIsTimeToDie = false; } protected override void DefWndProc(ref Message m) { switch (m.Msg) { case MESSAGE_CAPTURED_OK: { MemoryStream ms = new MemoryStream(); BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms); BitmapFormat.WriteBitmap(FPBuffer, mfpWidth, mfpHeight); Bitmap bmp = new Bitmap(ms); fingerPic = bmp; user user = UserSvc.GetUserSvc().LoginUserCheck(mDBHandle, fingerTemp); if (user != null) { checkSuccessUser = user; //将读取到的图片替换动图 isNeedClose = true; wrongUserbmp = false; Thread dataGridViewThread = new Thread(this.InfoRefresh); dataGridViewThread.Start(); } else { picFinger.Image = bmp; wrongUserbmp = true; lblInfo.Text = "未识别身份,请重新录入"; } } break; default: base.DefWndProc(ref m); break; } } private void lblClose_Click(object sender, EventArgs e) { checkSuccessUser = null; bIsTimeToDie = true; zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } private delegate void UpdateMessage(); private void InfoRefresh() { UpdateMessage del = new UpdateMessage(UpdateInfo); if (lblInfo.InvokeRequired) { lblInfo.BeginInvoke(del, new object[] { }); } } private void UpdateInfo() { picFinger.Image = fingerPic; lblInfo.Text = checkSuccessUser.name + ",您好!验证通过!"; zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); } private void timer1_Tick(object sender, EventArgs e) { Console.WriteLine("执行时间控件"); if (isNeedClose) { bIsTimeToDie = true; Thread.Sleep(2000); Console.WriteLine("已经获取到用户"); zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } } } }