-
Notifications
You must be signed in to change notification settings - Fork 46
/
MainForm.cs
124 lines (110 loc) · 4.58 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using PaddleOCRSharp;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace OcrHelper
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
[DllImport("user32", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, int flags);
OCRStructureResult ocrResult = new OCRStructureResult();
PaddleOCREngine ocrEngine;
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
string resultOld = "";
private void MainForm_Load(object sender, EventArgs e)
{
this.BackColor = Color.Red;
this.TransparencyKey = Color.Red;
SetWindowPos(this.Handle, new IntPtr(-1),this.Left,this.Top,this.Width,this.Height,1|2);
OCRModelConfig config = new OCRModelConfig();
string root = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
string modelPathroot = root + @"\inference";
config.det_infer = modelPathroot + @"\ch_PP-OCRv3_det_infer";
config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
config.rec_infer = modelPathroot + @"\ch_PP-OCRv3_rec_infer";
config.keys = modelPathroot + @"\ppocr_keys.txt";
ocrEngine = new PaddleOCREngine(config, new OCRParameter());
new Thread(() =>
{
while (true)
{
StartOcr(true);
Thread.Sleep(100);
}
})
{ IsBackground = true }.Start();
}
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
public void StartOcr(bool auto = false)
{
if (!checkBox_Auto.Checked && auto) { return; }
Image image = new Bitmap(panel_Image.Width, panel_Image.Height);
Graphics graph = Graphics.FromImage(image);
graph.CopyFromScreen(new Point(this.Left, this.Top + (this.Height - this.ClientSize.Height)), new Point(0, 0), new Size(panel_Image.Width, panel_Image.Height - (this.Height - this.ClientSize.Height)));
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ocrResult = ocrEngine.DetectStructure(image);
List<TextBlock> textBlocks = ocrResult.TextBlocks;
string result = "";
for (int i = 0; i < textBlocks.Count; i++)
{
result += textBlocks[i].Text + "\r\n";
}
if (checkBox_Append.Checked)
{
if (resultOld != result)
{
this.BeginInvoke(new Action(() =>
{
richTextBox_Result.Text += result;
if (!richTextBox_Result.Focused)
{
richTextBox_Result.SelectionStart = richTextBox_Result.Text.Length;
richTextBox_Result.ScrollToCaret();
}
}));
resultOld = result;
}
}
else
{
this.BeginInvoke(new Action(() =>
{
richTextBox_Result.Text = result;
}));
}
}
private void button_StartOcr_Click(object sender, EventArgs e)
{
StartOcr(false);
}
private void checkBox_Auto_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_Auto.Checked) { button_StartOcr.Enabled = false; }
else {
button_StartOcr.Enabled = true;
}
}
private void linkLabel_Source_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = "https://github.com/xksoft/OcrHelper" });
}
}
}