123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows;
- using Path = System.IO.Path;
- using System.Diagnostics;
- using IDCardRecognitionLibs;
- //using Emgu.CV.Ocl;
- using IDCardRecognitionLibs.IDCardRecognitionCore;
- using System.Threading;
- using System.Windows.Threading;
- using System.Threading.Tasks;
- //using static Emgu.CV.Stitching.Stitcher;
- namespace IDInfoExtraction
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- #region private
- // 本地图像数据
- private Bitmap _localImage;
- private Bitmap _detectImage;
- private List<string> _textAll = new List<string>();
- private IDCardRecognition _cardInfor;
- private IDCardRecognition _idCardRecognition;
- #endregion private
- public MainWindow()
- {
- InitializeComponent();
- string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- // 创建 IDCardRecognition 实例
- _cardInfor = new IDCardRecognition(dir);
- }
- /// <summary>
- /// 用户关闭窗口
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- _cardInfor.Dispose();
- }
- private void OnBtnLoadVideoClick(object sender, RoutedEventArgs e)
- {
- // 清空窗口上内容
- MyImageCanvas.ClearCanvas();
- var ofd = new Microsoft.Win32.OpenFileDialog();
- ofd.Filter = "图像|*.png;*.bmp;*.jpg;*.jpeg|所有文件|*.*";
- ofd.Multiselect = true;
- ofd.Title = "选择一幅图像文件";
- if (ofd.ShowDialog() == true)
- {
- var fileNames = ofd.FileNames;
- if (fileNames.Length == 0)
- {
- System.Windows.MessageBox.Show("需选择图像,请重新选择!");
- return;
- }
- foreach (var fileName in fileNames)
- {
- // 先判断是否为有效文件
- var fileExtension = Path.GetExtension(fileName);
- if (fileExtension != ".png" && fileExtension != ".bmp" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".pcd" && fileExtension != ".xyz" && fileExtension != ".txt")
- {
- throw new Exception(" Invalied file.");
- }
- // 判断是否有图像文件
- if (fileExtension == ".png" || fileExtension == ".bmp" || fileExtension == ".jpg" || fileExtension == ".jpeg")
- {
- _localImage = new Bitmap(fileName);
- MyImageCanvas.UpdateRGBImage(_localImage);
- _detectImage = _localImage.Clone(new Rectangle(0,0, _localImage.Width, _localImage.Height), _localImage.PixelFormat);
- }
- }
- }
- }
- public void OnBtnImgOCRClick(object sender, RoutedEventArgs e)
- {
- // 单次测试
- //TextBoxClear();
- //MyImageCanvas.ClearCanvas();
- //_detectImage = _localImage;
- //IdCardRecogResult textResult= _cardInfor.EvaluateOneImage(_localImage);
- ////可视化
- //TextName.Text = "姓名:" + textResult.Name;
- //TextGender.Text = "性别:" + textResult.Gender;
- //TextNation.Text = "民族:" + textResult.Nation;
- //TextBirthdate.Text = "出生:" + textResult.Birthdate;
- //TextAddress.Text = "地址:" + textResult.Address;
- //TextIdNumber.Text = "公民身份号码:" + textResult.IdNumber;
- //TextBoxPoint.Text = textResult.TimeSpan.ToString();
- // 循环测试
- Dispatcher.Invoke(() =>
- {
- KeepRun();
- });
- }
- private async void KeepRun()
- {
- while (true)
- {
- MyImageCanvas.UpdateRGBImage(_detectImage);
- IDCardRecogResult textResult = _cardInfor.EvaluateOneImage(_detectImage);
- Dispatcher.Invoke(() =>
- {
- //可视化
- TextName.Text = "姓名:" + textResult.Name;
- TextGender.Text = "性别:" + textResult.Gender;
- TextNation.Text = "民族:" + textResult.Nation;
- TextBirthdate.Text = "出生:" + textResult.Birthdate;
- TextAddress.Text = "地址:" + textResult.Address;
- TextIdNumber.Text = "公民身份号码:" + textResult.IdNumber;
- TextBoxPoint.Text = textResult.TimeSpan.ToString();
- });
- await Task.Delay(500); // 使用异步等待而不是 Thread.Sleep
- MyImageCanvas.ClearCanvas();
- }
- }
- public void TextBoxClear()
- {
- // 清除耗时显示
- Dispatcher.Invoke(() =>
- {
- TextName.Text = string.Empty;
- TextGender.Text = string.Empty;
- TextNation.Text = string.Empty;
- TextBirthdate.Text = string.Empty;
- TextAddress.Text = string.Empty;
- TextIdNumber.Text = string.Empty;
- });
- }
- }
- }
|