FingerCheck.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using EntityDataSet;
  13. using IntelligentControlForsx.Service.FingerSvc;
  14. using IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc;
  15. using libzkfpcsharp;
  16. namespace IntelligentControlForsx.ChildForms
  17. {
  18. public partial class FingerCheck : Form
  19. {
  20. #region 指纹读取常用量
  21. bool bIsTimeToDie = false;
  22. IntPtr mDevHandle = IntPtr.Zero;
  23. IntPtr mDBHandle = IntPtr.Zero;
  24. IntPtr FormHandle = IntPtr.Zero;
  25. const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
  26. byte[] FPBuffer;//返回图像
  27. private byte[] fingerTemp = new byte[2048];//指纹模板
  28. int fingerTempLength = 2048;//实际返回指纹模板长度
  29. private int mfpWidth = 0;//图片宽度
  30. private int mfpHeight = 0;//图片高度
  31. #endregion
  32. private UserSvc userSvc;
  33. //指纹验证通过的用户
  34. public static user checkSuccessUser = null;
  35. Bitmap fingerPic = null;
  36. private bool isNeedClose = false;
  37. private bool wrongUserbmp = false;
  38. public FingerCheck()
  39. {
  40. InitializeComponent();
  41. }
  42. private void FingerCheck_Load(object sender, EventArgs e)
  43. {
  44. FormHandle = this.Handle;
  45. userSvc = UserSvc.GetUserSvc();
  46. userSvc.InitUser();
  47. FingerInit();
  48. StartsScanning();
  49. }
  50. private void DoCapture()
  51. {
  52. while (!bIsTimeToDie)
  53. {
  54. int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength);
  55. if (ret == zkfp.ZKFP_ERR_OK)
  56. {
  57. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  58. }
  59. else
  60. {
  61. Thread.Sleep(1000);
  62. picFinger.Image = Image.FromFile(Application.StartupPath + @"\images\finger.gif");
  63. }
  64. Thread.Sleep(200);
  65. }
  66. }
  67. private void FingerInit()
  68. {
  69. int ret = zkfperrdef.ZKFP_ERR_OK;
  70. if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
  71. {
  72. int nCount = zkfp2.GetDeviceCount();
  73. if (nCount <= 0)
  74. {
  75. zkfp2.Terminate();
  76. MessageBox.Show("找不到设备,请检查指纹刷是否连接");
  77. }
  78. }
  79. else
  80. {
  81. MessageBox.Show("指纹输入设备初始化失败,请检查指纹刷是否连接");
  82. }
  83. }
  84. /// <summary>
  85. /// 打开扫描
  86. /// </summary>
  87. private void StartsScanning()
  88. {
  89. Console.WriteLine("设备初始化");
  90. int ret = zkfp.ZKFP_ERR_OK;
  91. if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
  92. {
  93. MessageBox.Show("打开扫描失败");
  94. return;
  95. }
  96. if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
  97. {
  98. MessageBox.Show("打开扫描失败");
  99. zkfp2.CloseDevice(mDevHandle);
  100. mDevHandle = IntPtr.Zero;
  101. return;
  102. }
  103. byte[] paramValue = new byte[4];
  104. int size = 4;
  105. zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
  106. zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
  107. size = 4;
  108. zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
  109. zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
  110. FPBuffer = new byte[mfpWidth * mfpHeight];
  111. Thread captureThread = new Thread(new ThreadStart(DoCapture));
  112. captureThread.IsBackground = true;
  113. captureThread.Start();
  114. bIsTimeToDie = false;
  115. }
  116. protected override void DefWndProc(ref Message m)
  117. {
  118. switch (m.Msg)
  119. {
  120. case MESSAGE_CAPTURED_OK:
  121. {
  122. MemoryStream ms = new MemoryStream();
  123. BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
  124. BitmapFormat.WriteBitmap(FPBuffer, mfpWidth, mfpHeight);
  125. Bitmap bmp = new Bitmap(ms);
  126. fingerPic = bmp;
  127. user user = UserSvc.GetUserSvc().LoginUserCheck(mDBHandle, fingerTemp);
  128. if (user != null)
  129. {
  130. checkSuccessUser = user;
  131. //将读取到的图片替换动图
  132. isNeedClose = true;
  133. wrongUserbmp = false;
  134. Thread dataGridViewThread = new Thread(this.InfoRefresh);
  135. dataGridViewThread.Start();
  136. }
  137. else
  138. {
  139. picFinger.Image = bmp;
  140. wrongUserbmp = true;
  141. lblInfo.Text = "未识别身份,请重新录入";
  142. }
  143. }
  144. break;
  145. default:
  146. base.DefWndProc(ref m);
  147. break;
  148. }
  149. }
  150. private void lblClose_Click(object sender, EventArgs e)
  151. {
  152. checkSuccessUser = null;
  153. bIsTimeToDie = true;
  154. zkfp2.Terminate();
  155. zkfp2.CloseDevice(mDevHandle);
  156. zkfp2.CloseDevice(mDBHandle);
  157. this.Dispose();
  158. }
  159. private delegate void UpdateMessage();
  160. private void InfoRefresh()
  161. {
  162. UpdateMessage del = new UpdateMessage(UpdateInfo);
  163. if (lblInfo.InvokeRequired)
  164. {
  165. lblInfo.BeginInvoke(del, new object[] { });
  166. }
  167. }
  168. private void UpdateInfo()
  169. {
  170. picFinger.Image = fingerPic;
  171. lblInfo.Text = checkSuccessUser.name + ",您好!验证通过!";
  172. zkfp2.Terminate();
  173. zkfp2.CloseDevice(mDevHandle);
  174. zkfp2.CloseDevice(mDBHandle);
  175. }
  176. private void timer1_Tick(object sender, EventArgs e)
  177. {
  178. Console.WriteLine("执行时间控件");
  179. if (isNeedClose)
  180. {
  181. bIsTimeToDie = true;
  182. Thread.Sleep(2000);
  183. Console.WriteLine("已经获取到用户");
  184. zkfp2.Terminate();
  185. zkfp2.CloseDevice(mDevHandle);
  186. zkfp2.CloseDevice(mDBHandle);
  187. this.Dispose();
  188. }
  189. }
  190. }
  191. }