UsrCustomlabeling.cs
15.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
using Asa.FaceControl;
using BLL;
using DeviceLibrary.AutoScanAndLabel;
using DocumentFormat.OpenXml.Wordprocessing;
using MemoryRead;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace SmartScan.PlusSettingFrm
{
public partial class UsrCustomlabeling : UserControl, ISetMenu
{
public static List<LabelingData> labelings = new List<LabelingData>();
private List<MaterialTemplate> mateCopy;
private int mateIndex = -1;
string flie = Application.StartupPath + "\\LabelingFiles";
public UsrCustomlabeling()
{
InitializeComponent();
mateCopy = BLLCommon.mateEdit.ToCopy();
LstMate.Items.AddRange(BLLCommon.mateEdit.Name);
InitializedData();
Language.SetLanguage(this);
}
static string BitmapToBase64(Bitmap bitmap)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 将Bitmap保存到MemoryStream中
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// 获取字节数组
byte[] byteArray = memoryStream.ToArray();
// 将字节数组转换为Base64编码的字符串
return Convert.ToBase64String(byteArray);
}
}
public void InitializedData()
{
if (!File.Exists(flie))
{
//File.Create(flie);
System.IO.Directory.CreateDirectory(flie);
}
var files = Directory.GetFiles(flie, "*.txt", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
try
{
string fileContent = File.ReadAllText(file);
LabelingData labelingData = JsonConvert.DeserializeObject<LabelingData>(fileContent);
if (labelingData != null)
{
labelings.Add(labelingData);
}
}
catch (Exception e)
{
LogNet.log.Info(file + "加载贴标数据错误:" + e.Message);
}
}
}
public FacePanel GetPanel()
{
//BitmapData bitmapData = new BitmapData();
//bitmapData.IsIDCamera = true;
//bitmapData.X = 512; bitmapData.Y = 512;
//Bitmap bitmap = new Bitmap("D:\\Desktop\\项目\\ns100\\测试条码\\7inch\\Kingbright2 - 50x70.jpg");
////bitmapData.ImageData = BitmapToBase64(bitmap);
//List<BarcodeInfos> barcodeInfos = new List<BarcodeInfos>();
//bitmapData.PlateW = 15;
//var list = Camera.GetBarCode(bitmap);
//list.ForEach(b => { barcodeInfos.Add(new BarcodeInfos { Angle = b.Angle, Text = b.Text, X = (int)b.Center.X, Y = (int)b.Center.Y }); });
//bitmapData.BarCodeList = barcodeInfos;
//var a = JsonConvert.SerializeObject(bitmapData);
tableLayoutPanel1.Width = facePanel1.Width - 10;
tableLayoutPanel1.Height = facePanel1.Height - 10;
return facePanel1;
}
public void Save()
{
if (!File.Exists(flie))
{
System.IO.Directory.CreateDirectory(flie);
}
foreach (var item in labelings)
{
string json = JsonConvert.SerializeObject(item);
string pathfile = flie + $"\\{item.Name}.txt";
File.WriteAllText(pathfile, json);
}
}
private void UsrCustomlabeling_Load(object sender, EventArgs e)
{
}
private void LstMate_SelectedIndexChanged(object sender, EventArgs e)
{
LabelRotationAngle.Text = null;
IsOverlaylabel.Checked = false;
Xoffset.Text = null;
Yoffset.Text = null;
IscIockwise.Checked = false;
IsCounterclockwise.Checked = false;
RotationAngle.Text = null;
LabelRotationAngle.Text = null;
if (LstMate.SelectedIndex == -1) return;
mateIndex = LstMate.SelectedIndex;
BLLCommon.mateMaxCodeID = mateCopy[mateIndex].GetMaxID();
string iamgepath = mateCopy[mateIndex].ImagePath;
if (!string.IsNullOrEmpty(iamgepath))
{
if (System.IO.File.Exists(iamgepath))
{
ImgShow.Image = ObjConversion.ReadImageFile(iamgepath);
}
}
//ImgShow.Image = mateCopy[mateIndex].Image;
ImgShow.CodeCenterClear();
ImgShow.AddCodeCenter(LabelCenter());
string name = mateCopy[mateIndex].Name;
if (labelings == null)
{
return;
}
var lable = labelings.Where(a => a.Name == name).FirstOrDefault();
if (lable != null)
{
IsOverlaylabel.Checked = lable.IsOverlaylabel;
LabelRotationAngle.Text = lable.LabelRotationAngle.ToString();
Xoffset.Text = lable.Xoffset.ToString();
Yoffset.Text = lable.Yoffset.ToString();
if (!IsOverlaylabel.Checked)
{
IscIockwise.Checked = lable.IscIockwise;
IsCounterclockwise.Checked = lable.IsCounterclockwise;
RotationAngle.Text = lable.RotationAngle.ToString();
}
}
}
private PointF[] LabelCenter()
{
List<PointF> item = new();
float x, y;
for (int i = 0; i < mateCopy[mateIndex].Code.Count; i++)
{
x = mateCopy[mateIndex].Code[i].CenterX;
y = mateCopy[mateIndex].Code[i].CenterY;
item.Add(new PointF(x, y));
}
return item.ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
//string a = " True;匹配成功条码xy:X=1498;Y=1934,角度=120";
//string name = "1";
//AMatch aMatch = new AMatch();
//aMatch.Angle = 120;
//aMatch.Points = new Point(1498, 1934);
//Point cenl = new Point(2000, 1800);
//LabelingAngle_New(name, aMatch, cenl, true, 7, out int angle);
if (mateIndex == -1)
{
return;
}
LabelingData labeling = new LabelingData();
if (labelings != null)
{
var list = labelings.Where(a => a.Name == mateCopy[mateIndex].Name).ToList();
foreach (var item in list) { labelings.Remove(item); }
}
labeling.Name = mateCopy[mateIndex].Name;
labeling.IsOverlaylabel = IsOverlaylabel.Checked;
labeling.Xoffset = Xoffset.Text != "" ? int.Parse(Xoffset.Text) : 0;
labeling.Yoffset = Yoffset.Text != "" ? int.Parse(Yoffset.Text) : 0;
if (!labeling.IsOverlaylabel)
{
labeling.IscIockwise = IscIockwise.Checked ? true : false;
labeling.IsCounterclockwise = IsCounterclockwise.Checked ? true : false;
labeling.RotationAngle = RotationAngle.Text != "" ? int.Parse(RotationAngle.Text) : 0;
labeling.LabelRotationAngle = LabelRotationAngle.Text != "" ? int.Parse(LabelRotationAngle.Text) : 0;
}
labelings.Add(labeling);
}
public static System.Drawing.Point LabelingAngle(string name, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
{
System.Drawing.Point lable = aMatch.Points;
var data = labelings.Where(a => a.Name == name).FirstOrDefault();
LogNet.log.Info($"{name}:原标签角度x={lable.X};y={lable.Y};中心点x={centrality.X};y={centrality.Y}");
if (data != null)
{
if (data.IsOverlaylabel)
{
#region 覆盖原标签
//LogNet.log.Info($"{name}:覆盖原标签;是否ID相机={isvaor};");
//aMatch.Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
//if (isvaor)
//{
// angle = ScanningCameraAngle(aMatch.Angle);
//}
//else
//{
// angle = OldAngleAlgo(centrality, lable, PlateW);
//}
//LogNet.log.Info($"{name}:覆盖原标签!新标签位置:{aMatch.Points};角度:{angle}");
//return aMatch.Points;
#endregion
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
System.Drawing.Point point = new Point(0, 0);
if (data.IscIockwise)
{
point = LabelingPosition.ClockwiseRotation(lable, centrality, data.RotationAngle);
}
if (data.IsCounterclockwise)
{
point = LabelingPosition.CounterclockwiseRotation(lable, centrality, data.RotationAngle);
}
if (!data.IscIockwise && !data.IsCounterclockwise)
{
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
System.Drawing.Point Newpoint = new System.Drawing.Point(point.X + data.Xoffset, point.Y + data.Yoffset);
angle = data.LabelRotationAngle;
LogNet.log.Info($"{name}:xy轴偏移后位置,x={Newpoint.X},y={Newpoint.Y};角度为:{angle}");
return Newpoint;
}
else
{
LogNet.log.Info($"未找到<{name}>模板:默认覆盖原标签!");
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
#region 覆盖原标签
//aMatch.Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
//if (isvaor)
//{
// angle = ScanningCameraAngle(aMatch.Angle);
//}
//else
//{
// angle = OldAngleAlgo(centrality, lable, PlateW);
//}
//LogNet.log.Info($"未找到<{name}>模板:默认覆盖原标签!新标签位置:{aMatch.Points};角度:{angle}");
//return aMatch.Points;
#endregion
}
}
public static System.Drawing.Point LabelingAngle_New(string name, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle)
{
System.Drawing.Point label = aMatch.Points;
var data = labelings.FirstOrDefault(a => a.Name == name);
LogNet.log.Info($"{name}:原标签角度x={label.X};y={label.Y};中心点x={centrality.X};y={centrality.Y}");
if (data == null)
{
LogNet.log.Info("未查询到贴标数据,使用覆盖原标签方式;");
return Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
}
else
{
if (data.IsOverlaylabel || (!data.IscIockwise && !data.IsCounterclockwise))
{
var position = Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, out angle);
return new Point(position.X+data.Xoffset,position.Y+data.Yoffset);
}
System.Drawing.Point point = new Point(0, 0);
if (data.IscIockwise)
{
point = LabelingPosition.ClockwiseRotation(label, centrality, data.RotationAngle);
}
else if (data.IsCounterclockwise)
{
point = LabelingPosition.CounterclockwiseRotation(label, centrality, data.RotationAngle);
}
System.Drawing.Point newPoint = new System.Drawing.Point(point.X + data.Xoffset, point.Y + data.Yoffset);
angle = data.LabelRotationAngle;
LogNet.log.Info($"{name}:xy轴偏移后位置,x={newPoint.X},y={newPoint.Y};角度为:{angle}");
return newPoint;
}
}
public static Point Overwritelabel(Point centrality, Point lable, bool isvaor, int PlateW, int Angle, out int angle)
{
Point Points = LabelingPosition.ClockwiseRotation(lable, centrality, 0);
if (isvaor)
{
angle = ScanningCameraAngle(Angle);
}
else
{
angle = OldAngleAlgo(centrality, lable, PlateW);
}
LogNet.log.Info($"覆盖原标签!是否id相机={isvaor};新标签位置:{Points};角度:{angle}");
return Points;
}
public static int ScanningCameraAngle(int angles)
{
int angle = 0;
double rotationAngle = 83;
if (angles < 0 && angles != 0)
{
angle = (int)(Math.Abs(angles) + rotationAngle);
}
else
{
if (angles <= 83)
{
angle = (int)Math.Abs(angles - rotationAngle);
}
else
{
angle = 350 - (int)(angles - rotationAngle);
}
}
angle += 90;
if (angle > 344)
{
angle -= 344;
}
return angle;
}
/// <summary>
/// 获取以点为中心的角度,-180~180
/// </summary>
/// <param name="pt1"></param>
/// <param name="pt2"></param>
/// <returns></returns>
public static double getAngle(Point pt1, Point pt2)
{
double mb_x = pt2.X - pt1.X;
double mb_y = pt2.Y - pt1.Y;
double rotation = Math.Atan2(mb_y, mb_x);
rotation = rotation / Math.PI * 180;
return rotation;
//double angle = 360 - rotation + 90;
//angle = angle < 0 ? 360 + angle : angle;
//angle = angle % 360;
//return Math.Floor(angle / 360 * 24);
}
public static int OldAngleAlgo(Point lable, Point center, int PlateW)
{
int labelAngle = 0;
var a = (int)getAngle(lable, center);
int angle = a < 0 ? 360 + a : a;
if (PlateW == 15)
{
labelAngle = angle + 120;
}
else if (PlateW == 13)
{
labelAngle = angle + 180;
}
else
{
labelAngle = angle + 180;
}
labelAngle += 83;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
return labelAngle;
//if (labelAngle <= 180)
//{
// labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
//}
//else
//{
// labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
//}
}
}
}