LabelingPosition.cs
11.2 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary.AutoScanAndLabel
{
public static class LabelingPosition
{
public static Point MultipleChoiceLabeling(LabelParam labelParam, Point centrality, out int angle,out Bitmap bitmaps)
{
//7寸XY偏移量
int Xpianyilian_7 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_7", 0);
int Ypianyilian_7 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_7", 0);
//13寸XY偏移量
int Xpianyilian_13 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_13", 0);
int Ypianyilian_13 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_13", 0);
//15寸XY偏移量
int Xpianyilian_15 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_15", 0);
int Ypianyilian_15 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_15", 0);
//7寸朝那个方向贴标true顺时针,false逆时针
bool istoward_7 = ConfigHelper.Config.Get<bool>("Labeling_istoward_7", true);
int degree_7 = ConfigHelper.Config.Get<int>("Labeling_degree_7", 90);
//13寸朝那个方向贴标true顺时针,false逆时针
bool istoward_13 = ConfigHelper.Config.Get<bool>("Labeling_istoward_13", true);
int degree_13 = ConfigHelper.Config.Get<int>("Labeling_degree_13", 90);
//15寸朝那个方向贴标true顺时针,false逆时针
bool istoward_15 = ConfigHelper.Config.Get<bool>("Labeling_istoward_15", true);
int degree_15 = ConfigHelper.Config.Get<int>("Labeling_degree_15", 90);
//7寸标签旋转多少度
int Scalerotationdegree_7 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_7", 90);
//13寸标签旋转多少度
int Scalerotationdegree_13 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_13", 90);
//15寸标签旋转多少度
int Scalerotationdegree_15 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_15", 90);
#region 数据
//标签位置
SolidBrush blue = new SolidBrush(Color.DarkBlue);
//料盘中心点
SolidBrush red = new SolidBrush(Color.Red);
//贴标位置
SolidBrush yeelow = new SolidBrush(Color.Yellow);
Bitmap srcbitmap;
if (File.Exists(labelParam.bitmapfilename))
{
srcbitmap = new Bitmap(labelParam.bitmapfilename);
}
else
{
srcbitmap = new Bitmap(3648, 3648);
}
Bitmap bitmap = new Bitmap(srcbitmap);
Graphics g = Graphics.FromImage(bitmap);
Size pointsize = new Size(60, 60);
g.FillEllipse(red, new Rectangle(centrality, pointsize));
int idenx = 0; //Array.FindIndex(labelParam.AMatch, x => x);
if (idenx == -1)
{
idenx = 0;
}
int angles = (int)labelParam.codeInfos[idenx].Orientation;
Point CalPoint = new Point(labelParam.codeInfos[idenx].X, labelParam.codeInfos[idenx].Y);
g.FillEllipse(blue, new Rectangle(CalPoint, pointsize));
#endregion
LogUtil.info($"标签坐标x={CalPoint.X},y={CalPoint.Y},中心点x={centrality.X},y={centrality.Y}");
Point point = new Point();
switch (labelParam.PlateW)
{
case 13:
point = LabelingAngle_13(CalPoint, centrality, out angle);
break;
case 15:
point = LabelingAngle_15(CalPoint, centrality, out angle);
break;
default:
point = LabelingAngle_7(CalPoint, centrality, out angle);
break;
}
g.FillEllipse(yeelow, new Rectangle(point, pointsize));
g.Save();
g.Dispose();
ImageSave(bitmap);
bitmaps = bitmap;
return point;
}
public static Point LabelingAngle_7(Point lable, Point centrality, out int angle)
{
//7寸XY偏移量
int Xpianyilian_7 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_7", 100);
int Ypianyilian_7 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_7", 100);
//7寸朝那个方向贴标true顺时针,false逆时针
bool istoward_7 = ConfigHelper.Config.Get<bool>("Labeling_istoward_7", true);
int degree_7 = ConfigHelper.Config.Get<int>("Labeling_degree_7", 90);
//7寸标签旋转多少度
int Scalerotationdegree_7 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_7", 90);
Point point = lable;
LogUtil.info($"7寸料盘,原标签角度={lable};中心点={centrality};");
if (istoward_7)
{
point = ClockwiseRotation(lable, centrality, degree_7);
}
else
{
point = CounterclockwiseRotation(lable, centrality, degree_7);
}
Point Newpoint = new Point(point.X + Xpianyilian_7, point.Y + Ypianyilian_7);
LogUtil.info($"7寸料盘,xy轴偏移后位置,x={Newpoint.X},y={Newpoint.Y}");
angle = Scalerotationdegree_7;
return Newpoint;
}
public static Point LabelingAngle_13(Point lable, Point centrality, out int angle)
{
//13寸XY偏移量
int Xpianyilian_13 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_13", 100);
int Ypianyilian_13 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_13", 100);
//13寸朝那个方向贴标true顺时针,false逆时针
bool istoward_13 = ConfigHelper.Config.Get<bool>("Labeling_istoward_13", true);
int degree_13 = ConfigHelper.Config.Get<int>("Labeling_degree_13", 90);
//13寸标签旋转多少度
int Scalerotationdegree_13 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_13", 90);
Point point = lable;
LogUtil.info($"13寸料盘,原标签角度={lable};中心点={centrality};");
if (istoward_13)
{
point = ClockwiseRotation(lable, centrality, degree_13);
}
else
{
point = CounterclockwiseRotation(lable, centrality, degree_13);
}
Point Newpoint = new Point(point.X + Xpianyilian_13, point.Y + Ypianyilian_13);
LogUtil.info($"13寸料盘,xy轴偏移后位置,x={Newpoint.X},y={Newpoint.Y}");
angle = Scalerotationdegree_13;
return Newpoint;
}
public static Point LabelingAngle_15(Point lable, Point centrality, out int angle)
{
//15寸XY偏移量
int Xpianyilian_15 = ConfigHelper.Config.Get<int>("Labeling_Xpianyilian_15", 100);
int Ypianyilian_15 = ConfigHelper.Config.Get<int>("Labeling_Ypianyilian_15", 100);
//15寸朝那个方向贴标true顺时针,false逆时针
bool istoward_15 = ConfigHelper.Config.Get<bool>("Labeling_istoward_15", true);
int degree_15 = ConfigHelper.Config.Get<int>("Labeling_degree_15", 90);
//15寸标签旋转多少度
int Scalerotationdegree_15 = ConfigHelper.Config.Get<int>("Labeling_Scalerotationdegree_15", 90);
Point point = lable;
LogUtil.info($"15寸料盘,原标签角度={lable};中心点={centrality};");
if (istoward_15)
{
point = ClockwiseRotation(lable, centrality, degree_15);
}
else
{
point = CounterclockwiseRotation(lable, centrality, degree_15);
}
Point Newpoint = new Point(point.X + Xpianyilian_15, point.Y + Ypianyilian_15);
LogUtil.info($"15寸料盘,xy轴偏移后位置,x={Newpoint.X},y={Newpoint.Y}");
angle = Scalerotationdegree_15;
return Newpoint;
}
/// <summary>
/// 顺时针旋转
/// </summary>
/// <param name="lable">标签坐标</param>
/// <param name="center">中心点</param>
/// <param name="angle">旋转角度</param>
/// <returns></returns>
static Point ClockwiseRotation(Point lable, Point center, double angle)
{
angle += 83;
double theta = angle * Math.PI / 180;
//double x, double y, double h, double k, double angle
//double x_new = (x - h) * Math.Cos(theta) - (y - k) * Math.Sin(theta) + h;
//double y_new = (x - h) * Math.Sin(theta) + (y - k) * Math.Cos(theta) + k;
double x_new = (lable.X - center.X) * Math.Cos(theta) - (lable.Y - center.Y) * Math.Sin(theta) + center.X;
double y_new = (lable.X - center.X) * Math.Sin(theta) + (lable.Y - center.Y) * Math.Cos(theta) + center.Y;
LogUtil.info($"顺时针旋转{angle}度,x={x_new},y={y_new};");
return new Point((int)x_new, (int)y_new);
}
/// <summary>
/// 逆时针旋转
/// </summary>
/// <param name="lable">标签坐标</param>
/// <param name="center">中心点</param>
/// <param name="angle">旋转角度</param>
/// <returns></returns>
static Point CounterclockwiseRotation(Point lable, Point center, double angle)
{
angle += 83;
double theta = angle * Math.PI / 180;
//double x, double y, double h, double k, double angle
//double x_new = (x - h) * Math.Cos(theta) + (y - k) * Math.Sin(theta) + h;
//double y_new = -(x - h) * Math.Sin(theta) + (y - k) * Math.Cos(theta) + k;
double x_new = (lable.X - center.X) * Math.Cos(theta) + (lable.Y - center.Y) * Math.Sin(theta) + center.X;
double y_new = -(lable.X - center.X) * Math.Sin(theta) + (lable.Y - center.Y) * Math.Cos(theta) + center.Y;
LogUtil.info($"逆时针旋转{angle}度,x={x_new},y={y_new};");
return new Point((int)x_new, (int)y_new);
}
static void ImageSave(Bitmap bitmap)
{
string filepath = Application.StartupPath + "\\image\\Labeling\\";
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
else if (Directory.GetFiles(filepath).Length > 100)
{
try
{
Directory.Delete(filepath, true);
Directory.CreateDirectory(filepath);
}
catch (Exception)
{
LogUtil.info($"删除失败:{filepath}");
}
}
Bitmap bitmaps = (Bitmap)bitmap.Clone();
bitmaps.Save(filepath + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + DateTime.Now.Millisecond + ".jpg");
//LabelResult?.Invoke(new LabelResult() { Bitmap = (Bitmap)bitmap.Clone() });
//bitmap.Dispose();
}
}
}