-
Notifications
You must be signed in to change notification settings - Fork 22
/
OcrManager.cs
254 lines (217 loc) · 8.1 KB
/
OcrManager.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
namespace WinForm_Ollama_Copilot
{
internal class OcrManager
{
Form1 _mForm1 = null;
public int _mWidth = 0;
public int _mHeight = 0;
public bool _mMouseDown = false;
public bool _mMouseOver = false;
public Point _mMouseMoveStart = Point.Empty;
public Point _mMouseMoveEnd = Point.Empty;
public Point _mMouseMoveOffset = Point.Empty;
private Image _mCaptureImage = null;
private readonly HttpClient _mClient = new HttpClient();
private bool _mIsRecognizing = false;
public void LoadConfig(Form1 form1)
{
_mForm1 = form1;
int x;
int y;
int.TryParse(_mForm1.TxtX.Text, out x);
int.TryParse(_mForm1.TxtY.Text, out y);
_mMouseMoveStart = new Point(x, y);
}
public void Uninit()
{
if (_mCaptureImage != null)
{
_mCaptureImage.Dispose();
_mCaptureImage = null;
}
}
public void Init(int width, int height)
{
if (_mCaptureImage == null)
{
_mWidth = width;
_mHeight = height;
_mCaptureImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}
}
#region Input Events
public void SetInputEvents(PictureBox pictureBox)
{
pictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureMouseDown);
pictureBox.MouseEnter += new System.EventHandler(this.PictureMouseEnter);
pictureBox.MouseLeave += new System.EventHandler(this.PictureMouseLeave);
pictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureMouseMove);
pictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureMouseUp);
}
public void PictureMouseDown(object sender, MouseEventArgs e)
{
if (!_mMouseDown && _mMouseOver)
{
_mMouseDown = true;
_mMouseMoveStart = new Point(e.X + _mMouseMoveOffset.X, e.Y + _mMouseMoveOffset.Y);
_mMouseMoveOffset = Point.Empty;
}
}
public void PictureMouseUp(object sender, MouseEventArgs e)
{
if (_mMouseDown)
{
_mMouseMoveEnd = new Point(e.X + _mMouseMoveOffset.X, e.Y + _mMouseMoveOffset.Y);
_mMouseMoveOffset = new Point(_mMouseMoveStart.X - _mMouseMoveEnd.X, _mMouseMoveStart.Y - _mMouseMoveEnd.Y);
}
_mMouseDown = false;
}
public void PictureMouseMove(object sender, MouseEventArgs e)
{
if (_mMouseDown)
{
_mMouseMoveEnd = new Point(e.X + _mMouseMoveOffset.X, e.Y + _mMouseMoveOffset.Y);
_mForm1.TxtX.Text = (_mMouseMoveStart.X - _mMouseMoveEnd.X).ToString();
_mForm1.TxtY.Text = (_mMouseMoveStart.Y - _mMouseMoveEnd.Y).ToString();
}
}
public void PictureMouseEnter(object sender, EventArgs e)
{
_mMouseOver = true;
}
public void PictureMouseLeave(object sender, EventArgs e)
{
_mMouseOver = false;
}
#endregion Input Events
private string CaptureBase64String(PictureBox pictureBox)
{
string base64String = string.Empty;
Graphics captureGraphics = null;
Graphics g = null;
Bitmap captureBitmap = null;
Brush brushCapture = null;
Pen pen = null;
try
{
// create graphics
captureGraphics = Graphics.FromImage(_mCaptureImage);
// copy pixels from screen
captureGraphics.CopyFromScreen(
_mMouseMoveStart.X - _mMouseMoveEnd.X,
_mMouseMoveStart.Y - _mMouseMoveEnd.Y,
0,
0,
new Size(_mWidth, _mHeight));
// do some cropping
g = pictureBox.CreateGraphics();
// extract the image
captureBitmap = new Bitmap(_mCaptureImage);
// convert to base64 string
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
captureBitmap.Save(ms, ImageFormat.Jpeg);
byte[] byteImage = ms.ToArray();
base64String = Convert.ToBase64String(byteImage);
}
g.DrawImage(_mCaptureImage, 0, 0, pictureBox.Width, pictureBox.Height);
}
catch (Exception ex)
{
Console.Error.WriteLine("Failed to capture image exception: {0}", ex);
}
finally
{
// cleanup
if (pen != null)
{
pen.Dispose();
}
if (brushCapture != null)
{
brushCapture.Dispose();
}
if (captureBitmap != null)
{
captureBitmap.Dispose();
}
if (g != null)
{
g.Dispose();
}
if (captureGraphics != null)
{
captureGraphics.Dispose();
}
}
return base64String;
}
public class ResultOCR
{
public string _mText = null;
public string _mError = null;
}
private async Task<ResultOCR> Base64ImageToString(string base64String)
{
ResultOCR result = new ResultOCR();
try
{
HttpRequestMessage httpRequestMessage =
new HttpRequestMessage(HttpMethod.Post, "http://localhost:11439/image_to_string");
JObject jobject = new JObject();
jobject["data"] = base64String;
string pJsonContent = jobject.ToString();
HttpContent httpContent = new StringContent(pJsonContent, Encoding.UTF8, "application/json");
httpRequestMessage.Content = httpContent;
var productValue = new ProductInfoHeaderValue("OcrManager_Client", "1.0");
var commentValue = new ProductInfoHeaderValue("(+http://localhost:11439/image_to_string)");
httpRequestMessage.Headers.UserAgent.Add(productValue);
httpRequestMessage.Headers.UserAgent.Add(commentValue);
var response = await _mClient.SendAsync(httpRequestMessage);
if (response != null)
{
using (HttpContent content = response.Content)
{
string text = await content.ReadAsStringAsync();
try
{
JObject responseJson = JObject.Parse(text);
result._mText = responseJson["result"].ToString();
}
catch
{
}
}
}
}
catch
{
result._mError = "Failed to reach OCR server. Is the server running?";
}
return result;
}
public async Task<ResultOCR> GetTextFromScreen(PictureBox pictureBox)
{
string base64String = CaptureBase64String(pictureBox);
if (_mIsRecognizing)
{
return null;
}
_mIsRecognizing = true;
ResultOCR result = await Base64ImageToString(base64String);
_mIsRecognizing = false;
return result;
}
}
}