TsrMainForm.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. using Tesseract;
  7. namespace TesseractScreenReader
  8. {
  9. public partial class TsrMainForm : Form
  10. {
  11. private readonly string _origWindowTitle;
  12. private DateTime _lastClipboard = DateTime.Now;
  13. private string AppendTitle
  14. {
  15. set => Text = $"{_origWindowTitle} - {value}";
  16. }
  17. public TsrMainForm()
  18. {
  19. InitializeComponent();
  20. _origWindowTitle = Text;
  21. AppendTitle = "就绪";
  22. }
  23. private void ReadClipBoardPicture_Click(object sender, EventArgs e)
  24. {
  25. using var img = Clipboard.GetImage();
  26. OcrImage(img);
  27. }
  28. private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e) => TopMost = TopMostCheckBox.Checked;
  29. private void ClearButton_Click(object sender, EventArgs e) => OutputRichTextBox.Clear();
  30. private void OcrImage(Image img)
  31. {
  32. AppendTitle = "正在处理…";
  33. try
  34. {
  35. if (null == img) throw new InvalidOperationException("无图片");
  36. if (false == File.Exists("chi_sim.traineddata")) throw new FileNotFoundException("缺少数据文件,请前 https://github.com/tesseract-ocr/tessdata/blob/master/chi_sim.traineddata 往下载,然后放到exe旁", "chi_sim.traineddata");
  37. using var upscale = new Bitmap(img, img.Width * 2, img.Height * 2);
  38. using (var tess = new TesseractEngine(@".", "chi_sim"))
  39. {
  40. tess.SetVariable("preserve_interword_spaces", SpaceCheckBox.Checked ? 1 : 0);
  41. var result = tess.Process((Bitmap)upscale, PageSegMode.Auto);
  42. OutputRichTextBox.Text =
  43. "========== " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff") + " =========="
  44. + Environment.NewLine + result.GetText().Trim()
  45. + Environment.NewLine + OutputRichTextBox.Text
  46. ;
  47. }
  48. }
  49. catch (Exception exception)
  50. {
  51. OutputRichTextBox.Text =
  52. "========== " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff") + " =========="
  53. + Environment.NewLine + "错误:" + exception.Message
  54. + Environment.NewLine + OutputRichTextBox.Text
  55. ;
  56. }
  57. finally
  58. {
  59. AppendTitle = "就绪";
  60. }
  61. }
  62. private void WatchingClipBoard_CheckedChanged(object sender, EventArgs e)
  63. {
  64. if (WatchingClipBoard.Checked) AddClipboardFormatListener(Handle);
  65. else RemoveClipboardFormatListener(Handle);
  66. }
  67. protected override void WndProc(ref Message m)
  68. {
  69. base.WndProc(ref m);
  70. if (m.Msg == WM_CLIPBOARDUPDATE && (DateTime.Now - _lastClipboard).TotalSeconds >= 1)
  71. {
  72. OcrImage(Clipboard.GetImage());
  73. _lastClipboard = DateTime.Now;
  74. }
  75. }
  76. [DllImport("user32.dll", SetLastError = true)]
  77. [return: MarshalAs(UnmanagedType.Bool)]
  78. private static extern bool AddClipboardFormatListener(IntPtr hwnd);
  79. [DllImport("user32.dll", SetLastError = true)]
  80. [return: MarshalAs(UnmanagedType.Bool)]
  81. private static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
  82. private const int WM_CLIPBOARDUPDATE = 0x031D;
  83. }
  84. }