FrmOpenCamera.cs
11.4 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asa.Camera
{
public partial class FrmOpenCamera : Form
{
private int id;
private string name;
public List<CameraRegion> regions;
private float zoom = 0.5f;
private Bitmap bmp;
private int selectIdx;
private Point oldPoint;
private FieldMargin fieldMargin = FieldMargin.Outside;
private const int FIELD_MARGIN = 5;
private readonly Font TEXT_FONT = new("宋体", 12f);
public FrmOpenCamera()
{
InitializeComponent();
}
public FrmOpenCamera(string cameraName, List<CameraRegion> regions) : this()
{
name = cameraName;
this.regions = new();
foreach (CameraRegion reg in regions)
this.regions.Add(reg.Clone());
selectIdx = -1;
Common.visionLib.Open();
bmp = Common.visionLib.GetImage(name);
NudZoom.Value = (decimal)zoom;
}
private void ChangeNameRatio()
{
if (selectIdx == -1)
{
TxtName.Text = "";
NudRatio.Value = 0;
}
else
{
TxtName.Text = regions[selectIdx].RegionName;
NudRatio.Value = (decimal)regions[selectIdx].Ratio;
}
}
private void FindFieldMargin(Point pt)
{
fieldMargin = FieldMargin.Outside;
RectangleF rect = new(regions[selectIdx].X * zoom, regions[selectIdx].Y * zoom, regions[selectIdx].Width * zoom, regions[selectIdx].Height * zoom);
if (Math.Abs(rect.X - pt.X) < FIELD_MARGIN)
{
if (Math.Abs(rect.Y - pt.Y) < FIELD_MARGIN)
fieldMargin = FieldMargin.LeftTop;
else if (Math.Abs(pt.Y - rect.Y - rect.Height) < FIELD_MARGIN)
fieldMargin = FieldMargin.LeftBottom;
else if (pt.Y > rect.Y && pt.Y < rect.Y + rect.Height)
fieldMargin = FieldMargin.Left;
}
else if (Math.Abs(rect.X + rect.Width - pt.X) < FIELD_MARGIN)
{
if (Math.Abs(rect.Y - pt.Y) < FIELD_MARGIN)
fieldMargin = FieldMargin.RightTop;
else if (Math.Abs(pt.Y - rect.Y - rect.Height) < FIELD_MARGIN)
fieldMargin = FieldMargin.RightBottom;
else if (pt.Y > rect.Y && pt.Y < rect.Y + rect.Height)
fieldMargin = FieldMargin.Right;
}
else if (pt.X > rect.X && pt.X < rect.X + rect.Width)
{
if (Math.Abs(rect.Y - pt.Y) < FIELD_MARGIN)
fieldMargin = FieldMargin.Top;
else if (Math.Abs(pt.Y - rect.Y - rect.Height) < FIELD_MARGIN)
fieldMargin = FieldMargin.Bottom;
else if (pt.Y > rect.Y && pt.Y < rect.Y + rect.Height)
fieldMargin = FieldMargin.Inside;
}
switch (fieldMargin)
{
case FieldMargin.Left: PicShow.Cursor = Cursors.SizeWE; break;
case FieldMargin.LeftTop: PicShow.Cursor = Cursors.SizeNWSE; break;
case FieldMargin.LeftBottom: PicShow.Cursor = Cursors.SizeNESW; break;
case FieldMargin.Right: PicShow.Cursor = Cursors.SizeWE; break;
case FieldMargin.RightBottom: PicShow.Cursor = Cursors.SizeNWSE; break;
case FieldMargin.RightTop: PicShow.Cursor = Cursors.SizeNESW; break;
case FieldMargin.Top: PicShow.Cursor = Cursors.SizeNS; break;
case FieldMargin.Bottom: PicShow.Cursor = Cursors.SizeNS; break;
case FieldMargin.Inside: PicShow.Cursor = Cursors.SizeAll; break;
case FieldMargin.Outside: PicShow.Cursor = Cursors.Default; break;
}
}
private void ChangeFieldMargin(int x, int y)
{
switch (fieldMargin)
{
case FieldMargin.Left:
regions[selectIdx].X += x;
regions[selectIdx].Width -= x;
break;
case FieldMargin.Top:
regions[selectIdx].Y += y;
regions[selectIdx].Height -= y;
break;
case FieldMargin.Right:
regions[selectIdx].Width += x;
break;
case FieldMargin.Bottom:
regions[selectIdx].Height += y;
break;
case FieldMargin.LeftTop:
regions[selectIdx].X += x;
regions[selectIdx].Width -= x;
regions[selectIdx].Y += y;
regions[selectIdx].Height -= y;
break;
case FieldMargin.RightTop:
regions[selectIdx].Y += y;
regions[selectIdx].Height -= y;
regions[selectIdx].Width += x;
break;
case FieldMargin.LeftBottom:
regions[selectIdx].X += x;
regions[selectIdx].Width -= x;
regions[selectIdx].Height += y;
break;
case FieldMargin.RightBottom:
regions[selectIdx].Width += x;
regions[selectIdx].Height += y;
break;
case FieldMargin.Inside:
regions[selectIdx].X += x;
regions[selectIdx].Y += y;
break;
}
}
private void FrmOpenCamera_FormClosing(object sender, FormClosingEventArgs e)
{
Common.visionLib.Close();
}
private void BtnGrabOne_Click(object sender, EventArgs e)
{
bmp = Common.visionLib.GetImage(name);
PicShow.Refresh();
}
private void BtnAdd_Click(object sender, EventArgs e)
{
CameraRegion region = new()
{
CameraName = name,
RegionName = "name" + ++id,
X = 0,
Y = 0,
Width = 100,
Height = 100,
Ratio = 0.3f
};
regions.Add(region);
selectIdx = regions.Count - 1;
ChangeNameRatio();
PicShow.Refresh();
}
private void BtnDel_Click(object sender, EventArgs e)
{
if (selectIdx == -1) return;
regions.RemoveAt(selectIdx);
selectIdx = regions.Count == 0 ? -1 : 0;
ChangeNameRatio();
PicShow.Refresh();
}
private void TxtName_TextChanged(object sender, EventArgs e)
{
if (selectIdx == -1) return;
regions[selectIdx].RegionName = TxtName.Text;
PicShow.Refresh();
}
private void NudRatio_ValueChanged(object sender, EventArgs e)
{
if (selectIdx == -1) return;
regions[selectIdx].Ratio = (float)NudRatio.Value;
PicShow.Refresh();
}
private void NudZoom_ValueChanged(object sender, EventArgs e)
{
zoom = (float)NudZoom.Value;
PicShow.Refresh();
}
private void BtnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void BtnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void PicShow_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
oldPoint = e.Location;
if (fieldMargin != FieldMargin.Outside) return;
float x = e.X / zoom; //放大到正常大小
float y = e.Y / zoom; //放大到正常大小
selectIdx = regions.FindIndex(match => match.X < x && match.Y < y && match.X + match.Width > x && match.Y + match.Height > y);
ChangeNameRatio();
if (selectIdx > -1)
FindFieldMargin(e.Location);
PicShow.Refresh();
}
private void PicShow_MouseMove(object sender, MouseEventArgs e)
{
if (selectIdx == -1) return;
if (e.Button == MouseButtons.Left)
{
int x = Convert.ToInt32((e.X - oldPoint.X) / zoom);
int y = Convert.ToInt32((e.Y - oldPoint.Y) / zoom);
ChangeFieldMargin(x, y);
oldPoint = e.Location;
PicShow.Refresh();
}
else if (e.Button == MouseButtons.None)
{
FindFieldMargin(e.Location);
}
}
private void PicShow_MouseUp(object sender, MouseEventArgs e)
{
}
private void PicShow_Paint(object sender, PaintEventArgs e)
{
if (bmp == null) return;
RectangleF destRect = new(0, 0, bmp.Width * zoom, bmp.Height * zoom);
RectangleF srcRect = new(0, 0, bmp.Width, bmp.Height);
e.Graphics.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
float x, y, width, height;
for (int i = 0; i < regions.Count; i++)
{
x = regions[i].X * zoom;
y = regions[i].Y * zoom;
width = regions[i].Width * zoom;
height = regions[i].Height * zoom;
string s = regions[i].ToString();
SizeF sf = e.Graphics.MeasureString(s, TEXT_FONT);
e.Graphics.FillRectangle(Brushes.White, x, y - sf.Height, sf.Width, sf.Height);
e.Graphics.DrawString(s, TEXT_FONT, Brushes.Black, x, y - sf.Height);
if (i == selectIdx)
{
e.Graphics.DrawRectangle(Pens.Red, x, y, width, height);
//4个角的矩形
e.Graphics.FillRectangle(Brushes.White, x - FIELD_MARGIN, y - FIELD_MARGIN, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.FillRectangle(Brushes.White, x + width, y - FIELD_MARGIN, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.FillRectangle(Brushes.White, x - FIELD_MARGIN, y + height, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.FillRectangle(Brushes.White, x + width, y + height, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.DrawRectangle(Pens.Black, x - FIELD_MARGIN, y - FIELD_MARGIN, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.DrawRectangle(Pens.Black, x + width, y - FIELD_MARGIN, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.DrawRectangle(Pens.Black, x - FIELD_MARGIN, y + height, FIELD_MARGIN, FIELD_MARGIN);
e.Graphics.DrawRectangle(Pens.Black, x + width, y + height, FIELD_MARGIN, FIELD_MARGIN);
}
else
{
e.Graphics.DrawRectangle(Pens.LimeGreen, x, y, width, height);
}
}
}
private enum FieldMargin
{
Outside,
Inside,
Left,
Top,
Right,
Bottom,
LeftTop,
RightTop,
LeftBottom,
RightBottom
}
}
}