UserEdit.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using EntityDataSet;
  13. using IntelligentControlForsx.Service.FingerSvc;
  14. using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
  15. using libzkfpcsharp;
  16. namespace IntelligentControlForsx.ChildForms
  17. {
  18. public partial class UserEdit : 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. byte[] finger1 = new byte[2048];
  32. byte[] finger2 = new byte[2048];
  33. byte[] finger3 = new byte[2048];
  34. byte[] registResultFinger1 = new byte[2048];//最终结果模板1
  35. byte[] registResultFinger2 = new byte[2048];//最终结果模板2
  36. byte[] registResultFinger3 = new byte[2048];//最终结果模板3
  37. byte[] registResultFinger4 = new byte[2048];//最终结果模板4
  38. byte[] registResultFinger5 = new byte[2048];//最终结果模板5
  39. byte[] registResultFinger6 = new byte[2048];//最终结果模板6
  40. byte[] registResultFinger7 = new byte[2048];//最终结果模板7
  41. byte[] registResultFinger8 = new byte[2048];//最终结果模板8
  42. byte[] registResultFinger9 = new byte[2048];//最终结果模板9
  43. byte[] registResultFinger10 = new byte[2048];//最终结果模板10
  44. private bool isEditFinger1 = false;
  45. private bool isEditFinger2 = false;
  46. private bool isEditFinger3 = false;
  47. private bool isEditFinger4 = false;
  48. private bool isEditFinger5 = false;
  49. private bool isEditFinger6 = false;
  50. private bool isEditFinger7 = false;
  51. private bool isEditFinger8 = false;
  52. private bool isEditFinger9 = false;
  53. private bool isEditFinger10 = false;
  54. int iFid = 1;//指纹编号
  55. private int pushFingerCount = 0;
  56. byte[] dataBaseTemp = new byte[2048];
  57. #endregion
  58. private UserSvc userSvc;
  59. //获取路径
  60. string homeStr = "";
  61. private user editUser = null;
  62. private int fingerNum = -1;
  63. public UserEdit(user user)
  64. {
  65. InitializeComponent();
  66. homeStr = Application.StartupPath;
  67. userSvc = UserSvc.GetUserSvc();
  68. userSvc.InitUser();
  69. FingerInit();
  70. StartsScanning();
  71. picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  72. editUser = user;
  73. }
  74. private void AddFinger_Load(object sender, EventArgs e)
  75. {
  76. FormHandle = this.Handle;
  77. IList<CbEntity> list = new List<CbEntity>();
  78. CbEntity et = new CbEntity();
  79. et.CbValue = "-1";
  80. et.ShowName = "请选择";
  81. list.Add(et);
  82. for (int i = 1; i <= 10; i++)
  83. {
  84. CbEntity entity = new CbEntity();
  85. entity.ShowName = "指纹" + i;
  86. entity.CbValue = i.ToString();
  87. list.Add(entity);
  88. }
  89. this.cbFinger.DataSource = list;
  90. this.cbFinger.DisplayMember = "showName";
  91. this.cbFinger.ValueMember = "cbValue";
  92. this.cbFinger.SelectedValue = "-1";
  93. if (editUser != null)
  94. {
  95. txtUserName.Text = editUser.user_name;
  96. txtName.Text = editUser.name;
  97. txtTel.Text = editUser.telephone;
  98. txtPassWord.Text = editUser.pass_word;
  99. }
  100. }
  101. private void btnSubmit_Click(object sender, EventArgs e)
  102. {
  103. string name = txtName.Text.Trim();
  104. string tel = txtTel.Text.Trim();
  105. string userName = txtUserName.Text.Trim();
  106. string passWord = txtPassWord.Text.Trim();
  107. if (string.IsNullOrEmpty(tel))
  108. {
  109. lblInfo.Text = "请输入电话";
  110. return;
  111. }
  112. if (string.IsNullOrEmpty(passWord))
  113. {
  114. lblInfo.Text = "请输入密码";
  115. return;
  116. }
  117. if (editUser != null)
  118. {
  119. editUser.telephone = tel;
  120. editUser.pass_word = passWord;
  121. if (isEditFinger1)
  122. editUser.finger1_template = registResultFinger1;
  123. if (isEditFinger2)
  124. editUser.finger2_template = registResultFinger2;
  125. if (isEditFinger3)
  126. editUser.finger3_template = registResultFinger3;
  127. if (isEditFinger4)
  128. editUser.finger4_template = registResultFinger4;
  129. if (isEditFinger5)
  130. editUser.finger5_template = registResultFinger5;
  131. if (isEditFinger6)
  132. editUser.finger6_template = registResultFinger6;
  133. if (isEditFinger7)
  134. editUser.finger7_template = registResultFinger7;
  135. if (isEditFinger8)
  136. editUser.finger8_template = registResultFinger8;
  137. if (isEditFinger9)
  138. editUser.finger9_template = registResultFinger9;
  139. if (isEditFinger10)
  140. editUser.finger10_template = registResultFinger10;
  141. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  142. {
  143. ctx.Entry(editUser).State = EntityState.Modified;
  144. int count = ctx.SaveChanges();
  145. if (count > 0)
  146. {
  147. MessageBox.Show("修改成功");
  148. zkfp2.Terminate();
  149. zkfp2.CloseDevice(mDevHandle);
  150. zkfp2.CloseDevice(mDBHandle);
  151. this.Dispose();
  152. }
  153. else
  154. {
  155. MessageBox.Show("修改失败,请联系管理员");
  156. }
  157. }
  158. }
  159. else
  160. {
  161. MessageBox.Show("获取用户失败,请关闭重新尝试");
  162. }
  163. }
  164. /// <summary>
  165. /// 初始化指纹设备
  166. /// </summary>
  167. private void FingerInit()
  168. {
  169. int ret = zkfperrdef.ZKFP_ERR_OK;
  170. if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
  171. {
  172. int nCount = zkfp2.GetDeviceCount();
  173. if (nCount <= 0)
  174. {
  175. zkfp2.Terminate();
  176. MessageBox.Show("找不到设备,请检查指纹刷是否连接");
  177. }
  178. }
  179. else
  180. {
  181. MessageBox.Show("指纹输入设备初始化失败,请检查指纹刷是否连接");
  182. }
  183. }
  184. /// <summary>
  185. /// 打开扫描
  186. /// </summary>
  187. private void StartsScanning()
  188. {
  189. Console.WriteLine("设备初始化");
  190. int ret = zkfp.ZKFP_ERR_OK;
  191. if (IntPtr.Zero == (mDevHandle = zkfp2.OpenDevice(0)))
  192. {
  193. MessageBox.Show("打开扫描失败");
  194. return;
  195. }
  196. if (IntPtr.Zero == (mDBHandle = zkfp2.DBInit()))
  197. {
  198. MessageBox.Show("打开扫描失败");
  199. zkfp2.CloseDevice(mDevHandle);
  200. mDevHandle = IntPtr.Zero;
  201. return;
  202. }
  203. byte[] paramValue = new byte[4];
  204. int size = 4;
  205. zkfp2.GetParameters(mDevHandle, 1, paramValue, ref size);
  206. zkfp2.ByteArray2Int(paramValue, ref mfpWidth);
  207. size = 4;
  208. zkfp2.GetParameters(mDevHandle, 2, paramValue, ref size);
  209. zkfp2.ByteArray2Int(paramValue, ref mfpHeight);
  210. FPBuffer = new byte[mfpWidth * mfpHeight];
  211. Thread captureThread = new Thread(new ThreadStart(DoCapture));
  212. captureThread.IsBackground = true;
  213. captureThread.Start();
  214. bIsTimeToDie = false;
  215. }
  216. /// <summary>
  217. /// 指纹扫描线程
  218. /// </summary>
  219. private void DoCapture()
  220. {
  221. while (!bIsTimeToDie)
  222. {
  223. fingerTempLength = 2048;
  224. int ret = zkfp2.AcquireFingerprint(mDevHandle, FPBuffer, fingerTemp, ref fingerTempLength);
  225. if (ret == zkfp.ZKFP_ERR_OK)
  226. {
  227. if (pushFingerCount == 0)
  228. {
  229. finger1 = (byte[])fingerTemp.Clone();
  230. Console.WriteLine("手指1");
  231. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  232. }
  233. else if (pushFingerCount == 1)
  234. {
  235. finger2 = (byte[])fingerTemp.Clone();
  236. Console.WriteLine("手指2");
  237. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  238. }
  239. else if (pushFingerCount == 2)
  240. {
  241. finger3 = (byte[])fingerTemp.Clone();
  242. Console.WriteLine("手指3");
  243. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  244. }
  245. else
  246. {
  247. FingerReadSvc.SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
  248. }
  249. }
  250. Thread.Sleep(200);
  251. }
  252. }
  253. protected override void DefWndProc(ref Message m)
  254. {
  255. switch (m.Msg)
  256. {
  257. case MESSAGE_CAPTURED_OK:
  258. {
  259. MemoryStream ms = new MemoryStream();
  260. BitmapFormat.GetBitmap(FPBuffer, mfpWidth, mfpHeight, ref ms);
  261. BitmapFormat.WriteBitmap(FPBuffer, mfpWidth, mfpHeight);
  262. Bitmap bmp = new Bitmap(ms);
  263. int ret = zkfp.ZKFP_ERR_OK;
  264. int fid = 0, score = 0;
  265. if (pushFingerCount == 0)
  266. {
  267. ret = zkfp2.DBIdentify(mDBHandle, finger1, ref fid, ref score);
  268. pushFingerCount = 1;
  269. lblInfo.Text = "指纹第一次录入成功,请再次输入指纹";
  270. this.picFinger1.Image = bmp;
  271. picFinger2.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  272. }
  273. else if (pushFingerCount == 1)
  274. {
  275. int compareScore = zkfp2.DBMatch(mDBHandle, finger1, finger2);
  276. if (compareScore <= 0)
  277. {
  278. lblInfo.Text = "请连续三次输入相同手指的指纹";
  279. finger1 = new byte[2048];
  280. finger2 = new byte[2048];
  281. finger3 = new byte[2048];
  282. pushFingerCount = 0;
  283. picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  284. picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  285. picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  286. }
  287. else
  288. {
  289. pushFingerCount = 2;
  290. lblInfo.Text = "指纹第二次录入成功,请再次输入指纹";
  291. this.picFinger2.Image = bmp;
  292. picFinger3.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  293. }
  294. }
  295. else if (pushFingerCount == 2)
  296. {
  297. int compareScore = zkfp2.DBMatch(mDBHandle, finger3, finger2);
  298. if (compareScore <= 0)
  299. {
  300. lblInfo.Text = "请连续三次输入相同手指的指纹";
  301. finger1 = new byte[2048];
  302. finger2 = new byte[2048];
  303. finger3 = new byte[2048];
  304. pushFingerCount = 0;
  305. picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  306. picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  307. picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  308. }
  309. else
  310. {
  311. pushFingerCount = 3;
  312. lblInfo.Text = "指纹第三次录入成功";
  313. this.picFinger3.Image = bmp;
  314. }
  315. }
  316. else
  317. {
  318. //txtInfo.Text = "未知错误";
  319. }
  320. if (pushFingerCount == 3)
  321. {
  322. byte[] registResultFinger = null;
  323. switch (fingerNum)
  324. {
  325. case 1:
  326. registResultFinger = registResultFinger1;
  327. isEditFinger1 = true;
  328. break;
  329. case 2:
  330. registResultFinger = registResultFinger2;
  331. isEditFinger2 = true;
  332. break;
  333. case 3:
  334. registResultFinger = registResultFinger3;
  335. isEditFinger3 = true;
  336. break;
  337. case 4:
  338. registResultFinger = registResultFinger4;
  339. isEditFinger4 = true;
  340. break;
  341. case 5:
  342. registResultFinger = registResultFinger5;
  343. isEditFinger5 = true;
  344. break;
  345. case 6:
  346. registResultFinger = registResultFinger6;
  347. isEditFinger6 = true;
  348. break;
  349. case 7:
  350. registResultFinger = registResultFinger7;
  351. isEditFinger7 = true;
  352. break;
  353. case 8:
  354. registResultFinger = registResultFinger8;
  355. isEditFinger8 = true;
  356. break;
  357. case 9:
  358. registResultFinger = registResultFinger9;
  359. isEditFinger9 = true;
  360. break;
  361. case 10:
  362. registResultFinger = registResultFinger10;
  363. isEditFinger10 = true;
  364. break;
  365. default: break;
  366. }
  367. if (registResultFinger != null)
  368. {
  369. int ret1 = zkfp2.DBMerge(mDBHandle, finger1, finger2, finger3, registResultFinger, ref pushFingerCount);
  370. int ret2 = zkfp2.DBAdd(mDBHandle, iFid, registResultFinger);
  371. if (ret1 == zkfp.ZKFP_ERR_OK && ret2 == zkfp.ZKFP_ERR_OK)
  372. {
  373. btnSubmit.Enabled = true;
  374. }
  375. }
  376. }
  377. }
  378. break;
  379. default:
  380. base.DefWndProc(ref m);
  381. break;
  382. }
  383. }
  384. private void btnClose_Click(object sender, EventArgs e)
  385. {
  386. zkfp2.Terminate();
  387. zkfp2.CloseDevice(mDevHandle);
  388. zkfp2.CloseDevice(mDBHandle);
  389. this.Dispose();
  390. }
  391. private void txtName_TextChanged(object sender, EventArgs e)
  392. {
  393. string name = txtName.Text.Trim();
  394. string tel = txtTel.Text.Trim();
  395. if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrEmpty(tel))
  396. {
  397. //FingerInit();
  398. //StartsScanning();
  399. //string homeStr = Application.StartupPath;
  400. //picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  401. }
  402. }
  403. private void txtTel_TextChanged(object sender, EventArgs e)
  404. {
  405. string name = txtName.Text.Trim();
  406. string tel = txtTel.Text.Trim();
  407. if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrEmpty(tel))
  408. {
  409. //FingerInit();
  410. //StartsScanning();
  411. //string homeStr = Application.StartupPath;
  412. //picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  413. }
  414. }
  415. private void btnRest_Click(object sender, EventArgs e)
  416. {
  417. finger1 = new byte[2048];
  418. finger2 = new byte[2048];
  419. finger3 = new byte[2048];
  420. registResultFinger1 = new byte[2048];
  421. registResultFinger2 = new byte[2048];
  422. registResultFinger3 = new byte[2048];
  423. registResultFinger4 = new byte[2048];
  424. registResultFinger5 = new byte[2048];
  425. registResultFinger6 = new byte[2048];
  426. registResultFinger7 = new byte[2048];
  427. registResultFinger8 = new byte[2048];
  428. registResultFinger9 = new byte[2048];
  429. registResultFinger10 = new byte[2048];
  430. pushFingerCount = 0;
  431. picFinger1.Image = Image.FromFile(homeStr + @"\images\finger.gif");
  432. picFinger2.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  433. picFinger3.Image = Image.FromFile(homeStr + @"\images\finger_static.png");
  434. }
  435. private void cbFinger_SelectedIndexChanged(object sender, EventArgs e)
  436. {
  437. string fingerNumString = cbFinger.SelectedValue.ToString().Trim();
  438. if (!fingerNumString.Contains("CbEntity"))
  439. {
  440. fingerNum = Convert.ToInt32(fingerNumString);
  441. finger1 = new byte[2048];
  442. finger2 = new byte[2048];
  443. finger3 = new byte[2048];
  444. pushFingerCount = 0;
  445. }
  446. }
  447. }
  448. public class CbEntity
  449. {
  450. public string ShowName { set; get; }
  451. public string CbValue { set; get; }
  452. }
  453. }