-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFingerPrint.cs
1694 lines (1481 loc) · 57.9 KB
/
FingerPrint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using libzkfpcsharp;
using Serilog;
using DigitalPlatform;
using DigitalPlatform.Text;
using DigitalPlatform.Interfaces;
using DigitalPlatform.CirculationClient;
using DigitalPlatform.Drawing;
namespace FingerprintCenter
{
/// <summary>
/// 指纹功能类
/// </summary>
public class FingerPrint : BioUtil
{
#if NO
public static event SpeakEventHandler Speak = null;
public static event CapturedEventHandler Captured = null;
public static event ImageReadyEventHandler ImageReady = null;
public static event DownloadProgressChangedEventHandler ProgressChanged = null;
/// <summary>
/// 提示框事件
/// </summary>
public static event MessagePromptEventHandler Prompt = null;
#endif
public override string DriverName
{
get
{
return "zk";
}
}
// 算法版本号
public override string AlgorithmVersion
{
get
{
return "10";
}
}
int _idSeed = 10;
Hashtable _id_barcode_table = new Hashtable(); // id --> barcode
Hashtable _barcode_id_table = new Hashtable(); // barcode --> id
IntPtr _dBHandle = IntPtr.Zero;
IntPtr _devHandle = IntPtr.Zero;
// 当前的运行模式
// read: 读取模式。每次扫入一个指纹,识别出证条码号以后触发事件
// register: 注册模式。要求读完三个指纹,API 才返回
string _mode = "read";
// 存储注册用的指纹模板
List<byte[]> _register_template_list = new List<byte[]>();
// 注册过程中,查重时,需要排除条码号
List<string> _exclude = new List<string>();
// 注册过程完成
AutoResetEvent _eventRegisterFinished = new AutoResetEvent(false);
public const int DefaultThreshold = 70; // 2019/6/19 修改为 70。此前是 10
int _shreshold = DefaultThreshold;
public int Shreshold
{
get
{
return _shreshold;
}
set
{
_shreshold = value;
}
}
// 默认的指纹登记质量最低分
public const int DefaultRegisterQuality = 60;
// 默认的指纹识别质量最低分
public const int DefaultRecognitionQuality = 60;
int _registerShreshold = DefaultRegisterQuality;
public int RegisterShreshold
{
get
{
return _registerShreshold;
}
set
{
_registerShreshold = value;
}
}
int _recognitionShreshold = DefaultRecognitionQuality;
public int RecognitionShreshold
{
get
{
return _recognitionShreshold;
}
set
{
_recognitionShreshold = value;
}
}
// 默认的指纹质量最低分
// public const int DefaultQuality = 60;
public override string BioTypeName
{
get
{
return "指纹";
}
}
public FingerPrint()
{
BrowseStyle = "fingerprint";
SearchFrom = "指纹时间戳";
ElementName = "fingerprint";
GetImage += PalmDriver_GetImage;
}
public override NormalResult Init(int dev_index)
{
try
{
NormalResult result = _init();
if (result.Value == -1)
return result;
return OpenZK(dev_index);
}
catch (Exception ex)
{
if (ex.Source == "libzkfpcsharp"
&& ex.Message.IndexOf("libzkfp.dll") != -1)
{
return new NormalResult
{
Value = -1,
ErrorCode = "driver not install",
ErrorInfo = "尚未安装'中控'指纹仪厂家驱动程序"
};
}
return new NormalResult { Value = -1, ErrorInfo = ex.Message };
}
}
public override NormalResult Free()
{
try
{
_free();
return new NormalResult();
}
catch (Exception ex)
{
return new NormalResult { Value = -1, ErrorInfo = ex.Message };
}
}
// 设备列表
List<string> _dev_list = new List<string>();
public new List<string> DeviceList
{
get
{
return new List<string>(_dev_list);
}
}
NormalResult _init()
{
_free();
_dev_list.Clear();
int ret = zkfperrdef.ZKFP_ERR_OK;
if ((ret = zkfp2.Init()) == zkfperrdef.ZKFP_ERR_OK)
{
// TODO: 允许设置和选择多个指纹阅读器中的一个
int nCount = zkfp2.GetDeviceCount();
if (nCount > 0)
{
for (int i = 0; i < nCount; i++)
{
_dev_list.Add(i.ToString());
}
}
else
{
zkfp2.Terminate();
// MessageBox.Show("No device connected!");
return new NormalResult { Value = -1, ErrorInfo = "尚未连接指纹阅读器" };
}
return new NormalResult();
}
else
{
// MessageBox.Show("Initialize fail, ret=" + ret + " !");
string message = $"初始化失败,错误码: {ret}";
if (ret == -1)
message = "尚未连接指纹阅读器";
return new NormalResult
{
Value = -1,
ErrorCode = "fingerprint:" + ret.ToString(),
ErrorInfo = message
};
}
}
void _free()
{
zkfp2.Terminate();
SetImage(null);
}
NormalResult OpenZK(int dev_index)
{
CloseZK();
int ret = zkfp.ZKFP_ERR_OK;
if (IntPtr.Zero == (_devHandle = zkfp2.OpenDevice(dev_index
//cmbIdx.SelectedIndex
)))
{
// MessageBox.Show("OpenDevice fail");
return new NormalResult { Value = -1, ErrorInfo = "打开设备失败" };
}
if (IntPtr.Zero == (_dBHandle = zkfp2.DBInit()))
{
// MessageBox.Show("Init DB fail");
//zkfp2.CloseDevice(_devHandle);
//_devHandle = IntPtr.Zero;
CloseZK();
return new NormalResult { Value = -1, ErrorInfo = "初始化高速缓存失败" };
}
_id_barcode_table.Clear();
_barcode_id_table.Clear();
#if NO
int old_value = GetIntParameter(3);
byte[] value = new byte[4];
bool bRet = zkfp2.Int2ByteArray(500, value);
ret = zkfp2.SetParameters(_devHandle, 3, value, 4);
#endif
return new NormalResult();
#if NO
bnInit.Enabled = false;
bnFree.Enabled = true;
bnOpen.Enabled = false;
bnClose.Enabled = true;
bnEnroll.Enabled = true;
bnVerify.Enabled = true;
bnIdentify.Enabled = true;
RegisterCount = 0;
cbRegTmp = 0;
iFid = 1;
for (int i = 0; i < 3; i++)
{
RegTmps[i] = new byte[2048];
}
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;
textRes.Text = "Open succ";
#endif
}
void CloseZK()
{
if (_dBHandle != IntPtr.Zero)
{
zkfp2.DBFree(_dBHandle);
_dBHandle = IntPtr.Zero;
}
if (_devHandle != IntPtr.Zero)
{
zkfp2.CloseDevice(_devHandle);
_devHandle = IntPtr.Zero;
}
}
public void Light(string strColor, int duration = 500)
{
Task.Run(() =>
{
int code = 101;
if (strColor == "white")
code = 101;
else if (strColor == "green")
code = 102;
else if (strColor == "red")
code = 103;
byte[] value = new byte[4];
bool bRet = zkfp2.Int2ByteArray(1, value);
int ret = zkfp2.SetParameters(_devHandle, code, value, 4);
Thread.Sleep(duration);
bRet = zkfp2.Int2ByteArray(0, value);
ret = zkfp2.SetParameters(_devHandle, code, value, 4);
});
}
public void ClearDB()
{
if (_dBHandle != IntPtr.Zero)
{
int ret = zkfp2.DBClear(_dBHandle);
_id_barcode_table.Clear();
_barcode_id_table.Clear();
}
}
int RemoveItem(string strReaderBarcode,
out string strError)
{
return AddItems(new List<FingerprintItem> { new FingerprintItem { ReaderBarcode = strReaderBarcode } },
null,
out strError);
}
// TODO: info_param 要使用
// 添加高速缓存事项
// 如果items == null 或者 items.Count == 0,表示要清除当前的全部缓存内容
// 如果一个item对象的FingerprintString为空,表示要删除这个缓存事项
// return:
// 0 成功
// 其他 失败。错误码
public override int AddItems(
List<FingerprintItem> items,
ProcessInfo info_param,
out string strError)
{
strError = "";
#if NO
if (this.m_host == null)
{
if (Open(out strError) == -1)
return -1;
}
#endif
if (_dBHandle == IntPtr.Zero)
{
strError = "指纹设备尚未初始化";
return -1;
}
// 清除已有的全部缓存内容
if (items == null || items.Count == 0)
{
#if NO
if (this.m_handle != -1)
{
this.m_host.FreeFPCacheDB(this.m_handle);
this.m_handle = -1;
}
#endif
ClearDB();
return 0;
}
#if NO
if (this.m_handle == -1)
{
this.m_handle = this.m_host.CreateFPCacheDB();
this.id_barcode_table.Clear();
this.barcode_id_table.Clear();
}
#endif
List<string> failed_barcodes = new List<string>();
foreach (FingerprintItem item in items)
{
// TODO: 这里可以尝试检查拟加入的指纹模板是否在高速缓存中已经存在
// 看看条码号以前是否已经存在?
if (_barcode_id_table.Contains(item.ReaderBarcode) == true)
{
int nOldID = (int)_barcode_id_table[item.ReaderBarcode];
// this.m_host.RemoveRegTemplateFromFPCacheDB(this.m_handle, nOldID);
zkfp2.DBDel(_dBHandle, nOldID);
_id_barcode_table.Remove(nOldID.ToString());
if (string.IsNullOrEmpty(item.FingerprintString) == true)
_barcode_id_table.Remove(item.ReaderBarcode);
}
if (string.IsNullOrEmpty(item.FingerprintString) == false)
{
int id = _idSeed++;
_id_barcode_table[id.ToString()] = item.ReaderBarcode;
_barcode_id_table[item.ReaderBarcode] = id;
try
{
int ret = zkfp2.DBAdd(_dBHandle, id, zkfp2.Base64ToBlob(item.FingerprintString));
if (ret != 0)
{
_id_barcode_table.Remove(id.ToString());
_barcode_id_table.Remove(item.ReaderBarcode);
// 可能是因为加入了重复或者相似的指纹模板导致
//strError = $"DBAdd() 失败,证条码号={item.ReaderBarcode}, 错误码={ret}";
//return ret;
failed_barcodes.Add(item.ReaderBarcode + "|" + ret);
}
// this.m_host.AddRegTemplateStrToFPCacheDB(this.m_handle, id, item.FingerprintString);
}
catch (Exception ex)
{
strError = "AddRegTemplateStrToFPCacheDB() error. id=" + id.ToString() + " ,item.FingerprintString='" + item.FingerprintString + "', message=" + ex.Message;
return -1;
}
}
}
if (failed_barcodes.Count > 0)
{
strError = "下列证条码号对应的指纹模板加入高速缓存时失败: " + StringUtil.MakePathList(failed_barcodes);
return -1;
}
return 0;
}
#if NO
// 处理一小批指纹数据的装入
// parameters:
static void GetSomeFingerprintData(
LibraryChannel channel,
List<string> lines,
CancellationToken token,
out List<DigitalPlatform.LibraryClient.localhost.Record> records)
{
// strError = "";
records = new List<DigitalPlatform.LibraryClient.localhost.Record>();
for (; ; )
{
token.ThrowIfCancellationRequested();
DigitalPlatform.LibraryClient.localhost.Record[] searchresults = null;
string[] paths = new string[lines.Count];
lines.CopyTo(paths);
REDO_GETRECORDS:
long lRet = channel.GetBrowseRecords(
null,
paths,
"id,cols,format:cfgs/browse_fingerprint",
out searchresults,
out string strError);
if (lRet == -1)
{
#if NO
DialogResult temp_result = MessageBox.Show(this,
strError + "\r\n\r\n是否重试?",
"ReaderSearchForm",
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
if (temp_result == DialogResult.Retry)
goto REDO_GETRECORDS;
return -1;
#endif
if (Prompt != null)
{
MessagePromptEventArgs e = new MessagePromptEventArgs
{
// e.MessageText = "获得书目记录 '"+strCommand+"' ("+StringUtil.MakePathList(format_list)+") 时发生错误: " + strError;
MessageText = strError + "\r\n\r\n是否重试?",
Actions = "yes,no,cancel"
};
Prompt(channel, e);
if (e.ResultAction == "cancel")
throw new ChannelException(channel.ErrorCode, strError);
else if (e.ResultAction == "yes")
goto REDO_GETRECORDS;
else
{
// no 也是抛出异常。因为继续下一批代价太大
throw new ChannelException(channel.ErrorCode, strError);
}
}
else
throw new ChannelException(channel.ErrorCode, strError);
}
records.AddRange(searchresults);
// 去掉已经做过的一部分
lines.RemoveRange(0, searchresults.Length);
if (lines.Count == 0)
break;
}
}
#endif
#if NO
public static int InitFingerprintCache(
LibraryChannel channel,
string strDir,
CancellationToken token,
out string strError)
{
BioEnv env = new BioEnv {
AddItems = AddItems,
SetProgress = SetProgress,
ShowMessage = ShowMessage,
LoaderPrompt = Loader_Prompt,
};
return BioUtil.InitFingerprintCache(
channel,
strDir,
token,
env,
out strError);
}
#endif
// Capture 过程中用到的变量
class CaptureData
{
// StartCapture() 所使用的 token。记忆下来使用
// public CancellationToken _cancelToken = new CancellationToken();
public byte[] CapTmp = new byte[2048];
public int cbCapTmp = 2048;
// public byte[] FPBuffer;
public int mfpWidth = 0;
public int mfpHeight = 0;
}
static CaptureData _captureData = new CaptureData();
const int PARAMETER_PICTURE_WIDTH = 1; // 图象宽
const int PARAMETER_PICTURE_HEIGHT = 2; // 图象高
// index:
// 1 图像宽
// 2 图像高
int GetIntParameter(int index)
{
int value = 0;
byte[] paramValue = new byte[4];
int size = 4;
zkfp2.GetParameters(_devHandle, index, paramValue, ref size);
zkfp2.ByteArray2Int(paramValue, ref value);
return value;
}
public override void StartCapture(CancellationToken token)
{
Log.Debug($"StartCapture()");
_captureData.mfpWidth = GetIntParameter(PARAMETER_PICTURE_WIDTH);
_captureData.mfpHeight = GetIntParameter(PARAMETER_PICTURE_HEIGHT);
// _captureData.FPBuffer = new byte[_captureData.mfpWidth * _captureData.mfpHeight];
_register_template_list.Clear();
_exclude.Clear();
Thread captureThread = new Thread(new ParameterizedThreadStart(CaptureThreadMain));
// captureThread.IsBackground = true;
captureThread.Start(token);
// _captureData._cancelToken = token;
}
void CaptureThreadMain(object obj)
{
CancellationToken token = (CancellationToken)obj;
Log.Debug($"Begin CaptureThreadMain()");
try
{
// while (!_captureData._cancelToken.IsCancellationRequested)
while (!token.IsCancellationRequested)
{
byte[] image_buffer = new byte[_captureData.mfpWidth * _captureData.mfpHeight];
byte[] template_buffer = new byte[2048];
int template_buffer_length = 2048;
// 这一句可能抛出内存损坏异常
int ret = zkfp2.AcquireFingerprint(_devHandle,
image_buffer,
template_buffer,
ref template_buffer_length);
if (ret == zkfp.ZKFP_ERR_OK)
{
var quality = GetIntParameter(10002);
// SendMessage(FormHandle, MESSAGE_CAPTURED_OK, IntPtr.Zero, IntPtr.Zero);
ProcessCaptureData(image_buffer,
template_buffer,
template_buffer_length,
quality);
}
Task.Delay(200, token).Wait(token);
// Thread.Sleep(200);
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Log.Error($"*** CaptureThreadMain() Exception: {ExceptionUtil.GetExceptionText(ex)}");
}
finally
{
Log.Debug($"End CaptureThreadMain()");
}
}
#region 练习模式
List<int> m_gameScores = new List<int>();
int CountContinue(int v)
{
int nCount = 0;
for (int i = this.m_gameScores.Count - 1; i >= 0; i--)
{
if (this.m_gameScores[i] != v)
{
return nCount;
}
nCount++;
}
return nCount;
}
string GetScoreString(int v)
{
string strResult = "";
if (v >= 100)
{
int nContine = CountContinue(100);
if (nContine >= 1)
{
strResult = "连续 " + (nContine + 1).ToString() + " 次 100 分!";
goto END1;
}
else
strResult = "极端完美!";
}
else if (v >= 90)
strResult = "帅呆了!";
else if (v >= 80)
strResult = "非常好!";
else if (v >= 70)
strResult = "很好!";
else if (v >= 60)
strResult = "还行!";
else if (v >= 50)
strResult = "加油啊!";
else
strResult = "不好意思!";
strResult += v.ToString() + " 分";
END1:
m_gameScores.Add(v);
while (this.m_gameScores.Count > 100)
{
this.m_gameScores.RemoveAt(0);
}
return strResult;
}
#endregion
public static Bitmap BuildTextImage(string strText,
Color backColor,
float fFontSize = 64,
int nWidth = 400)
{
// 文字图片
return ArtText.BuildArtText(
strText,
"Microsoft YaHei", // "Consolas", //
fFontSize, // (float)16,
FontStyle.Regular, // .Bold,
Color.White,
backColor, // Color.DarkRed,
Color.Gray,
ArtEffect.None,
nWidth);
}
// parameters:
// template_buffer 指纹模板数据
// length template_buffer 数组内有效数据长度
void ProcessCaptureData(
byte[] image_buffer,
byte[] template_buffer,
int length,
int quality)
{
if (this.HasImageReady())
{
Task.Run(() =>
{
try
{
// TODO: 注意检查这里是否会出现内存泄漏
MemoryStream ms = new MemoryStream();
BitmapFormat.GetBitmap(image_buffer,
_captureData.mfpWidth,
_captureData.mfpHeight,
/*ref*/ ms);
ms.Seek(0, SeekOrigin.Begin);
TriggerImageReady(null, new ImageReadyEventArgs { Image = new Bitmap(ms), Quality = quality });
// 2022/6/10
ms.Seek(0, SeekOrigin.Begin);
SetImage(new Bitmap(ms));
}
catch (Exception ex)
{
// 返回一个带有文字的图片
SetImage(BuildTextImage(
ex.Message,
Color.DarkRed,
32,
600));
}
/*
// 2022/6/7
{
byte[] temp = new byte[image_buffer.Length];
Array.Copy(image_buffer, temp, image_buffer.Length);
SetImage(temp, // image_buffer,
_captureData.mfpWidth,
_captureData.mfpHeight,
null);
}
*/
});
}
// 练习模式
if (_mode == "practice")
{
if (quality >= 60)
Light("green");
else
Light("red");
{
string text = GetScoreString(quality);
Speaking(text,
$"{text}\r\n质量: {quality}");
SendRegisterMessage(text);
}
return;
}
// 检查指纹质量
if (_mode == "register")
{
if (quality < RegisterShreshold)
{
Light("red");
string text = $"指纹图像质量不佳({quality}),请重新扫入";
Speaking(text,
$"{text}\r\n质量: {quality}");
SendRegisterMessage(text);
return;
}
}
else
{
if (quality < RecognitionShreshold)
{
Light("red");
string text = $"指纹图像质量不佳({quality}),请重新扫入";
Speaking(text,
$"{text}\r\n质量: {quality}");
return;
}
}
if (_mode == "register")
{
// 查重
{
int id = 0, score = 0;
int ret = zkfp2.DBIdentify(_dBHandle, template_buffer, ref id, ref score);
if (zkfp.ZKFP_ERR_OK == ret)
{
// 根据 id 取出 barcode 字符串
string strBarcode = (string)_id_barcode_table[id.ToString()];
if (_exclude.IndexOf(strBarcode) == -1)
{
string text = $"您的指纹以前已经被 {strBarcode} 注册过了(id={id}),无法重复注册";
Speaking(text,
$"{text}\r\n质量: {quality}");
SendRegisterMessage(text);
return;
}
}
}
// 和上一次的比对
if (_register_template_list.Count > 0)
{
int nRet = zkfp2.DBMatch(_dBHandle, template_buffer, _register_template_list[_register_template_list.Count - 1]);
if (nRet <= 0)
{
_register_template_list.Clear(); // 从头来
Light("red");
string text = "刚扫入的指纹和先前的指纹不一致,请继续重新扫入";
Speaking(text,
$"{text}\r\n质量: {quality}");
SendRegisterMessage(text);
return;
}
}
{
byte[] buffer = new byte[length];
Array.Copy(template_buffer, buffer, length);
_register_template_list.Add(buffer);
}
if (_register_template_list.Count >= 3)
{
Light("green");
// 结束 register 轮回
_eventRegisterFinished.Set();
SendRegisterMessage("指纹扫入完成");
return;
}
Light("green");
{
string text = "很好。还需要扫入 " + (3 - _register_template_list.Count) + " 个指纹";
Speaking(text,
$"{text}\r\n质量: {quality}");
SendRegisterMessage(text);
}
return;
}
// 指纹识别
{
int ret = zkfp.ZKFP_ERR_OK;
int fid = 0, score = 0;
ret = zkfp2.DBIdentify(_dBHandle, template_buffer, ref fid, ref score);
Debug.WriteLine(string.Format("ret={0}, fid={1}, score={2}", ret, fid, score));
if (score >= _shreshold
// zkfp.ZKFP_ERR_OK == ret
)
{
//textRes.Text = "Identify succ, fid= " + fid + ",score=" + score + "!";
//return;
// 根据 id 取出 barcode 字符串,然后发送给当前焦点 textbox
string strBarcode = (string)_id_barcode_table[fid.ToString()];
//SafeBeep(1);
CapturedEventArgs e1 = new CapturedEventArgs
{
Text = strBarcode,
Score = score,
Quality = quality,
MessageID = NewMessageID(),
CreateTime = DateTime.Now,
};
Light("green");
TriggerCaptured(null, e1);
// SendKeys.SendWait(strBarcode + "\r");
//if (this.BeepOn == false)
// Speak("很好");
// 闪绿灯
//SafeLight("green");
}
else
{
//textRes.Text = "Identify fail, ret= " + ret;
//return;
CapturedEventArgs e1 = new CapturedEventArgs
{
Score = score,
ErrorInfo = $"无法识别, 错误码={ret}",
Quality = quality,
MessageID = NewMessageID(),
CreateTime = DateTime.Now,
};
Light("red");
TriggerCaptured(null, e1);
}
}
}
static int _messageID = 1;
static string NewMessageID()
{
return _messageID++.ToString();
}
bool SendRegisterMessage(string text)
{
return SendTextMessage(
new CapturedEventArgs
{
Text = "register:" + text,
Quality = -1, // 表示这是提示信息,不是识别的号码
MessageID = "?",
});
}
bool SendTextMessage(
// string barcode,
CapturedEventArgs e1)
{
// var is_text = barcode.StartsWith("!");
var now = DateTime.Now;
e1.MessageID = NewMessageID();
e1.CreateTime = now;
TriggerCaptured(null, e1);
return true; // 已经发送
}
// exception:
// 可能会抛出异常。在 token 中断时
public override TextResult GetRegisterString(
Image image,
string strExcludeBarcodes)