diff --git a/ICS/App.config b/ICS/App.config index 6e91854..53c59c2 100644 --- a/ICS/App.config +++ b/ICS/App.config @@ -9,12 +9,6 @@ - - - - - - diff --git a/ICS/App.config b/ICS/App.config index 6e91854..53c59c2 100644 --- a/ICS/App.config +++ b/ICS/App.config @@ -9,12 +9,6 @@ - - - - - - diff --git a/ICS/FingerPrintForm.cs b/ICS/FingerPrintForm.cs index b16a64e..8bc4231 100644 --- a/ICS/FingerPrintForm.cs +++ b/ICS/FingerPrintForm.cs @@ -29,6 +29,7 @@ // 采集线程工作标志位 bool captureWorking = false; + bool threadExit = false; // 小窗口显示指纹图像 byte[] fpImgBuffer; @@ -36,14 +37,16 @@ int RegisterCount = 0; // 需要采集测次数 const int REGISTER_ROUND = 3; + // 当前DB中保存的指纹ID + int fingerId = 1; // 每次采集的指纹模板数据 - byte[][] RegTmps; + byte[][] regTmps; // 合并后的指纹模板数据 - byte[] RegTmp; + byte[] regTmp; // 单次采集的指纹模板数据 - byte[] CapTmp; + byte[] capTmp; // 单次采集的指纹模板数据长度 最长为2048个字节 int cbCapTmp = 0; @@ -62,6 +65,8 @@ public static log4net.ILog log = log4net.LogManager.GetLogger("ICS"); public bool CaptureWorking { get => captureWorking; set => captureWorking = value; } + public bool ThreadExit { get => threadExit; set => threadExit = value; } + public int FingerId { get => fingerId; set => fingerId = value; } [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); @@ -93,10 +98,7 @@ return -2; } else { - captureThread = new Thread(new ThreadStart(DoCapture)); - captureThread.IsBackground = true; - captureThread.Start(); - + fingerId = 1; return 0; } } @@ -109,8 +111,12 @@ public void CloseFingerDevice(int index) { + // 关闭设备 if (IntPtr.Zero!= fingerPrintDevHandle) { + zkfp2.DBFree(dbHandle); + dbHandle = IntPtr.Zero; + zkfp2.CloseDevice(fingerPrintDevHandle); fingerPrintDevHandle= IntPtr.Zero; } @@ -121,8 +127,20 @@ /// /// public void StartCaptureFingerPrint() - { + if (IntPtr.Zero == fingerPrintDevHandle) + { + this.labelTips.Text = "设备还未初始化"; + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "success", "false" }, + { "message", "device not initialized" } + }; + Form1.SendMsgToClient("CapFingerPrint", JsonConvert.SerializeObject(obj)); + return; + } + this.labelTips.Text = "请用同一个手指按压3次"; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FingerPrintForm)); this.picFingerPrint.Image = ((System.Drawing.Image)(resources.GetObject("picFingerPrint.Image"))); @@ -138,29 +156,46 @@ // 生成指纹图像字节数组 fpImgBuffer = new byte[fpWidth * fpHeight]; - RegTmps = new byte[REGISTER_ROUND][]; + regTmps = new byte[REGISTER_ROUND][]; for (int i = 0; i < REGISTER_ROUND; i++) { - RegTmps[i] = new byte[2048]; + regTmps[i] = new byte[2048]; } - RegTmp = new byte[2048]; - CapTmp = new byte[2048]; + regTmp = new byte[2048]; + capTmp = new byte[2048]; this.RegisterCount = 0; + // 开启采集线程 + captureThread = new Thread(new ThreadStart(DoCapture)); + ThreadExit = false; + captureThread.IsBackground = true; + captureThread.Start(); + // 采集工作线程标志位 CaptureWorking = true; } + public void StopCaptureFingerPrint() + { + // 停止工作 + CaptureWorking = false; + + // 停止线程 + ThreadExit = true; + + this.Hide(); + } + /// /// 执行采集指纹动作 /// private void DoCapture() { - while (true) + while (ThreadExit == false) { if (CaptureWorking == true) { - int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, CapTmp, ref cbCapTmp); + int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, capTmp, ref cbCapTmp); if (ret == zkfp.ZKFP_ERR_OK) { SendMessage(formHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); @@ -181,22 +216,44 @@ Bitmap bmp = new Bitmap(ms); this.picFingerPrint.Image = bmp; - JObject obj = new JObject(); - obj.Add("command", "CaptureFingerPrint"); - obj.Add("image", "data:image/bmp;base64," + Form1.ToBase64(bmp)); + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "image", "data:image/bmp;base64," + Form1.ToBase64(bmp) } + }; + // 第一次采集 if (RegisterCount == 0) { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); - RegisterCount++; + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); - obj.Add("success", "true"); - obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 判断是否在数据库中有重复的指纹 + // 仅在第一次采集时判断重复 后续会调用DBMatch判断是否与第一次采集的指纹为同一手指 + int fid = 0; + int score = 0; + int identifyRet = zkfp2.DBIdentify(dbHandle, capTmp, ref fid, ref score); + if (identifyRet == 0 && fid >= 0) + { + // 找到本次采集已经保存的指纹 + obj.Add("success", "false"); + obj.Add("message", "fingerPrint found"); - this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; - } else + this.labelTips.Text = "数据库中找到相同的手指" + fid + "\r\n请用不同手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + else + { + // 数据库中没有找到 进行下一次采集 + RegisterCount++; + + obj.Add("success", "true"); + obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + + this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + } + else { - int matchRet = zkfp2.DBMatch(dbHandle, CapTmp, RegTmps[RegisterCount - 1]); + int matchRet = zkfp2.DBMatch(dbHandle, capTmp, regTmps[RegisterCount - 1]); if (matchRet <= 0) { // 本次采集的指纹与上次采集的指纹不是同一个手指 提示 @@ -207,25 +264,32 @@ this.labelTips.Text = "与上一次的手指不同\r\n请用同一手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; } else { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); RegisterCount++; obj.Add("success", "true"); obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 一枚指纹的3次采集结束 if (RegisterCount >= REGISTER_ROUND) { - int mergeRet = zkfp2.DBMerge(dbHandle, RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref cbRegTmp); + // 合并3次指纹 + int mergeRet = zkfp2.DBMerge(dbHandle, regTmps[0], regTmps[1], regTmps[2], regTmp, ref cbRegTmp); if (mergeRet == zkfperrdef.ZKFP_ERR_OK) { - obj.Add("tempLen", cbRegTmp); - obj.Add("tempData", zkfp2.BlobToBase64(RegTmp, cbRegTmp)); + // 采集成功后将指纹保存到数据库中 + int addRet = zkfp2.DBAdd(dbHandle, FingerId, regTmp); + if (addRet == 0) + { + FingerId++; + obj.Add("tempLen", cbRegTmp); + obj.Add("tempData", zkfp2.BlobToBase64(regTmp, cbRegTmp)); + } + + this.labelTips.Text = "指纹采集成功"; log.Info("指纹采集成功"); - this.Hide(); - CaptureWorking = false; - - Form1.FinalizeFingerPrint(); + StopCaptureFingerPrint(); } else { diff --git a/ICS/App.config b/ICS/App.config index 6e91854..53c59c2 100644 --- a/ICS/App.config +++ b/ICS/App.config @@ -9,12 +9,6 @@ - - - - - - diff --git a/ICS/FingerPrintForm.cs b/ICS/FingerPrintForm.cs index b16a64e..8bc4231 100644 --- a/ICS/FingerPrintForm.cs +++ b/ICS/FingerPrintForm.cs @@ -29,6 +29,7 @@ // 采集线程工作标志位 bool captureWorking = false; + bool threadExit = false; // 小窗口显示指纹图像 byte[] fpImgBuffer; @@ -36,14 +37,16 @@ int RegisterCount = 0; // 需要采集测次数 const int REGISTER_ROUND = 3; + // 当前DB中保存的指纹ID + int fingerId = 1; // 每次采集的指纹模板数据 - byte[][] RegTmps; + byte[][] regTmps; // 合并后的指纹模板数据 - byte[] RegTmp; + byte[] regTmp; // 单次采集的指纹模板数据 - byte[] CapTmp; + byte[] capTmp; // 单次采集的指纹模板数据长度 最长为2048个字节 int cbCapTmp = 0; @@ -62,6 +65,8 @@ public static log4net.ILog log = log4net.LogManager.GetLogger("ICS"); public bool CaptureWorking { get => captureWorking; set => captureWorking = value; } + public bool ThreadExit { get => threadExit; set => threadExit = value; } + public int FingerId { get => fingerId; set => fingerId = value; } [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); @@ -93,10 +98,7 @@ return -2; } else { - captureThread = new Thread(new ThreadStart(DoCapture)); - captureThread.IsBackground = true; - captureThread.Start(); - + fingerId = 1; return 0; } } @@ -109,8 +111,12 @@ public void CloseFingerDevice(int index) { + // 关闭设备 if (IntPtr.Zero!= fingerPrintDevHandle) { + zkfp2.DBFree(dbHandle); + dbHandle = IntPtr.Zero; + zkfp2.CloseDevice(fingerPrintDevHandle); fingerPrintDevHandle= IntPtr.Zero; } @@ -121,8 +127,20 @@ /// /// public void StartCaptureFingerPrint() - { + if (IntPtr.Zero == fingerPrintDevHandle) + { + this.labelTips.Text = "设备还未初始化"; + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "success", "false" }, + { "message", "device not initialized" } + }; + Form1.SendMsgToClient("CapFingerPrint", JsonConvert.SerializeObject(obj)); + return; + } + this.labelTips.Text = "请用同一个手指按压3次"; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FingerPrintForm)); this.picFingerPrint.Image = ((System.Drawing.Image)(resources.GetObject("picFingerPrint.Image"))); @@ -138,29 +156,46 @@ // 生成指纹图像字节数组 fpImgBuffer = new byte[fpWidth * fpHeight]; - RegTmps = new byte[REGISTER_ROUND][]; + regTmps = new byte[REGISTER_ROUND][]; for (int i = 0; i < REGISTER_ROUND; i++) { - RegTmps[i] = new byte[2048]; + regTmps[i] = new byte[2048]; } - RegTmp = new byte[2048]; - CapTmp = new byte[2048]; + regTmp = new byte[2048]; + capTmp = new byte[2048]; this.RegisterCount = 0; + // 开启采集线程 + captureThread = new Thread(new ThreadStart(DoCapture)); + ThreadExit = false; + captureThread.IsBackground = true; + captureThread.Start(); + // 采集工作线程标志位 CaptureWorking = true; } + public void StopCaptureFingerPrint() + { + // 停止工作 + CaptureWorking = false; + + // 停止线程 + ThreadExit = true; + + this.Hide(); + } + /// /// 执行采集指纹动作 /// private void DoCapture() { - while (true) + while (ThreadExit == false) { if (CaptureWorking == true) { - int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, CapTmp, ref cbCapTmp); + int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, capTmp, ref cbCapTmp); if (ret == zkfp.ZKFP_ERR_OK) { SendMessage(formHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); @@ -181,22 +216,44 @@ Bitmap bmp = new Bitmap(ms); this.picFingerPrint.Image = bmp; - JObject obj = new JObject(); - obj.Add("command", "CaptureFingerPrint"); - obj.Add("image", "data:image/bmp;base64," + Form1.ToBase64(bmp)); + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "image", "data:image/bmp;base64," + Form1.ToBase64(bmp) } + }; + // 第一次采集 if (RegisterCount == 0) { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); - RegisterCount++; + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); - obj.Add("success", "true"); - obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 判断是否在数据库中有重复的指纹 + // 仅在第一次采集时判断重复 后续会调用DBMatch判断是否与第一次采集的指纹为同一手指 + int fid = 0; + int score = 0; + int identifyRet = zkfp2.DBIdentify(dbHandle, capTmp, ref fid, ref score); + if (identifyRet == 0 && fid >= 0) + { + // 找到本次采集已经保存的指纹 + obj.Add("success", "false"); + obj.Add("message", "fingerPrint found"); - this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; - } else + this.labelTips.Text = "数据库中找到相同的手指" + fid + "\r\n请用不同手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + else + { + // 数据库中没有找到 进行下一次采集 + RegisterCount++; + + obj.Add("success", "true"); + obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + + this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + } + else { - int matchRet = zkfp2.DBMatch(dbHandle, CapTmp, RegTmps[RegisterCount - 1]); + int matchRet = zkfp2.DBMatch(dbHandle, capTmp, regTmps[RegisterCount - 1]); if (matchRet <= 0) { // 本次采集的指纹与上次采集的指纹不是同一个手指 提示 @@ -207,25 +264,32 @@ this.labelTips.Text = "与上一次的手指不同\r\n请用同一手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; } else { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); RegisterCount++; obj.Add("success", "true"); obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 一枚指纹的3次采集结束 if (RegisterCount >= REGISTER_ROUND) { - int mergeRet = zkfp2.DBMerge(dbHandle, RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref cbRegTmp); + // 合并3次指纹 + int mergeRet = zkfp2.DBMerge(dbHandle, regTmps[0], regTmps[1], regTmps[2], regTmp, ref cbRegTmp); if (mergeRet == zkfperrdef.ZKFP_ERR_OK) { - obj.Add("tempLen", cbRegTmp); - obj.Add("tempData", zkfp2.BlobToBase64(RegTmp, cbRegTmp)); + // 采集成功后将指纹保存到数据库中 + int addRet = zkfp2.DBAdd(dbHandle, FingerId, regTmp); + if (addRet == 0) + { + FingerId++; + obj.Add("tempLen", cbRegTmp); + obj.Add("tempData", zkfp2.BlobToBase64(regTmp, cbRegTmp)); + } + + this.labelTips.Text = "指纹采集成功"; log.Info("指纹采集成功"); - this.Hide(); - CaptureWorking = false; - - Form1.FinalizeFingerPrint(); + StopCaptureFingerPrint(); } else { diff --git a/ICS/Form1.Designer.cs b/ICS/Form1.Designer.cs index 14a17e4..4326772 100644 --- a/ICS/Form1.Designer.cs +++ b/ICS/Form1.Designer.cs @@ -107,7 +107,7 @@ this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(47, 17); this.label2.TabIndex = 2; - this.label2.Text = " V1.3.0"; + this.label2.Text = " V1.3.1"; // // pictureBox1 // @@ -188,6 +188,7 @@ this.axIrisDevCtrl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axIrisDevCtrl1.OcxState"))); this.axIrisDevCtrl1.Size = new System.Drawing.Size(633, 291); this.axIrisDevCtrl1.TabIndex = 10; + this.axIrisDevCtrl1.OnCapture += new AxIrisDevCtrlLib._DIrisDevCtrlEvents_OnCaptureEventHandler(this.axIrisDevCtrl1_OnCapture); // // Form1 // @@ -226,11 +227,11 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private DevComponents.DotNetBar.Wizard wizard1; + // private DevComponents.DotNetBar.Wizard wizard1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label_initDev; private System.Windows.Forms.Label label_openCamera; - private IrisCtrl.IrisColCtrl irisColCtrl1; + IrisCtrl.IrisColCtrl irisColCtrl1; private AxIrisDevCtrlLib.AxIrisDevCtrl axIrisDevCtrl1; } } diff --git a/ICS/App.config b/ICS/App.config index 6e91854..53c59c2 100644 --- a/ICS/App.config +++ b/ICS/App.config @@ -9,12 +9,6 @@ - - - - - - diff --git a/ICS/FingerPrintForm.cs b/ICS/FingerPrintForm.cs index b16a64e..8bc4231 100644 --- a/ICS/FingerPrintForm.cs +++ b/ICS/FingerPrintForm.cs @@ -29,6 +29,7 @@ // 采集线程工作标志位 bool captureWorking = false; + bool threadExit = false; // 小窗口显示指纹图像 byte[] fpImgBuffer; @@ -36,14 +37,16 @@ int RegisterCount = 0; // 需要采集测次数 const int REGISTER_ROUND = 3; + // 当前DB中保存的指纹ID + int fingerId = 1; // 每次采集的指纹模板数据 - byte[][] RegTmps; + byte[][] regTmps; // 合并后的指纹模板数据 - byte[] RegTmp; + byte[] regTmp; // 单次采集的指纹模板数据 - byte[] CapTmp; + byte[] capTmp; // 单次采集的指纹模板数据长度 最长为2048个字节 int cbCapTmp = 0; @@ -62,6 +65,8 @@ public static log4net.ILog log = log4net.LogManager.GetLogger("ICS"); public bool CaptureWorking { get => captureWorking; set => captureWorking = value; } + public bool ThreadExit { get => threadExit; set => threadExit = value; } + public int FingerId { get => fingerId; set => fingerId = value; } [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); @@ -93,10 +98,7 @@ return -2; } else { - captureThread = new Thread(new ThreadStart(DoCapture)); - captureThread.IsBackground = true; - captureThread.Start(); - + fingerId = 1; return 0; } } @@ -109,8 +111,12 @@ public void CloseFingerDevice(int index) { + // 关闭设备 if (IntPtr.Zero!= fingerPrintDevHandle) { + zkfp2.DBFree(dbHandle); + dbHandle = IntPtr.Zero; + zkfp2.CloseDevice(fingerPrintDevHandle); fingerPrintDevHandle= IntPtr.Zero; } @@ -121,8 +127,20 @@ /// /// public void StartCaptureFingerPrint() - { + if (IntPtr.Zero == fingerPrintDevHandle) + { + this.labelTips.Text = "设备还未初始化"; + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "success", "false" }, + { "message", "device not initialized" } + }; + Form1.SendMsgToClient("CapFingerPrint", JsonConvert.SerializeObject(obj)); + return; + } + this.labelTips.Text = "请用同一个手指按压3次"; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FingerPrintForm)); this.picFingerPrint.Image = ((System.Drawing.Image)(resources.GetObject("picFingerPrint.Image"))); @@ -138,29 +156,46 @@ // 生成指纹图像字节数组 fpImgBuffer = new byte[fpWidth * fpHeight]; - RegTmps = new byte[REGISTER_ROUND][]; + regTmps = new byte[REGISTER_ROUND][]; for (int i = 0; i < REGISTER_ROUND; i++) { - RegTmps[i] = new byte[2048]; + regTmps[i] = new byte[2048]; } - RegTmp = new byte[2048]; - CapTmp = new byte[2048]; + regTmp = new byte[2048]; + capTmp = new byte[2048]; this.RegisterCount = 0; + // 开启采集线程 + captureThread = new Thread(new ThreadStart(DoCapture)); + ThreadExit = false; + captureThread.IsBackground = true; + captureThread.Start(); + // 采集工作线程标志位 CaptureWorking = true; } + public void StopCaptureFingerPrint() + { + // 停止工作 + CaptureWorking = false; + + // 停止线程 + ThreadExit = true; + + this.Hide(); + } + /// /// 执行采集指纹动作 /// private void DoCapture() { - while (true) + while (ThreadExit == false) { if (CaptureWorking == true) { - int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, CapTmp, ref cbCapTmp); + int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, capTmp, ref cbCapTmp); if (ret == zkfp.ZKFP_ERR_OK) { SendMessage(formHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); @@ -181,22 +216,44 @@ Bitmap bmp = new Bitmap(ms); this.picFingerPrint.Image = bmp; - JObject obj = new JObject(); - obj.Add("command", "CaptureFingerPrint"); - obj.Add("image", "data:image/bmp;base64," + Form1.ToBase64(bmp)); + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "image", "data:image/bmp;base64," + Form1.ToBase64(bmp) } + }; + // 第一次采集 if (RegisterCount == 0) { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); - RegisterCount++; + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); - obj.Add("success", "true"); - obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 判断是否在数据库中有重复的指纹 + // 仅在第一次采集时判断重复 后续会调用DBMatch判断是否与第一次采集的指纹为同一手指 + int fid = 0; + int score = 0; + int identifyRet = zkfp2.DBIdentify(dbHandle, capTmp, ref fid, ref score); + if (identifyRet == 0 && fid >= 0) + { + // 找到本次采集已经保存的指纹 + obj.Add("success", "false"); + obj.Add("message", "fingerPrint found"); - this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; - } else + this.labelTips.Text = "数据库中找到相同的手指" + fid + "\r\n请用不同手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + else + { + // 数据库中没有找到 进行下一次采集 + RegisterCount++; + + obj.Add("success", "true"); + obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + + this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + } + else { - int matchRet = zkfp2.DBMatch(dbHandle, CapTmp, RegTmps[RegisterCount - 1]); + int matchRet = zkfp2.DBMatch(dbHandle, capTmp, regTmps[RegisterCount - 1]); if (matchRet <= 0) { // 本次采集的指纹与上次采集的指纹不是同一个手指 提示 @@ -207,25 +264,32 @@ this.labelTips.Text = "与上一次的手指不同\r\n请用同一手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; } else { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); RegisterCount++; obj.Add("success", "true"); obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 一枚指纹的3次采集结束 if (RegisterCount >= REGISTER_ROUND) { - int mergeRet = zkfp2.DBMerge(dbHandle, RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref cbRegTmp); + // 合并3次指纹 + int mergeRet = zkfp2.DBMerge(dbHandle, regTmps[0], regTmps[1], regTmps[2], regTmp, ref cbRegTmp); if (mergeRet == zkfperrdef.ZKFP_ERR_OK) { - obj.Add("tempLen", cbRegTmp); - obj.Add("tempData", zkfp2.BlobToBase64(RegTmp, cbRegTmp)); + // 采集成功后将指纹保存到数据库中 + int addRet = zkfp2.DBAdd(dbHandle, FingerId, regTmp); + if (addRet == 0) + { + FingerId++; + obj.Add("tempLen", cbRegTmp); + obj.Add("tempData", zkfp2.BlobToBase64(regTmp, cbRegTmp)); + } + + this.labelTips.Text = "指纹采集成功"; log.Info("指纹采集成功"); - this.Hide(); - CaptureWorking = false; - - Form1.FinalizeFingerPrint(); + StopCaptureFingerPrint(); } else { diff --git a/ICS/Form1.Designer.cs b/ICS/Form1.Designer.cs index 14a17e4..4326772 100644 --- a/ICS/Form1.Designer.cs +++ b/ICS/Form1.Designer.cs @@ -107,7 +107,7 @@ this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(47, 17); this.label2.TabIndex = 2; - this.label2.Text = " V1.3.0"; + this.label2.Text = " V1.3.1"; // // pictureBox1 // @@ -188,6 +188,7 @@ this.axIrisDevCtrl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axIrisDevCtrl1.OcxState"))); this.axIrisDevCtrl1.Size = new System.Drawing.Size(633, 291); this.axIrisDevCtrl1.TabIndex = 10; + this.axIrisDevCtrl1.OnCapture += new AxIrisDevCtrlLib._DIrisDevCtrlEvents_OnCaptureEventHandler(this.axIrisDevCtrl1_OnCapture); // // Form1 // @@ -226,11 +227,11 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private DevComponents.DotNetBar.Wizard wizard1; + // private DevComponents.DotNetBar.Wizard wizard1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label_initDev; private System.Windows.Forms.Label label_openCamera; - private IrisCtrl.IrisColCtrl irisColCtrl1; + IrisCtrl.IrisColCtrl irisColCtrl1; private AxIrisDevCtrlLib.AxIrisDevCtrl axIrisDevCtrl1; } } diff --git a/ICS/Form1.cs b/ICS/Form1.cs index 0cf9929..4056855 100644 --- a/ICS/Form1.cs +++ b/ICS/Form1.cs @@ -18,6 +18,7 @@ using libzkfpcsharp; using SuperSocket.Common; using Sample; +using Newtonsoft.Json.Linq; namespace ICS { @@ -87,6 +88,7 @@ public int Init() { + Console.WriteLine(ConfigHelper.GetAppConfig("devType")); if (ConfigHelper.GetAppConfig("devType") == "1") { String light = "1A"; @@ -167,30 +169,61 @@ log.Info("指纹采集设备初始化成功[devCount=" + devCount + "]"); // 默认打开第一个设备 + fingerForm.ThreadExit = false; retCode = fingerForm.OpenFingerDevice(0); if (retCode == 0) { log.Info("指纹采集设备打开成功"); + + // 返回前端界面消息 + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "true" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } else { log.Error("指纹采集设备打开失败"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } else { zkfp2.Terminate(); log.Error("没有找到指纹采集设备"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } else { log.Error("初始化指纹采集设备异常[retCode=" + retCode + "]"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } public static void FinalizeFingerPrint() { + fingerForm.CloseFingerDevice(0); zkfp2.Terminate(); } @@ -424,19 +457,27 @@ SendMsgToClient(reValue, sendMsg); } } + // 开始采集 + else if (reValue.Contains("StartFingerPrint")) + { + // 初始化并打开设备 + // 初始化数据库 + this.InitFingerPrintDevice(); + } // 采集指纹 else if (reValue.Contains("CapFingerPrint")) { - this.InitFingerPrintDevice(); - - fingerForm.Show(); + // 开启采集线程 + // fingerForm.Show(); fingerForm.StartCaptureFingerPrint(); } + // 结束采集 else if (reValue.Contains("StopFingerPrint")) { - fingerForm.CaptureWorking = false; - fingerForm.Hide(); - fingerForm.CloseFingerDevice(0); + // 结束线程 + // 关闭设备 + // fingerForm.Hide(); + fingerForm.StopCaptureFingerPrint(); Form1.FinalizeFingerPrint(); } } @@ -451,7 +492,7 @@ { log.Info("与客户端:" + GetSessionName(session) + "的会话被关闭 原因:" + value.ToString()); var msg = string.Format("{0:HH:MM:ss} {1} 离开聊天室", DateTime.Now, GetSessionName(session)); - irisColCtrl1.Visible = false; + // irisColCtrl1.Visible = false; SendToAll(session, msg); } @@ -611,8 +652,6 @@ isInited = false; irisColCtrl1.CaptureEvent -= CaptureEvent; - - bool captureSuccess = true; if (eyeType == "3") { @@ -672,8 +711,6 @@ } - - private void axIrisDevCtrl1_OnCapture(object sender, AxIrisDevCtrlLib._DIrisDevCtrlEvents_OnCaptureEvent e) { string sendMsg = ""; @@ -761,7 +798,6 @@ } } - private void FindWindow() { while (true) diff --git a/ICS/App.config b/ICS/App.config index 6e91854..53c59c2 100644 --- a/ICS/App.config +++ b/ICS/App.config @@ -9,12 +9,6 @@ - - - - - - diff --git a/ICS/FingerPrintForm.cs b/ICS/FingerPrintForm.cs index b16a64e..8bc4231 100644 --- a/ICS/FingerPrintForm.cs +++ b/ICS/FingerPrintForm.cs @@ -29,6 +29,7 @@ // 采集线程工作标志位 bool captureWorking = false; + bool threadExit = false; // 小窗口显示指纹图像 byte[] fpImgBuffer; @@ -36,14 +37,16 @@ int RegisterCount = 0; // 需要采集测次数 const int REGISTER_ROUND = 3; + // 当前DB中保存的指纹ID + int fingerId = 1; // 每次采集的指纹模板数据 - byte[][] RegTmps; + byte[][] regTmps; // 合并后的指纹模板数据 - byte[] RegTmp; + byte[] regTmp; // 单次采集的指纹模板数据 - byte[] CapTmp; + byte[] capTmp; // 单次采集的指纹模板数据长度 最长为2048个字节 int cbCapTmp = 0; @@ -62,6 +65,8 @@ public static log4net.ILog log = log4net.LogManager.GetLogger("ICS"); public bool CaptureWorking { get => captureWorking; set => captureWorking = value; } + public bool ThreadExit { get => threadExit; set => threadExit = value; } + public int FingerId { get => fingerId; set => fingerId = value; } [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); @@ -93,10 +98,7 @@ return -2; } else { - captureThread = new Thread(new ThreadStart(DoCapture)); - captureThread.IsBackground = true; - captureThread.Start(); - + fingerId = 1; return 0; } } @@ -109,8 +111,12 @@ public void CloseFingerDevice(int index) { + // 关闭设备 if (IntPtr.Zero!= fingerPrintDevHandle) { + zkfp2.DBFree(dbHandle); + dbHandle = IntPtr.Zero; + zkfp2.CloseDevice(fingerPrintDevHandle); fingerPrintDevHandle= IntPtr.Zero; } @@ -121,8 +127,20 @@ /// /// public void StartCaptureFingerPrint() - { + if (IntPtr.Zero == fingerPrintDevHandle) + { + this.labelTips.Text = "设备还未初始化"; + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "success", "false" }, + { "message", "device not initialized" } + }; + Form1.SendMsgToClient("CapFingerPrint", JsonConvert.SerializeObject(obj)); + return; + } + this.labelTips.Text = "请用同一个手指按压3次"; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FingerPrintForm)); this.picFingerPrint.Image = ((System.Drawing.Image)(resources.GetObject("picFingerPrint.Image"))); @@ -138,29 +156,46 @@ // 生成指纹图像字节数组 fpImgBuffer = new byte[fpWidth * fpHeight]; - RegTmps = new byte[REGISTER_ROUND][]; + regTmps = new byte[REGISTER_ROUND][]; for (int i = 0; i < REGISTER_ROUND; i++) { - RegTmps[i] = new byte[2048]; + regTmps[i] = new byte[2048]; } - RegTmp = new byte[2048]; - CapTmp = new byte[2048]; + regTmp = new byte[2048]; + capTmp = new byte[2048]; this.RegisterCount = 0; + // 开启采集线程 + captureThread = new Thread(new ThreadStart(DoCapture)); + ThreadExit = false; + captureThread.IsBackground = true; + captureThread.Start(); + // 采集工作线程标志位 CaptureWorking = true; } + public void StopCaptureFingerPrint() + { + // 停止工作 + CaptureWorking = false; + + // 停止线程 + ThreadExit = true; + + this.Hide(); + } + /// /// 执行采集指纹动作 /// private void DoCapture() { - while (true) + while (ThreadExit == false) { if (CaptureWorking == true) { - int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, CapTmp, ref cbCapTmp); + int ret = zkfp2.AcquireFingerprint(fingerPrintDevHandle, fpImgBuffer, capTmp, ref cbCapTmp); if (ret == zkfp.ZKFP_ERR_OK) { SendMessage(formHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero); @@ -181,22 +216,44 @@ Bitmap bmp = new Bitmap(ms); this.picFingerPrint.Image = bmp; - JObject obj = new JObject(); - obj.Add("command", "CaptureFingerPrint"); - obj.Add("image", "data:image/bmp;base64," + Form1.ToBase64(bmp)); + JObject obj = new JObject + { + { "command", "CaptureFingerPrint" }, + { "image", "data:image/bmp;base64," + Form1.ToBase64(bmp) } + }; + // 第一次采集 if (RegisterCount == 0) { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); - RegisterCount++; + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); - obj.Add("success", "true"); - obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 判断是否在数据库中有重复的指纹 + // 仅在第一次采集时判断重复 后续会调用DBMatch判断是否与第一次采集的指纹为同一手指 + int fid = 0; + int score = 0; + int identifyRet = zkfp2.DBIdentify(dbHandle, capTmp, ref fid, ref score); + if (identifyRet == 0 && fid >= 0) + { + // 找到本次采集已经保存的指纹 + obj.Add("success", "false"); + obj.Add("message", "fingerPrint found"); - this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; - } else + this.labelTips.Text = "数据库中找到相同的手指" + fid + "\r\n请用不同手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + else + { + // 数据库中没有找到 进行下一次采集 + RegisterCount++; + + obj.Add("success", "true"); + obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + + this.labelTips.Text = "请再按压" + (REGISTER_ROUND - RegisterCount) + "次"; + } + } + else { - int matchRet = zkfp2.DBMatch(dbHandle, CapTmp, RegTmps[RegisterCount - 1]); + int matchRet = zkfp2.DBMatch(dbHandle, capTmp, regTmps[RegisterCount - 1]); if (matchRet <= 0) { // 本次采集的指纹与上次采集的指纹不是同一个手指 提示 @@ -207,25 +264,32 @@ this.labelTips.Text = "与上一次的手指不同\r\n请用同一手指再按压" + (REGISTER_ROUND - RegisterCount) + "次"; } else { - Array.Copy(CapTmp, RegTmps[RegisterCount], cbCapTmp); + Array.Copy(capTmp, regTmps[RegisterCount], cbCapTmp); RegisterCount++; obj.Add("success", "true"); obj.Add("pressLeft", REGISTER_ROUND - RegisterCount); + // 一枚指纹的3次采集结束 if (RegisterCount >= REGISTER_ROUND) { - int mergeRet = zkfp2.DBMerge(dbHandle, RegTmps[0], RegTmps[1], RegTmps[2], RegTmp, ref cbRegTmp); + // 合并3次指纹 + int mergeRet = zkfp2.DBMerge(dbHandle, regTmps[0], regTmps[1], regTmps[2], regTmp, ref cbRegTmp); if (mergeRet == zkfperrdef.ZKFP_ERR_OK) { - obj.Add("tempLen", cbRegTmp); - obj.Add("tempData", zkfp2.BlobToBase64(RegTmp, cbRegTmp)); + // 采集成功后将指纹保存到数据库中 + int addRet = zkfp2.DBAdd(dbHandle, FingerId, regTmp); + if (addRet == 0) + { + FingerId++; + obj.Add("tempLen", cbRegTmp); + obj.Add("tempData", zkfp2.BlobToBase64(regTmp, cbRegTmp)); + } + + this.labelTips.Text = "指纹采集成功"; log.Info("指纹采集成功"); - this.Hide(); - CaptureWorking = false; - - Form1.FinalizeFingerPrint(); + StopCaptureFingerPrint(); } else { diff --git a/ICS/Form1.Designer.cs b/ICS/Form1.Designer.cs index 14a17e4..4326772 100644 --- a/ICS/Form1.Designer.cs +++ b/ICS/Form1.Designer.cs @@ -107,7 +107,7 @@ this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(47, 17); this.label2.TabIndex = 2; - this.label2.Text = " V1.3.0"; + this.label2.Text = " V1.3.1"; // // pictureBox1 // @@ -188,6 +188,7 @@ this.axIrisDevCtrl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axIrisDevCtrl1.OcxState"))); this.axIrisDevCtrl1.Size = new System.Drawing.Size(633, 291); this.axIrisDevCtrl1.TabIndex = 10; + this.axIrisDevCtrl1.OnCapture += new AxIrisDevCtrlLib._DIrisDevCtrlEvents_OnCaptureEventHandler(this.axIrisDevCtrl1_OnCapture); // // Form1 // @@ -226,11 +227,11 @@ private System.Windows.Forms.Label label1; private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private DevComponents.DotNetBar.Wizard wizard1; + // private DevComponents.DotNetBar.Wizard wizard1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label_initDev; private System.Windows.Forms.Label label_openCamera; - private IrisCtrl.IrisColCtrl irisColCtrl1; + IrisCtrl.IrisColCtrl irisColCtrl1; private AxIrisDevCtrlLib.AxIrisDevCtrl axIrisDevCtrl1; } } diff --git a/ICS/Form1.cs b/ICS/Form1.cs index 0cf9929..4056855 100644 --- a/ICS/Form1.cs +++ b/ICS/Form1.cs @@ -18,6 +18,7 @@ using libzkfpcsharp; using SuperSocket.Common; using Sample; +using Newtonsoft.Json.Linq; namespace ICS { @@ -87,6 +88,7 @@ public int Init() { + Console.WriteLine(ConfigHelper.GetAppConfig("devType")); if (ConfigHelper.GetAppConfig("devType") == "1") { String light = "1A"; @@ -167,30 +169,61 @@ log.Info("指纹采集设备初始化成功[devCount=" + devCount + "]"); // 默认打开第一个设备 + fingerForm.ThreadExit = false; retCode = fingerForm.OpenFingerDevice(0); if (retCode == 0) { log.Info("指纹采集设备打开成功"); + + // 返回前端界面消息 + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "true" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } else { log.Error("指纹采集设备打开失败"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } else { zkfp2.Terminate(); log.Error("没有找到指纹采集设备"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } else { log.Error("初始化指纹采集设备异常[retCode=" + retCode + "]"); + + JObject obj = new JObject + { + { "command", "StartFingerPrint" }, + { "success", "false" } + }; + SendMsgToClient(reValue, JsonConvert.SerializeObject(obj)); } } public static void FinalizeFingerPrint() { + fingerForm.CloseFingerDevice(0); zkfp2.Terminate(); } @@ -424,19 +457,27 @@ SendMsgToClient(reValue, sendMsg); } } + // 开始采集 + else if (reValue.Contains("StartFingerPrint")) + { + // 初始化并打开设备 + // 初始化数据库 + this.InitFingerPrintDevice(); + } // 采集指纹 else if (reValue.Contains("CapFingerPrint")) { - this.InitFingerPrintDevice(); - - fingerForm.Show(); + // 开启采集线程 + // fingerForm.Show(); fingerForm.StartCaptureFingerPrint(); } + // 结束采集 else if (reValue.Contains("StopFingerPrint")) { - fingerForm.CaptureWorking = false; - fingerForm.Hide(); - fingerForm.CloseFingerDevice(0); + // 结束线程 + // 关闭设备 + // fingerForm.Hide(); + fingerForm.StopCaptureFingerPrint(); Form1.FinalizeFingerPrint(); } } @@ -451,7 +492,7 @@ { log.Info("与客户端:" + GetSessionName(session) + "的会话被关闭 原因:" + value.ToString()); var msg = string.Format("{0:HH:MM:ss} {1} 离开聊天室", DateTime.Now, GetSessionName(session)); - irisColCtrl1.Visible = false; + // irisColCtrl1.Visible = false; SendToAll(session, msg); } @@ -611,8 +652,6 @@ isInited = false; irisColCtrl1.CaptureEvent -= CaptureEvent; - - bool captureSuccess = true; if (eyeType == "3") { @@ -672,8 +711,6 @@ } - - private void axIrisDevCtrl1_OnCapture(object sender, AxIrisDevCtrlLib._DIrisDevCtrlEvents_OnCaptureEvent e) { string sendMsg = ""; @@ -761,7 +798,6 @@ } } - private void FindWindow() { while (true) diff --git a/ICS/ICS.csproj b/ICS/ICS.csproj index e7475d5..a7ee986 100644 --- a/ICS/ICS.csproj +++ b/ICS/ICS.csproj @@ -105,9 +105,24 @@ ..\irisCtrl\irisCtrl\CaptureEye.dll - + + + ..\irisCtrl\irisCtrl\Interop.IAiCamIrisLib.dll + True + + + False + True + bin\Debug\Interop.IrisDevCtrlLib.dll + + + False + ..\irisCtrl\irisCtrl\IrisCtrl.dll + ..\irisCtrl\irisCtrl\Emgu.CV.UI.dll @@ -117,19 +132,6 @@ ..\irisCtrl\irisCtrl\Emgu.CV.World.dll - - ..\irisCtrl\irisCtrl\Interop.IAiCamIrisLib.dll - True - - - False - True - bin\Debug\Interop.IrisDevCtrlLib.dll - - - False - ..\irisCtrl\irisCtrl\IrisCtrl.dll - ..\packages\libzkfpcsharp.dll