1
0

MainWindow.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows;
  5. using Path = System.IO.Path;
  6. using System.Diagnostics;
  7. using IDCardRecognitionLibs;
  8. //using Emgu.CV.Ocl;
  9. using IDCardRecognitionLibs.IDCardRecognitionCore;
  10. using System.Threading;
  11. using System.Windows.Threading;
  12. using System.Threading.Tasks;
  13. //using static Emgu.CV.Stitching.Stitcher;
  14. namespace IDInfoExtraction
  15. {
  16. /// <summary>
  17. /// MainWindow.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class MainWindow : Window
  20. {
  21. #region private
  22. // 本地图像数据
  23. private Bitmap _localImage;
  24. private Bitmap _detectImage;
  25. private List<string> _textAll = new List<string>();
  26. private IDCardRecognition _cardInfor;
  27. private IDCardRecognition _idCardRecognition;
  28. #endregion private
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  33. // 创建 IDCardRecognition 实例
  34. _cardInfor = new IDCardRecognition(dir);
  35. }
  36. /// <summary>
  37. /// 用户关闭窗口
  38. /// </summary>
  39. /// <param name="sender"></param>
  40. /// <param name="e"></param>
  41. private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
  42. {
  43. _cardInfor.Dispose();
  44. }
  45. private void OnBtnLoadVideoClick(object sender, RoutedEventArgs e)
  46. {
  47. // 清空窗口上内容
  48. MyImageCanvas.ClearCanvas();
  49. var ofd = new Microsoft.Win32.OpenFileDialog();
  50. ofd.Filter = "图像|*.png;*.bmp;*.jpg;*.jpeg|所有文件|*.*";
  51. ofd.Multiselect = true;
  52. ofd.Title = "选择一幅图像文件";
  53. if (ofd.ShowDialog() == true)
  54. {
  55. var fileNames = ofd.FileNames;
  56. if (fileNames.Length == 0)
  57. {
  58. System.Windows.MessageBox.Show("需选择图像,请重新选择!");
  59. return;
  60. }
  61. foreach (var fileName in fileNames)
  62. {
  63. // 先判断是否为有效文件
  64. var fileExtension = Path.GetExtension(fileName);
  65. if (fileExtension != ".png" && fileExtension != ".bmp" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".pcd" && fileExtension != ".xyz" && fileExtension != ".txt")
  66. {
  67. throw new Exception(" Invalied file.");
  68. }
  69. // 判断是否有图像文件
  70. if (fileExtension == ".png" || fileExtension == ".bmp" || fileExtension == ".jpg" || fileExtension == ".jpeg")
  71. {
  72. _localImage = new Bitmap(fileName);
  73. MyImageCanvas.UpdateRGBImage(_localImage);
  74. _detectImage = _localImage.Clone(new Rectangle(0,0, _localImage.Width, _localImage.Height), _localImage.PixelFormat);
  75. }
  76. }
  77. }
  78. }
  79. public void OnBtnImgOCRClick(object sender, RoutedEventArgs e)
  80. {
  81. // 单次测试
  82. //TextBoxClear();
  83. //MyImageCanvas.ClearCanvas();
  84. //_detectImage = _localImage;
  85. //IdCardRecogResult textResult= _cardInfor.EvaluateOneImage(_localImage);
  86. ////可视化
  87. //TextName.Text = "姓名:" + textResult.Name;
  88. //TextGender.Text = "性别:" + textResult.Gender;
  89. //TextNation.Text = "民族:" + textResult.Nation;
  90. //TextBirthdate.Text = "出生:" + textResult.Birthdate;
  91. //TextAddress.Text = "地址:" + textResult.Address;
  92. //TextIdNumber.Text = "公民身份号码:" + textResult.IdNumber;
  93. //TextBoxPoint.Text = textResult.TimeSpan.ToString();
  94. // 循环测试
  95. Dispatcher.Invoke(() =>
  96. {
  97. KeepRun();
  98. });
  99. }
  100. private async void KeepRun()
  101. {
  102. while (true)
  103. {
  104. MyImageCanvas.UpdateRGBImage(_detectImage);
  105. IDCardRecogResult textResult = _cardInfor.EvaluateOneImage(_detectImage);
  106. Dispatcher.Invoke(() =>
  107. {
  108. //可视化
  109. TextName.Text = "姓名:" + textResult.Name;
  110. TextGender.Text = "性别:" + textResult.Gender;
  111. TextNation.Text = "民族:" + textResult.Nation;
  112. TextBirthdate.Text = "出生:" + textResult.Birthdate;
  113. TextAddress.Text = "地址:" + textResult.Address;
  114. TextIdNumber.Text = "公民身份号码:" + textResult.IdNumber;
  115. TextBoxPoint.Text = textResult.TimeSpan.ToString();
  116. });
  117. await Task.Delay(500); // 使用异步等待而不是 Thread.Sleep
  118. MyImageCanvas.ClearCanvas();
  119. }
  120. }
  121. public void TextBoxClear()
  122. {
  123. // 清除耗时显示
  124. Dispatcher.Invoke(() =>
  125. {
  126. TextName.Text = string.Empty;
  127. TextGender.Text = string.Empty;
  128. TextNation.Text = string.Empty;
  129. TextBirthdate.Text = string.Empty;
  130. TextAddress.Text = string.Empty;
  131. TextIdNumber.Text = string.Empty;
  132. });
  133. }
  134. }
  135. }