FingerCheck2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 FingerCheck2 : Form
  19. {
  20. /// <summary>
  21. /// 记录当前登陆用户,如果当前用户为空,则控制操作需进行身份验证操作
  22. /// </summary>
  23. public static user nowUser = null;
  24. #region 指纹读取常用量
  25. bool bIsTimeToDie = false;
  26. IntPtr mDevHandle = IntPtr.Zero;
  27. IntPtr mDBHandle = IntPtr.Zero;
  28. IntPtr FormHandle = IntPtr.Zero;
  29. const int MESSAGE_CAPTURED_OK = 0x0400 + 6;
  30. byte[] FPBuffer;//返回图像
  31. private byte[] fingerTemp = new byte[2048];//指纹模板
  32. int fingerTempLength = 2048;//实际返回指纹模板长度
  33. private int mfpWidth = 0;//图片宽度
  34. private int mfpHeight = 0;//图片高度
  35. #endregion
  36. #region 用户名密码
  37. #endregion
  38. private UserSvc userSvc;
  39. //指纹验证通过的用户
  40. public static user checkSuccessUser = null;
  41. Bitmap fingerPic = null;
  42. private bool isNeedClose = false;
  43. private bool wrongUserbmp = false;
  44. public FingerCheck2()
  45. {
  46. InitializeComponent();
  47. txtPassWord.LostFocus += new EventHandler(txtPassWord_LostFocus);
  48. txtPassWord.GotFocus += new EventHandler(txtPassWord_GotFocus);
  49. this.groupBox1.Hide();
  50. this.cbUser.Hide();
  51. this.txtPassWord.Hide();
  52. this.btnLogin.Hide();
  53. this.lblClose.BringToFront();
  54. this.TopMost = true;
  55. }
  56. private void FingerCheck_Load(object sender, EventArgs e)
  57. {
  58. FormHandle = this.Handle;
  59. userSvc = UserSvc.GetUserSvc();
  60. userSvc.InitUser();
  61. FingerInit();
  62. StartsScanning();
  63. }
  64. private void DoCapture()
  65. {
  66. while (!bIsTimeToDie)
  67. {
  68. int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength);
  69. if (ret == zkfp.ZKFP_ERR_OK)
  70. {
  71. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  72. }
  73. else
  74. {
  75. Thread.Sleep(1000);
  76. picFinger.Image = Image.FromFile(Application.StartupPath + @"\images\finger.gif");
  77. }
  78. Thread.Sleep(200);
  79. }
  80. }
  81. private void FingerInit()
  82. {
  83. int ret = zkfperrdef.ZKFP_ERR_OK;
  84. if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
  85. {
  86. int nCount = zkfp2.GetDeviceCount();
  87. if (nCount <= 0)
  88. {
  89. zkfp2.Terminate();
  90. // MessageBox.Show("找不到设备,请检查指纹刷是否连接");
  91. }
  92. }
  93. else
  94. {
  95. //MessageBox.Show("指纹输入设备初始化失败,请检查指纹刷是否连接");
  96. }
  97. }
  98. /// <summary>
  99. /// 打开扫描
  100. /// </summary>
  101. private void StartsScanning()
  102. {
  103. Console.WriteLine("设备初始化");
  104. int ret = zkfp.ZKFP_ERR_OK;
  105. if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
  106. {
  107. // MessageBox.Show("打开扫描失败");
  108. return;
  109. }
  110. if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
  111. {
  112. // MessageBox.Show("打开扫描失败");
  113. zkfp2.CloseDevice(mDevHandle);
  114. mDevHandle = IntPtr.Zero;
  115. return;
  116. }
  117. byte[] paramValue = new byte[4];
  118. int size = 4;
  119. zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
  120. zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
  121. size = 4;
  122. zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
  123. zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
  124. FPBuffer = new byte[mfpWidth * mfpHeight];
  125. Thread captureThread = new Thread(new ThreadStart(DoCapture));
  126. captureThread.IsBackground = true;
  127. captureThread.Start();
  128. bIsTimeToDie = false;
  129. }
  130. protected override void DefWndProc(ref Message m)
  131. {
  132. switch (m.Msg)
  133. {
  134. case MESSAGE_CAPTURED_OK:
  135. {
  136. MemoryStream ms = new MemoryStream();
  137. BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
  138. BitmapFormat.WriteBitmap(FPBuffer, mfpWidth, mfpHeight);
  139. Bitmap bmp = new Bitmap(ms);
  140. fingerPic = bmp;
  141. user user = UserSvc.GetUserSvc().LoginUserCheck(mDBHandle, fingerTemp);
  142. if (user != null)
  143. {
  144. checkSuccessUser = user;
  145. //将读取到的图片替换动图
  146. isNeedClose = true;
  147. wrongUserbmp = false;
  148. Thread dataGridViewThread = new Thread(this.InfoRefresh);
  149. dataGridViewThread.Start();
  150. }
  151. else
  152. {
  153. picFinger.Image = bmp;
  154. wrongUserbmp = true;
  155. lblInfo.Text = "未识别身份,请重新录入";
  156. }
  157. }
  158. break;
  159. default:
  160. base.DefWndProc(ref m);
  161. break;
  162. }
  163. }
  164. private void lblClose_Click(object sender, EventArgs e)
  165. {
  166. checkSuccessUser = null;
  167. bIsTimeToDie = true;
  168. zkfp2.Terminate();
  169. zkfp2.CloseDevice(mDevHandle);
  170. zkfp2.CloseDevice(mDBHandle);
  171. this.Dispose();
  172. }
  173. private delegate void UpdateMessage();
  174. private void InfoRefresh()
  175. {
  176. UpdateMessage del = new UpdateMessage(UpdateInfo);
  177. if (lblInfo.InvokeRequired)
  178. {
  179. lblInfo.BeginInvoke(del, new object[] { });
  180. }
  181. }
  182. private void UpdateInfo()
  183. {
  184. picFinger.Image = fingerPic;
  185. lblInfo.Text = checkSuccessUser.name + ",您好!验证通过!";
  186. zkfp2.Terminate();
  187. zkfp2.CloseDevice(mDevHandle);
  188. zkfp2.CloseDevice(mDBHandle);
  189. }
  190. private void timer1_Tick(object sender, EventArgs e)
  191. {
  192. if (isNeedClose)
  193. {
  194. bIsTimeToDie = true;
  195. Thread.Sleep(2000);
  196. Console.WriteLine("已经获取到用户");
  197. zkfp2.Terminate();
  198. zkfp2.CloseDevice(mDevHandle);
  199. zkfp2.CloseDevice(mDBHandle);
  200. this.Dispose();
  201. }
  202. }
  203. private void BindUserCheck()
  204. {
  205. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  206. {
  207. IList<user> userList = new List<user>();
  208. user u = new user();
  209. u.id = -1;
  210. u.name = "请选择用户";
  211. userList.Add(u);
  212. userList = userList.Union(ctx.user.Select(s => s).ToList()).ToList();
  213. this.cbUser.DataSource = userList;
  214. this.cbUser.DisplayMember = "name";
  215. this.cbUser.ValueMember = "id";
  216. this.cbUser.SelectedValue = -1;
  217. }
  218. }
  219. private void btnLogin_Click(object sender, EventArgs e)
  220. {
  221. string userIdString = cbUser.SelectedValue.ToString().Trim();
  222. int userId = Convert.ToInt32(userIdString);
  223. string passWord = txtPassWord.Text.Trim();
  224. if (userId == -1)
  225. {
  226. lblInfo.Text = "请选择用户";
  227. return;
  228. }
  229. if (string.IsNullOrEmpty(passWord))
  230. {
  231. return;
  232. }
  233. user userData = null;
  234. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  235. {
  236. userData = ctx.user.Where(s => s.id == userId).FirstOrDefault();
  237. }
  238. if (userData == null)
  239. {
  240. lblInfo.Text = "用户名不存在";
  241. return;
  242. }
  243. else
  244. {
  245. if (userData.pass_word == passWord)
  246. {
  247. checkSuccessUser = userData;
  248. bIsTimeToDie = true;
  249. zkfp2.Terminate();
  250. zkfp2.CloseDevice(mDevHandle);
  251. zkfp2.CloseDevice(mDBHandle);
  252. this.Dispose();
  253. }
  254. else
  255. {
  256. lblInfo.Text = "密码错误";
  257. return;
  258. }
  259. }
  260. }
  261. private void btnPut_Click(object sender, EventArgs e)
  262. {
  263. if (btnPut.Text.Trim() == "密码验证")
  264. {
  265. picFinger.Hide();
  266. groupBox1.Show();
  267. cbUser.Show();
  268. txtPassWord.Show();
  269. btnLogin.Show();
  270. btnPut.Text = "指纹验证";
  271. lblInfo.Text = "";
  272. BindUserCheck();
  273. }
  274. else
  275. {
  276. picFinger.Show();
  277. groupBox1.Hide();
  278. cbUser.Hide();
  279. txtPassWord.Hide();
  280. btnLogin.Hide();
  281. btnPut.Text = "密码验证";
  282. lblInfo.Text = "请验证指纹";
  283. }
  284. }
  285. private void txtPassWord_MouseClick(object sender, MouseEventArgs e)
  286. {
  287. string passWord = txtPassWord.Text.Trim();
  288. if (passWord == "请输入密码")
  289. {
  290. txtPassWord.Text = "";
  291. txtPassWord.PasswordChar = '*';
  292. }
  293. }
  294. private void txtPassWord_GotFocus(object sender, EventArgs e)
  295. {
  296. string passWord = txtPassWord.Text.Trim();
  297. if (passWord == "请输入密码")
  298. {
  299. txtPassWord.Text = "";
  300. txtPassWord.PasswordChar = '*';
  301. }
  302. }
  303. private void txtPassWord_LostFocus(object sender, EventArgs e)
  304. {
  305. string passWord = txtPassWord.Text.Trim();
  306. if (string.IsNullOrEmpty(passWord))
  307. {
  308. txtPassWord.Text = "请输入密码";
  309. txtPassWord.PasswordChar = new char();
  310. }
  311. }
  312. private void cbUser_SelectedIndexChanged(object sender, EventArgs e)
  313. {
  314. txtPassWord.Text = "请输入密码";
  315. txtPassWord.PasswordChar = new char();
  316. }
  317. }
  318. }