基本信息
源码名称:C#开发的OCR实例,用Tesseract实现
源码大小:1.37M
文件格式:.zip
开发语言:C#
更新时间:2013-01-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
OCR之Tesseract
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { #region 全局变量 private string __OutputFileName = string.Empty; private WebClient client = new WebClient(); #endregion #region 系统函数 public MainWindow() { InitializeComponent(); } #endregion #region 用户函数 private bool fn数据验证() { if ((bool)this.rbtn本地图片.IsChecked) { if (!File.Exists(this.txt本地图片.Text)) { this.labMsg.Content = "本地图片不存在,请重新选择。"; this.txt本地图片.Focus(); return true; } } if ((bool)this.rbtn网络图片.IsChecked) { Regex reg = new Regex(@"^https?://([\w-] \.) [\w-] (/[\w- ./?%&=]*)?$"); if (!reg.IsMatch(this.txt网络图片.Text)) { this.labMsg.Content = "网络图片路径错误,请在浏览器中测试能否打开该图片。"; this.txt网络图片.Focus(); return true; } } if (!Directory.Exists(this.txt输出目录.Text)) { this.labMsg.Content = "输出目录不存在,请重新选择。"; this.txt输出目录.Focus(); return true; } return false; } private void fnStartDownload(string v_strImgPath, string v_strOutputDir, out string v_strTmpPath) { int n = v_strImgPath.LastIndexOf('/'); string URLAddress = v_strImgPath.Substring(0, n); string fileName = v_strImgPath.Substring(n 1, v_strImgPath.Length - n - 1); this.__OutputFileName = v_strOutputDir "\\" fileName.Substring(0, fileName.LastIndexOf(".")); if (!Directory.Exists(System.Configuration.ConfigurationManager.AppSettings["tmpPath"])) { Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["tmpPath"]); } string Dir = System.Configuration.ConfigurationManager.AppSettings["tmpPath"]; v_strTmpPath = Dir "\\" fileName; WebRequest myre = WebRequest.Create(URLAddress); client.DownloadFile(v_strImgPath, v_strTmpPath); //Stream str = client.OpenRead(v_strImgPath); //StreamReader reader = new StreamReader(str); //byte[] mbyte = new byte[Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxDownloadImgLength"])]; //int allmybyte = (int)mbyte.Length; //int startmbyte = 0; //while (allmybyte > 0) //{ // int m = str.Read(mbyte, startmbyte, allmybyte); // if (m == 0) // { // break; // } // startmbyte = m; // allmybyte -= m; //} //FileStream fstr = new FileStream(v_strTmpPath, FileMode.Create, FileAccess.Write); //fstr.Write(mbyte, 0, startmbyte); //str.Close(); //fstr.Close(); } private void fnOCR(string v_strTesseractPath, string v_strSourceImgPath, string v_strOutputPath, string v_strLangPath) { using (Process process = new System.Diagnostics.Process()) { process.StartInfo.FileName = v_strTesseractPath; process.StartInfo.Arguments = v_strSourceImgPath " " v_strOutputPath " -l " v_strLangPath; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.WaitForExit(); } } #endregion #region 系统事件 private void btn浏览_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "请选择一个本地图片"; ofd.Multiselect = false; ofd.Filter = "支持图片格式(*.jpg,*.jpeg,*.gif,*.bmp,*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.txt本地图片.Text = ofd.FileName; this.img图片.Source = new BitmapImage(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute)); this.labMsg.Content = "本地图片已成功加载。"; } } private void rbtn本地图片_Checked(object sender, RoutedEventArgs e) { if (this.IsInitialized) { if ((bool)this.rbtn本地图片.IsChecked) { this.txt本地图片.Text = string.Empty; this.txt本地图片.IsEnabled = true; this.btn浏览.IsEnabled = true; this.txt本地图片.Focus(); this.txt网络图片.Text = "http://"; this.txt网络图片.IsEnabled = false; this.img图片.Source = null; } } } private void rbtn网络图片_Checked(object sender, RoutedEventArgs e) { if (this.IsInitialized) { if ((bool)this.rbtn网络图片.IsChecked) { this.txt本地图片.Text = string.Empty; this.txt本地图片.IsEnabled = false; this.btn浏览.IsEnabled = false; this.txt网络图片.Text = "http://"; this.txt网络图片.IsEnabled = true; this.img图片.Source = null; } } } private void txt网络图片_TextChanged(object sender, TextChangedEventArgs e) { Regex reg = new Regex(@"^https?://([\w-] \.) [\w-] (/[\w- ./?%&=]*)?$"); if (reg.IsMatch(this.txt网络图片.Text)) { this.img图片.Source = new BitmapImage(new Uri(this.txt网络图片.Text, UriKind.RelativeOrAbsolute)); this.labMsg.Content = "网络图片已成功加载。"; } } private void btn输出浏览_Click(object sender, RoutedEventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "请选择一个输出目录"; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.txt输出目录.Text = fbd.SelectedPath; } } private void btn清空_Click(object sender, RoutedEventArgs e) { this.txtOCRed.Text = string.Empty; this.txtOCRed.Focus(); } private void btnOCR_Click(object sender, RoutedEventArgs e) { if (this.fn数据验证()) { return; } try { DirectoryInfo dir = new DirectoryInfo(this.txt输出目录.Text); if ((bool)this.rbtn本地图片.IsChecked) { FileInfo file = new FileInfo(this.txt本地图片.Text); string name = file.Name.Substring(0, file.Name.LastIndexOf(".")); this.__OutputFileName = dir.FullName @"\" name; this.fnOCR(System.Configuration.ConfigurationManager.AppSettings["TesseractPath"] , this.txt本地图片.Text, this.__OutputFileName , System.Configuration.ConfigurationManager.AppSettings["LangPath"]); } else if ((bool)this.rbtn网络图片.IsChecked) { string TmpPath = string.Empty; this.fnStartDownload(this.txt网络图片.Text, dir.FullName, out TmpPath); this.fnOCR(System.Configuration.ConfigurationManager.AppSettings["TesseractPath"] , TmpPath, this.__OutputFileName , System.Configuration.ConfigurationManager.AppSettings["LangPath"]); } FileStream fs = new FileStream(this.__OutputFileName ".txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); this.txtOCRed.Text = sr.ReadToEnd(); sr.Close(); this.labMsg.Content = "OCR成功。"; } catch { this.labMsg.Content = "OCR失败。"; } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { DirectoryInfo dir = new DirectoryInfo("tmp"); foreach (FileInfo file in dir.GetFiles()) { file.Delete(); } } #endregion }