using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using EntityDataSet; using IntelligentControlForsx.Service.FingerSvc; using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd; using libzkfpcsharp; namespace IntelligentControlForsx.ChildForms { public partial class UserEdit : 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;//图片高度 byte[] finger1 = new byte[2048]; byte[] finger2 = new byte[2048]; byte[] finger3 = new byte[2048]; byte[] registResultFinger1 = new byte[2048];//最终结果模板1 byte[] registResultFinger2 = new byte[2048];//最终结果模板2 byte[] registResultFinger3 = new byte[2048];//最终结果模板3 byte[] registResultFinger4 = new byte[2048];//最终结果模板4 byte[] registResultFinger5 = new byte[2048];//最终结果模板5 byte[] registResultFinger6 = new byte[2048];//最终结果模板6 byte[] registResultFinger7 = new byte[2048];//最终结果模板7 byte[] registResultFinger8 = new byte[2048];//最终结果模板8 byte[] registResultFinger9 = new byte[2048];//最终结果模板9 byte[] registResultFinger10 = new byte[2048];//最终结果模板10 private bool isEditFinger1 = false; private bool isEditFinger2 = false; private bool isEditFinger3 = false; private bool isEditFinger4 = false; private bool isEditFinger5 = false; private bool isEditFinger6 = false; private bool isEditFinger7 = false; private bool isEditFinger8 = false; private bool isEditFinger9 = false; private bool isEditFinger10 = false; int iFid = 1;//指纹编号 private int pushFingerCount = 0; byte[] dataBaseTemp = new byte[2048]; #endregion private UserSvc userSvc; //获取路径 string homeStr = ""; private user editUser = null; private int fingerNum = -1; public UserEdit(user user) { InitializeComponent(); homeStr = Application.StartupPath; userSvc = UserSvc.GetUserSvc(); userSvc.InitUser(); FingerInit(); StartsScanning(); picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); editUser = user; } private void AddFinger_Load(object sender, EventArgs e) { FormHandle = this.Handle; IList list = new List(); CbEntity et = new CbEntity(); et.CbValue = "-1"; et.ShowName = "请选择"; list.Add(et); for (int i = 1; i <= 10; i++) { CbEntity entity = new CbEntity(); entity.ShowName = "指纹" + i; entity.CbValue = i.ToString(); list.Add(entity); } this.cbFinger.DataSource = list; this.cbFinger.DisplayMember = "showName"; this.cbFinger.ValueMember = "cbValue"; this.cbFinger.SelectedValue = "-1"; if (editUser != null) { txtUserName.Text = editUser.user_name; txtName.Text = editUser.name; txtTel.Text = editUser.telephone; txtPassWord.Text = editUser.pass_word; } } private void btnSubmit_Click(object sender, EventArgs e) { string name = txtName.Text.Trim(); string tel = txtTel.Text.Trim(); string userName = txtUserName.Text.Trim(); string passWord = txtPassWord.Text.Trim(); if (string.IsNullOrEmpty(tel)) { lblInfo.Text = "请输入电话"; return; } if (string.IsNullOrEmpty(passWord)) { lblInfo.Text = "请输入密码"; return; } if (editUser != null) { editUser.telephone = tel; editUser.pass_word = passWord; if (isEditFinger1) editUser.finger1_template = registResultFinger1; if (isEditFinger2) editUser.finger2_template = registResultFinger2; if (isEditFinger3) editUser.finger3_template = registResultFinger3; if (isEditFinger4) editUser.finger4_template = registResultFinger4; if (isEditFinger5) editUser.finger5_template = registResultFinger5; if (isEditFinger6) editUser.finger6_template = registResultFinger6; if (isEditFinger7) editUser.finger7_template = registResultFinger7; if (isEditFinger8) editUser.finger8_template = registResultFinger8; if (isEditFinger9) editUser.finger9_template = registResultFinger9; if (isEditFinger10) editUser.finger10_template = registResultFinger10; using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { ctx.Entry(editUser).State = EntityState.Modified; int count = ctx.SaveChanges(); if (count > 0) { MessageBox.Show("修改成功"); zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } else { MessageBox.Show("修改失败,请联系管理员"); } } } else { MessageBox.Show("获取用户失败,请关闭重新尝试"); } } /// /// 初始化指纹设备 /// 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; } /// /// 指纹扫描线程 /// private void DoCapture() { while (!bIsTimeToDie) { fingerTempLength = 2048; int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength); if (ret == zkfp.ZKFP_ERR_OK) { if (pushFingerCount == 0) { finger1 = (byte[])fingerTemp.Clone(); Console.WriteLine("手指1"); FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); } else if (pushFingerCount == 1) { finger2 = (byte[])fingerTemp.Clone(); Console.WriteLine("手指2"); FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); } else if (pushFingerCount == 2) { finger3 = (byte[])fingerTemp.Clone(); Console.WriteLine("手指3"); FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); } else { FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); } } Thread.Sleep(200); } } 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); int ret = zkfp.ZKFP_ERR_OK; int fid = 0, score = 0; if (pushFingerCount == 0) { ret = zkfp2.DBIdentify(mDBHandle, finger1, ref fid, ref score); pushFingerCount = 1; lblInfo.Text = "指纹第一次录入成功,请再次输入指纹"; this.picFinger1.Image = bmp; picFinger2.Image = Image.FromFile(homeStr + @"\images\finger.gif"); } else if (pushFingerCount == 1) { int compareScore = zkfp2.DBMatch(mDBHandle, finger1, finger2); if (compareScore <= 0) { lblInfo.Text = "请连续三次输入相同手指的指纹"; finger1 = new byte[2048]; finger2 = new byte[2048]; finger3 = new byte[2048]; pushFingerCount = 0; picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); } else { pushFingerCount = 2; lblInfo.Text = "指纹第二次录入成功,请再次输入指纹"; this.picFinger2.Image = bmp; picFinger3.Image = Image.FromFile(homeStr + @"\images\finger.gif"); } } else if (pushFingerCount == 2) { int compareScore = zkfp2.DBMatch(mDBHandle, finger3, finger2); if (compareScore <= 0) { lblInfo.Text = "请连续三次输入相同手指的指纹"; finger1 = new byte[2048]; finger2 = new byte[2048]; finger3 = new byte[2048]; pushFingerCount = 0; picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); } else { pushFingerCount = 3; lblInfo.Text = "指纹第三次录入成功"; this.picFinger3.Image = bmp; } } else { //txtInfo.Text = "未知错误"; } if (pushFingerCount == 3) { byte[] registResultFinger = null; switch (fingerNum) { case 1: registResultFinger = registResultFinger1; isEditFinger1 = true; break; case 2: registResultFinger = registResultFinger2; isEditFinger2 = true; break; case 3: registResultFinger = registResultFinger3; isEditFinger3 = true; break; case 4: registResultFinger = registResultFinger4; isEditFinger4 = true; break; case 5: registResultFinger = registResultFinger5; isEditFinger5 = true; break; case 6: registResultFinger = registResultFinger6; isEditFinger6 = true; break; case 7: registResultFinger = registResultFinger7; isEditFinger7 = true; break; case 8: registResultFinger = registResultFinger8; isEditFinger8 = true; break; case 9: registResultFinger = registResultFinger9; isEditFinger9 = true; break; case 10: registResultFinger = registResultFinger10; isEditFinger10 = true; break; default: break; } if (registResultFinger != null) { int ret1 = zkfp2.DBMerge(mDBHandle, finger1, finger2, finger3, registResultFinger, ref pushFingerCount); int ret2 = zkfp2.DBAdd(mDBHandle, iFid, registResultFinger); if (ret1 == zkfp.ZKFP_ERR_OK && ret2 == zkfp.ZKFP_ERR_OK) { btnSubmit.Enabled = true; } } } } break; default: base.DefWndProc(ref m); break; } } private void btnClose_Click(object sender, EventArgs e) { zkfp2.Terminate(); zkfp2.CloseDevice(mDevHandle); zkfp2.CloseDevice(mDBHandle); this.Dispose(); } private void txtName_TextChanged(object sender, EventArgs e) { string name = txtName.Text.Trim(); string tel = txtTel.Text.Trim(); if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrEmpty(tel)) { //FingerInit(); //StartsScanning(); //string homeStr = Application.StartupPath; //picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); } } private void txtTel_TextChanged(object sender, EventArgs e) { string name = txtName.Text.Trim(); string tel = txtTel.Text.Trim(); if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrEmpty(tel)) { //FingerInit(); //StartsScanning(); //string homeStr = Application.StartupPath; //picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); } } private void btnRest_Click(object sender, EventArgs e) { finger1 = new byte[2048]; finger2 = new byte[2048]; finger3 = new byte[2048]; registResultFinger1 = new byte[2048]; registResultFinger2 = new byte[2048]; registResultFinger3 = new byte[2048]; registResultFinger4 = new byte[2048]; registResultFinger5 = new byte[2048]; registResultFinger6 = new byte[2048]; registResultFinger7 = new byte[2048]; registResultFinger8 = new byte[2048]; registResultFinger9 = new byte[2048]; registResultFinger10 = new byte[2048]; pushFingerCount = 0; picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif"); picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png"); } private void cbFinger_SelectedIndexChanged(object sender, EventArgs e) { string fingerNumString = cbFinger.SelectedValue.ToString().Trim(); if (!fingerNumString.Contains("CbEntity")) { fingerNum = Convert.ToInt32(fingerNumString); finger1 = new byte[2048]; finger2 = new byte[2048]; finger3 = new byte[2048]; pushFingerCount = 0; } } } public class CbEntity { public string ShowName { set; get; } public string CbValue { set; get; } } }