FrmOpenCamera.cs
14.1 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
using CameraVisionLib.Model;
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 cameraName;
private List<RegionPlan> cameraPlan;
private int planIndex;
private int regionIndex;
private float zoom = 0.5f;
private Bitmap bmp;
private Point oldPoint;
private FieldMargin fieldMargin = FieldMargin.Outside;
private const int FIELD_MARGIN = 5;
private readonly Font TEXT_FONT = new("宋体", 12f);
internal FrmOpenCamera()
{
InitializeComponent();
}
internal FrmOpenCamera(string name) : this()
{
id = 0;
cameraName = name;
planIndex = -1;
regionIndex = -1;
if (Common.visionLib.regionLists.TryGetValue(name, out cameraPlan))
{
for (int i = 0; i < cameraPlan.Count; i++)
LstPlan.Items.Add(cameraPlan[i].Name);
}
Common.visionLib.Open(name);
bmp = Common.visionLib.GetImage(name);
NudZoom.Value = (decimal)zoom;
}
private void ChangeNameRatio()
{
if (regionIndex == -1)
{
TxtName.Text = "";
NudRatio.Value = 0;
}
else
{
TxtName.Text = cameraPlan[planIndex].Info[regionIndex].Name;
NudRatio.Value = Convert.ToDecimal(cameraPlan[planIndex].Info[regionIndex].Region.Ratio);
}
}
private void FindFieldMargin(Point pt)
{
fieldMargin = FieldMargin.Outside;
RectangleF rect = new(cameraPlan[planIndex].Info[regionIndex].Region.X * zoom, cameraPlan[planIndex].Info[regionIndex].Region.Y * zoom, cameraPlan[planIndex].Info[regionIndex].Region.Width * zoom, cameraPlan[planIndex].Info[regionIndex].Region.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:
cameraPlan[planIndex].Info[regionIndex].Region.X += x;
cameraPlan[planIndex].Info[regionIndex].Region.Width -= x;
break;
case FieldMargin.Top:
cameraPlan[planIndex].Info[regionIndex].Region.Y += y;
cameraPlan[planIndex].Info[regionIndex].Region.Height -= y;
break;
case FieldMargin.Right:
cameraPlan[planIndex].Info[regionIndex].Region.Width += x;
break;
case FieldMargin.Bottom:
cameraPlan[planIndex].Info[regionIndex].Region.Height += y;
break;
case FieldMargin.LeftTop:
cameraPlan[planIndex].Info[regionIndex].Region.X += x;
cameraPlan[planIndex].Info[regionIndex].Region.Width -= x;
cameraPlan[planIndex].Info[regionIndex].Region.Y += y;
cameraPlan[planIndex].Info[regionIndex].Region.Height -= y;
break;
case FieldMargin.RightTop:
cameraPlan[planIndex].Info[regionIndex].Region.Y += y;
cameraPlan[planIndex].Info[regionIndex].Region.Height -= y;
cameraPlan[planIndex].Info[regionIndex].Region.Width += x;
break;
case FieldMargin.LeftBottom:
cameraPlan[planIndex].Info[regionIndex].Region.X += x;
cameraPlan[planIndex].Info[regionIndex].Region.Width -= x;
cameraPlan[planIndex].Info[regionIndex].Region.Height += y;
break;
case FieldMargin.RightBottom:
cameraPlan[planIndex].Info[regionIndex].Region.Width += x;
cameraPlan[planIndex].Info[regionIndex].Region.Height += y;
break;
case FieldMargin.Inside:
cameraPlan[planIndex].Info[regionIndex].Region.X += x;
cameraPlan[planIndex].Info[regionIndex].Region.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(cameraName);
//bmp = new Bitmap(@"C:\Neotel\测试图.jpg");
PicShow.Refresh();
}
private void BtnAdd_Click(object sender, EventArgs e)
{
if (planIndex == -1) return;
string name = "name" + ++id;
RegionInfo info = new(name, 0, 0, 100, 100, 0.3f);
cameraPlan[planIndex].Info.Add(info);
regionIndex = cameraPlan[planIndex].Info.Count - 1;
ChangeNameRatio();
PicShow.Refresh();
}
private void BtnDel_Click(object sender, EventArgs e)
{
if (regionIndex == -1) return;
if (planIndex == -1) return;
cameraPlan[planIndex].Info.RemoveAt(regionIndex);
regionIndex = cameraPlan[planIndex].Info.Count == 0 ? -1 : 0;
ChangeNameRatio();
PicShow.Refresh();
}
private void TxtName_TextChanged(object sender, EventArgs e)
{
if (regionIndex == -1) return;
cameraPlan[planIndex].Info[regionIndex].Name = TxtName.Text;
PicShow.Refresh();
}
private void NudRatio_ValueChanged(object sender, EventArgs e)
{
if (regionIndex == -1) return;
cameraPlan[planIndex].Info[regionIndex].Region.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 PicShow_MouseDown(object sender, MouseEventArgs e)
{
if (planIndex == -1) return;
if (e.Button != MouseButtons.Left) return;
oldPoint = e.Location;
if (fieldMargin != FieldMargin.Outside) return;
float x = e.X / zoom; //放大到正常大小
float y = e.Y / zoom; //放大到正常大小
regionIndex = cameraPlan[planIndex].Info.FindIndex(match => match.Region.X < x && match.Region.Y < y && match.Region.X + match.Region.Width > x && match.Region.Y + match.Region.Height > y);
ChangeNameRatio();
if (regionIndex > -1)
FindFieldMargin(e.Location);
PicShow.Refresh();
}
private void PicShow_MouseMove(object sender, MouseEventArgs e)
{
if (regionIndex == -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);
if (planIndex < 0) return;
List<RegionInfo> info = cameraPlan[planIndex].Info;
float x, y, width, height;
for (int i = 0; i < info.Count; i++)
{
x = info[i].Region.X * zoom;
y = info[i].Region.Y * zoom;
width = info[i].Region.Width * zoom;
height = info[i].Region.Height * zoom;
string s = info[i].Name;
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 == regionIndex)
{
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
}
private void FrmOpenCamera_Load(object sender, EventArgs e)
{
}
private void LstPlan_SelectedIndexChanged(object sender, EventArgs e)
{
planIndex = LstPlan.SelectedIndex;
regionIndex = -1;
PicShow.Refresh();
}
private void BtnAddPlan_Click(object sender, EventArgs e)
{
string name = Microsoft.VisualBasic.Interaction.InputBox("输入名称");
if (string.IsNullOrWhiteSpace(name)) return;
RegionPlan plan = new RegionPlan(name);
cameraPlan.Add(plan);
LstPlan.Items.Add(name);
LstPlan.SelectedIndex = cameraPlan.Count - 1;
}
private void BtnDelPlan_Click(object sender, EventArgs e)
{
if (planIndex == -1) return;
DialogResult dr = MessageBox.Show("删除" + LstPlan.Text, "", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) return;
cameraPlan.RemoveAt(planIndex);
LstPlan.Items.RemoveAt(planIndex);
planIndex = -1;
if (LstPlan.Items.Count > 0)
LstPlan.SelectedIndex = 0;
}
private void BtnRenamePlan_Click(object sender, EventArgs e)
{
if (planIndex == -1) return;
string name = Microsoft.VisualBasic.Interaction.InputBox(LstPlan.Text + " 重命名为");
if (string.IsNullOrWhiteSpace(name)) return;
cameraPlan[planIndex].Name = name;
int temp = planIndex;
LstPlan.Items.RemoveAt(planIndex);
LstPlan.Items.Insert(temp, name);
LstPlan.SelectedIndex = temp;
}
}
}