123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Drawing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using Tesseract;
- namespace TesseractScreenReader
- {
- public partial class TsrMainForm : Form
- {
- private readonly string _origWindowTitle;
- private DateTime _lastClipboard = DateTime.Now;
- private string AppendTitle
- {
- set => Text = $"{_origWindowTitle} - {value}";
- }
- public TsrMainForm()
- {
- InitializeComponent();
- _origWindowTitle = Text;
- AppendTitle = "就绪";
- }
- private void ReadClipBoardPicture_Click(object sender, EventArgs e)
- {
- using var img = Clipboard.GetImage();
- OcrImage(img);
- }
- private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e) => TopMost = TopMostCheckBox.Checked;
- private void ClearButton_Click(object sender, EventArgs e) => OutputRichTextBox.Clear();
- private void OcrImage(Image img)
- {
- AppendTitle = "正在处理…";
- try
- {
- if (null == img) throw new InvalidOperationException("无图片");
- 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");
- using var upscale = new Bitmap(img, img.Width * 2, img.Height * 2);
- using (var tess = new TesseractEngine(@".", "chi_sim"))
- {
- tess.SetVariable("preserve_interword_spaces", SpaceCheckBox.Checked ? 1 : 0);
- var result = tess.Process((Bitmap)upscale, PageSegMode.Auto);
- OutputRichTextBox.Text =
- "========== " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff") + " =========="
- + Environment.NewLine + result.GetText().Trim()
- + Environment.NewLine + OutputRichTextBox.Text
- ;
- }
- }
- catch (Exception exception)
- {
- OutputRichTextBox.Text =
- "========== " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ff") + " =========="
- + Environment.NewLine + "错误:" + exception.Message
- + Environment.NewLine + OutputRichTextBox.Text
- ;
- }
- finally
- {
- AppendTitle = "就绪";
- }
- }
- private void WatchingClipBoard_CheckedChanged(object sender, EventArgs e)
- {
- if (WatchingClipBoard.Checked) AddClipboardFormatListener(Handle);
- else RemoveClipboardFormatListener(Handle);
- }
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m);
- if (m.Msg == WM_CLIPBOARDUPDATE && (DateTime.Now - _lastClipboard).TotalSeconds >= 1)
- {
- OcrImage(Clipboard.GetImage());
- _lastClipboard = DateTime.Now;
- }
- }
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool AddClipboardFormatListener(IntPtr hwnd);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
- private const int WM_CLIPBOARDUPDATE = 0x031D;
- }
- }
|