Recognition.cs
12.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace BLL
{
/// <summary>
/// Optical Character Recognition
/// </summary>
public class OCR
{
private PictureBox _pic;
private bool moveImage; //true 移动图像 false 矩形选择
private CodeSplice.BarCode _code;
private Tesseract.TesseractEngine eng;
private Point oldPoint = new Point();
private PointF codeCenter = new PointF();
private int index;
private Bitmap ocrRotate; //旋转后的图片
private Rectangle ocrTemp; //临时
private List<Rectangle> ocrRect; //ocr红框的位置
private List<string> ocrText; //ocr文本
private bool ocrDraw = false; //ocr画框
private Point ocrOffset = new Point(); //偏移
private readonly Pen PEN_SELECT = new Pen(Color.Red, 2);
private readonly Pen PEN_RECT = new Pen(Color.PaleVioletRed, 1);
private readonly Pen PEN_DRAW = new Pen(Color.Black, 2);
private readonly Font FONT = new Font("宋体", 12f);
/// <summary>
/// ocr识别
/// </summary>
/// <param name="pic"></param>
public OCR(PictureBox pic)
{
try
{
eng = new Tesseract.TesseractEngine("./tessdata", "eng", Tesseract.EngineMode.TesseractAndCube);
}
catch (Exception ex)
{
eng = null;
CodeSplice.Common.Log.OutError(ex.ToString());
}
index = -1;
ocrRect = new List<Rectangle>();
ocrText = new List<string>();
_pic = pic;
pic.MouseMove += Pic_MouseMove;
pic.MouseDown += Pic_MouseDown;
pic.MouseUp += Pic_MouseUp;
pic.Paint += Pic_Paint;
pic.Resize += Pic_Resize;
}
public Bitmap Image { set; get; }
public void SetCode(CodeSplice.BarCode code)
{
_code = code;
ocrOffset = new Point();
//ocrRect.Clear();
index = -1;
CalcImage();
_pic.Refresh();
}
public void SelectMode()
{
moveImage = false;
_pic.Cursor = Cursors.Cross;
}
public void MoveMode()
{
moveImage = true;
_pic.Cursor = Cursors.SizeAll;
}
public void DelSelect()
{
if (index > -1)
{
ocrRect.RemoveAt(index);
ocrText.RemoveAt(index);
index = -1;
_pic.Refresh();
}
}
public void DelAll()
{
ocrRect.Clear();
ocrText.Clear();
index = -1;
_pic.Refresh();
}
public Rectangle[] GetRect()
{
Rectangle[] rect = new Rectangle[ocrRect.Count];
for (int i = 0; i < rect.Length; i++)
{
rect[i] = new Rectangle(Convert.ToInt32(ocrRect[i].X - codeCenter.X), Convert.ToInt32(ocrRect[i].Y - codeCenter.Y), ocrRect[i].Width, ocrRect[i].Height);
}
return rect;
}
public void SetRect(Rectangle[] rect)
{
ocrRect.Clear();
ocrText.Clear();
for (int i = 0; i < rect.Length; i++)
{
rect[i].X = Convert.ToInt32(rect[i].X + codeCenter.X);
rect[i].Y = Convert.ToInt32(rect[i].Y + codeCenter.Y);
Bitmap bmp = new Bitmap(rect[i].Width, rect[i].Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(ocrRotate, new Rectangle(0, 0, bmp.Width, bmp.Height), rect[i], GraphicsUnit.Pixel);
g.Save();
g.Dispose();
if (eng == null)
{
ocrText.Add("");
}
else
{
Tesseract.Page page = eng.Process(bmp);
string s = page.GetText();
page.Dispose();
ocrText.Add(s.Trim('\n'));
}
}
ocrRect.AddRange(rect);
_pic.Refresh();
}
private void CalcImage()
{
PointF pt = _code.Center;
double aa = Math.Atan2(pt.Y, pt.X);
aa = aa * 180 / Math.PI; //转角度
double cc = Math.Sqrt(pt.X * pt.X + pt.Y * pt.Y);
aa += _code.Angle;
aa = aa * Math.PI / 180; //转弧度
double x1 = Math.Cos(aa) * cc;
double y1 = Math.Sin(aa) * cc;
int w = _pic.ClientSize.Width;
int h = _pic.ClientSize.Height;
ocrRotate = new Bitmap(w, h);
Graphics g = Graphics.FromImage(ocrRotate);
codeCenter.X = w / 2f + ocrOffset.X;
codeCenter.Y = h / 2f + ocrOffset.Y;
float dx = Convert.ToSingle(x1 - codeCenter.X);
float dy = Convert.ToSingle(y1 - codeCenter.Y);
g.TranslateTransform(-dx, -dy);
g.RotateTransform(_code.Angle);
RectangleF src = new RectangleF(0, 0, Image.Width, Image.Height);
g.DrawImage(Image, src, src, GraphicsUnit.Pixel);
g.ResetTransform();
g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y - 10, codeCenter.X + 10, codeCenter.Y + 10);
g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y + 10, codeCenter.X + 10, codeCenter.Y - 10);
g.Save();
g.Dispose();
}
private void Pic_Resize(object sender, EventArgs e)
{
}
private void Pic_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (moveImage)
{
ocrOffset.X += e.X - oldPoint.X;
ocrOffset.Y += e.Y - oldPoint.Y;
for (int i = 0; i < ocrRect.Count; i++)
{
ocrRect[i] = new Rectangle(ocrRect[i].X + e.X - oldPoint.X, ocrRect[i].Y + e.Y - oldPoint.Y, ocrRect[i].Width, ocrRect[i].Height);
}
oldPoint = e.Location;
CalcImage();
}
else
{
if (index > -1)
{
ocrRect[index] = new Rectangle(ocrRect[index].X + e.X - oldPoint.X, ocrRect[index].Y + e.Y - oldPoint.Y, ocrRect[index].Width, ocrRect[index].Height);
oldPoint = e.Location;
}
else
{
if (e.Location.X > oldPoint.X)
{
ocrTemp.X = oldPoint.X;
ocrTemp.Width = e.Location.X - oldPoint.X;
}
else
{
ocrTemp.X = e.Location.X;
ocrTemp.Width = oldPoint.X - e.Location.X;
}
if (e.Location.Y > oldPoint.Y)
{
ocrTemp.Y = oldPoint.Y;
ocrTemp.Height = e.Location.Y - oldPoint.Y;
}
else
{
ocrTemp.Y = e.Location.Y;
ocrTemp.Height = oldPoint.Y - e.Location.Y;
}
ocrDraw = true;
}
}
_pic.Refresh();
}
//else
//{
// if (!moveImage)
// {
// bool find = false;
// for (int i = 0; i < ocrRect.Count; i++)
// {
// if (ocrRect[i].Contains(e.Location))
// {
// find = true;
// if (index != i)
// {
// ocrInside = true;
// index = i;
// _pic.Cursor = Cursors.Hand;
// _pic.Refresh();
// break;
// }
// }
// }
// if (!find)
// {
// if (ocrInside)
// {
// ocrInside = false;
// index = -1;
// _pic.Cursor = Cursors.Cross;
// _pic.Refresh();
// }
// }
// }
//}
}
private void Pic_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
oldPoint = e.Location;
ocrTemp = new Rectangle(e.X, e.Y, 0, 0);
if (!moveImage)
{
bool find = false;
for (int i = 0; i < ocrRect.Count; i++)
{
if (ocrRect[i].Contains(e.Location))
{
find = true;
if (index != i)
{
index = i;
_pic.Cursor = Cursors.Hand;
_pic.Refresh();
break;
}
}
}
if (!find)
{
if (index > -1)
{
index = -1;
_pic.Cursor = Cursors.Cross;
_pic.Refresh();
}
}
}
}
}
private void Pic_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (ocrDraw)
{
//ocr识别
if (ocrTemp.Width == 0 || ocrTemp.Height == 0) return;
ocrRect.Add(ocrTemp);
index = ocrRect.Count - 1;
ocrDraw = false;
Bitmap bmp = new Bitmap(ocrTemp.Width, ocrTemp.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(ocrRotate, new Rectangle(0, 0, bmp.Width, bmp.Height), ocrTemp, GraphicsUnit.Pixel);
g.Save();
g.Dispose();
if (eng == null)
{
ocrText.Add("");
}
else
{
Tesseract.Page page = eng.Process(bmp);
string s = page.GetText();
page.Dispose();
ocrText.Add(s.Trim('\n'));
}
}
else if (index > -1)
{
Bitmap bmp = new Bitmap(ocrRect[index].Width, ocrRect[index].Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(ocrRotate, new Rectangle(0, 0, bmp.Width, bmp.Height), ocrRect[index], GraphicsUnit.Pixel);
g.Save();
g.Dispose();
if (eng == null)
{
ocrText[index] = "";
}
else
{
Tesseract.Page page = eng.Process(bmp);
string s = page.GetText();
page.Dispose();
ocrText[index] = s.Trim('\n');
}
}
_pic.Refresh();
}
}
private void Pic_Paint(object sender, PaintEventArgs e)
{
if (ocrRotate == null) return;
e.Graphics.DrawImage(ocrRotate, 0, 0);
for (int i = 0; i < ocrRect.Count; i++)
{
e.Graphics.DrawRectangle(i == index ? PEN_SELECT : PEN_RECT, ocrRect[i]);
SizeF sf = e.Graphics.MeasureString(ocrText[i], FONT);
e.Graphics.FillRectangle(Brushes.White, ocrRect[i].X, ocrRect[i].Bottom, sf.Width, sf.Height); //白色背景
e.Graphics.DrawString(ocrText[i], FONT, Brushes.Red, ocrRect[i].X, ocrRect[i].Bottom); //红色文本
}
if (ocrDraw)
e.Graphics.DrawRectangle(PEN_DRAW, ocrTemp);
}
}
}