AioMarkControl.cs
6.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AOI;
using System.Threading;
using System.Drawing.Drawing2D;
using System.IO;
using Asa.Theme;
namespace AccAOI.control
{
public partial class AioMarkControl : ABaseControl
{
public AioMarkControl()
{
InitializeComponent();
}
public override AoiMethod GetAoiInfo()
{
if (this.AoiInfo is AoiMarkMethod)
{
AoiMarkMethod method = (AoiMarkMethod)AoiInfo;
float.TryParse(flatTextSearchZoom.Text, out float zoom);
method.SearchPathZoom = zoom;
if (currPath != null)
{
method.RoiPath = currPath;
}
int value =trackBarSamePercent.Value;
SetText(txtSamePercent, trackBarSamePercent.Value);
// int value = trackBarSamePercent.Value;
method.SamePercent = value;
AoiInfo = method;
}
return AoiInfo;
}
public override void ShowAoiInfo()
{
if (this.AoiInfo is AoiMarkMethod)
{
AoiMarkMethod method = (AoiMarkMethod)AoiInfo;
trackBarSamePercent.Value = (int)method.SamePercent;
IsShowOk = true;
UpdateImage();
}
}
private void btnTest_Click(object sender, EventArgs e)
{
if (this.AoiInfo is AoiMarkMethod)
{
string fileName = txtImage.Text.ToString();
Image checkImg = null;
if (File.Exists(fileName))
{
//读取图片内容
checkImg = (Image)Image.FromFile(fileName).Clone();
}
if (checkImg == null)
{
checkImg = FrmAoiSetting.BaseImg;
}
DateTime time = DateTime.Now;
AoiMarkMethod mark = (AoiMarkMethod)AoiInfo;
Image result= mark.FixImage(FrmAoiSetting.BaseImg, checkImg);
TimeSpan span = DateTime.Now - time;
if (result == null)
{
this.aoiImage.Image = null;
lblResult.ForeColor = Color.Red ;
lblResult.Text = "Mark区域无效";
}
else
{
lblResult.ForeColor = Color.Green;
lblResult.Text = "OK" ;
this.aoiImage.Image = result;
}
lblTime.Text= "耗时:" + Math.Round(span.TotalSeconds, 1)+ "秒";
}
}
public override void UpdateImage()
{
if (!IsShowOk)
{
return;
}
if (Monitor.TryEnter(UpdateLock))
{
try
{
int value = trackBarSamePercent.Value;
SetText(txtSamePercent, trackBarSamePercent.Value);
Image BaseImage = GetImg();
if (BaseImage == null || currPath == null)
{
return;
}
GetAoiInfo();
AoiMarkMethod markMethod= (AoiMarkMethod)AoiInfo;
GraphicsPath searchPath = markMethod.GetSearchPath();
BImageBox.ShowPath(searchPath, Color.Yellow);
GC.Collect();
}
catch (Exception ex)
{
Console.WriteLine("UpdateImage出错:" + ex.ToString());
}
finally
{
Monitor.Exit(UpdateLock);
}
}
else
{
Console.WriteLine("UpdateImage执行失败,未得到锁");
}
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "打开本地图片";
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
txtImage.Text = fileName;
}
private void trackBarSamePercent_ValueChanged(object sender, EventArgs e)
{
UpdateImage();
}
private void SetTbValue(TrackBar tb, int value)
{
if (tb.Value.Equals(value.ToString()))
{
return;
}
if (value < tb.Minimum)
{
tb.Value = tb.Minimum;
}
else if (value > tb.Maximum)
{
tb.Value = tb.Maximum;
}
else
{
tb.Value = value;
}
UpdateImage();
}
private void SetText(FlatText text, int value)
{
if (text.Text.ToString().Equals(value.ToString()))
{
return;
}
text.Text = value.ToString();
}
private void txtSamePercent_TextChanged(object sender, EventArgs e)
{
int value = AOIFormUtil.GetIntValue(txtSamePercent);
SetTbValue(trackBarSamePercent, value);
UpdateImage();
}
}
}