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 FingerCheck2 : Form { /// /// 记录当前登陆用户,如果当前用户为空,则控制操作需进行身份验证操作 /// public static user nowUser = null; #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 #region 用户名密码 #endregion private UserSvc userSvc; //指纹验证通过的用户 public static user checkSuccessUser = null; Bitmap fingerPic = null; private bool isNeedClose = false; private bool wrongUserbmp = false; public FingerCheck2() { InitializeComponent(); txtPassWord.LostFocus += new EventHandler(txtPassWord_LostFocus); txtPassWord.GotFocus += new EventHandler(txtPassWord_GotFocus); this.groupBox1.Hide(); this.cbUser.Hide(); this.txtPassWord.Hide(); this.btnLogin.Hide(); this.lblClose.BringToFront(); this.TopMost = true; } 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) { if (isNeedClose) { bIsTimeToDie = true; Thread.Sleep(2000); Console.WriteLine("已经获取到用户"); zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } } private void BindUserCheck() { using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { IList userList = new List(); user u = new user(); u.id = -1; u.name = "请选择用户"; userList.Add(u); userList = userList.Union(ctx.user.Select(s => s).ToList()).ToList(); this.cbUser.DataSource = userList; this.cbUser.DisplayMember = "name"; this.cbUser.ValueMember = "id"; this.cbUser.SelectedValue = -1; } } private void btnLogin_Click(object sender, EventArgs e) { string userIdString = cbUser.SelectedValue.ToString().Trim(); int userId = Convert.ToInt32(userIdString); string passWord = txtPassWord.Text.Trim(); if (userId == -1) { lblInfo.Text = "请选择用户"; return; } if (string.IsNullOrEmpty(passWord)) { return; } user userData = null; using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { userData = ctx.user.Where(s => s.id == userId).FirstOrDefault(); } if (userData == null) { lblInfo.Text = "用户名不存在"; return; } else { if (userData.pass_word == passWord) { checkSuccessUser = userData; bIsTimeToDie = true; zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } else { lblInfo.Text = "密码错误"; return; } } } private void btnPut_Click(object sender, EventArgs e) { if (btnPut.Text.Trim() == "密码验证") { picFinger.Hide(); groupBox1.Show(); cbUser.Show(); txtPassWord.Show(); btnLogin.Show(); btnPut.Text = "指纹验证"; lblInfo.Text = ""; BindUserCheck(); } else { picFinger.Show(); groupBox1.Hide(); cbUser.Hide(); txtPassWord.Hide(); btnLogin.Hide(); btnPut.Text = "密码验证"; lblInfo.Text = "请验证指纹"; } } private void txtPassWord_MouseClick(object sender, MouseEventArgs e) { string passWord = txtPassWord.Text.Trim(); if (passWord == "请输入密码") { txtPassWord.Text = ""; txtPassWord.PasswordChar = '*'; } } private void txtPassWord_GotFocus(object sender, EventArgs e) { string passWord = txtPassWord.Text.Trim(); if (passWord == "请输入密码") { txtPassWord.Text = ""; txtPassWord.PasswordChar = '*'; } } private void txtPassWord_LostFocus(object sender, EventArgs e) { string passWord = txtPassWord.Text.Trim(); if (string.IsNullOrEmpty(passWord)) { txtPassWord.Text = "请输入密码"; txtPassWord.PasswordChar = new char(); } } private void cbUser_SelectedIndexChanged(object sender, EventArgs e) { txtPassWord.Text = "请输入密码"; txtPassWord.PasswordChar = new char(); } } }