当前位置:   article > 正文

C#WinForm-OCRTessnet图像识别_c# ocr识别插件

c# ocr识别插件

基于OCRTessnet图像识别

首先下载OCRTessnet库文件和语言包。点击下载

在项目中添加库文件tessnet2-64的引用

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsApplication6
  11. {
  12. using System.Drawing;
  13. using tessnet2;
  14. public partial class OCRTessnet : Form
  15. {
  16. public string imgPath;
  17. private Bitmap image;
  18. private Tesseract ocr;
  19. public OCRTessnet(string imgPath)
  20. {
  21. InitializeComponent();
  22. this.imgPath = imgPath;
  23. imgPath = @"F:\doc\cLibrary\C#\2.png"; //识别目标图片
  24. image = new Bitmap(imgPath);
  25. //image = new Bitmap(@"C:\OCRTest\number.jpg");
  26. ocr = new Tesseract();
  27. ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only
  28. //@"C:\OCRTest\tessdata" contains the language package, without this the method crash and app breaks
  29. ocr.Init(@"F:\doc\cLibrary\tessdata", "eng", true);//添加语言包引用
  30. pbResult.ImageLocation = imgPath;
  31. }
  32. private void btnTessnet_Click(object sender, EventArgs e)
  33. {
  34. try
  35. {
  36. var result = ocr.DoOCR(image, Rectangle.Empty);
  37. string temp="";
  38. foreach (Word word in result)
  39. temp += word.Text+ "\r\n";
  40. tbResult.Text = temp;
  41. }
  42. catch (Exception exception)
  43. {
  44. }
  45. }
  46. private void pbResult_Click(object sender, EventArgs e)
  47. {
  48. }
  49. private void btnBack_Click(object sender, EventArgs e)
  50. {
  51. backMain();
  52. }
  53. private void backMain()
  54. {
  55. Form1 form1 = new Form1();
  56. this.Hide();
  57. form1.Show();
  58. }
  59. }
  60. }

这里我是在其他窗体跳转到当前窗体,区别在于构造函数多了一个参数imgPath,这里你可以删掉然后写成局部变量。

下边是运行结果


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/620043
推荐阅读
相关标签
  

闽ICP备14008679号