Common.cs
29.5 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
using CodeLibrary;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public class Common
{
public delegate void LabelResultEventHandler(LabelResult args);
public static event LabelResultEventHandler LabelResult;
/**获取角度*/
// 求3点形成的夹角
public static float getAngle2(Point pt1, Point pt2, Point pt0)
{
double ma_x = pt1.X - pt0.X;
double ma_y = pt1.Y - pt0.Y;
double mb_x = pt2.X - pt0.X;
double mb_y = pt2.Y - pt0.Y;
double k = (ma_x * mb_y - mb_x * ma_y);
if (k != 0)
{
k = (k / Math.Sqrt(k * k));
}
else
{
k = 1;
}
double v1 = (ma_x * mb_x) + (ma_y * mb_y);
double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y);
double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y);
double cosM = v1 / (ma_val * mb_val);
return (float)(Math.Acos(cosM) * 180 / Math.PI * k);
}
/// <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 double distance(Point p1, Point p2)
{
double result;
result = Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
return result;
}
/// <summary>
/// 以中心点旋转Angle角度
/// </summary>
/// <param name="origin">中心点</param>
/// <param name="reRotate">待旋转的点</param>
/// <param name="angle">旋转角度, 逆时针</param>
public static Point PointRotate(Point origin, Point rePoint, double angle)
{
double x = (rePoint.X - origin.X) * Math.Cos(angle) - (rePoint.Y - origin.Y) * Math.Sin(angle) + origin.X;
double y = (rePoint.X - origin.X) * Math.Sin(angle) + (rePoint.Y - origin.Y) * Math.Cos(angle) + origin.Y;
//rePoint.X = x;
//rePoint.Y = y;
return new Point((int)x, (int)y);
}
/// <summary>
/// 以指定点为中心,测算指定角度,指定长度位置的点
/// </summary>
/// <param name="p1">原点</param>
/// <param name="angle">角度</param>
/// <param name="distance">距离</param>
/// <returns></returns>
public static Point PointWithAngle(Point p1, double angle, double distance)
{
var x2 = p1.X + distance * Math.Cos(angle / 180 * Math.PI);
var y2 = p1.Y + distance * Math.Sin(angle / 180 * Math.PI);
return new Point((int)x2, (int)y2);
}
/// <summary>
///
/// </summary>
/// <param name="labelParam"></param>
/// <param name="org">中心点坐标</param>
/// <param name="Label_R_Offset">贴标臂偏移</param>
/// <param name="labelAngle">贴标角度</param>
/// <returns></returns>
public static Point CalcLabelPoint(LabelParam labelParam, Point org, int Label_R_Offset, int Label_R_Angle_Diff, out int labelAngle)
{
bool singlefeature = ConfigHelper.Config.Get("UseSingleFeatureCode", true);
if (singlefeature)
return calcSinglePrimaryLabelPoint(labelParam, org, Label_R_Offset, Label_R_Angle_Diff, out labelAngle);
else
return calcMultiPrimaryLabelPoint2(labelParam, org, Label_R_Offset, Label_R_Angle_Diff, out labelAngle);
}
static Point calcSinglePrimaryLabelPoint(LabelParam labelParam, Point org, int Label_R_Offset, int Label_R_Angle_Diff, out int labelAngle)
{
//标签位置
SolidBrush blue = new SolidBrush(Color.DarkBlue);
//料盘中心点
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush tmpPlace = new SolidBrush(Color.BlueViolet);
//贴标位置
SolidBrush yeelow = new SolidBrush(Color.Yellow);
Font font = new Font(FontFamily.GenericSansSerif, 60);
Pen Greenpen = new Pen(Color.Green, 10);
Bitmap srcbitmap;
if (File.Exists(labelParam.bitmapfilename))
{
srcbitmap = new Bitmap(labelParam.bitmapfilename);
}
else
{
srcbitmap = new Bitmap(3648, 3648);
}
Bitmap bitmap = new Bitmap(srcbitmap.Width, srcbitmap.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(srcbitmap, 0, 0);
int srcbitmap_Width = srcbitmap.Width;
srcbitmap.Dispose();
Size pointsize = new Size(60, 60);
int widthOffset = 0;
Point Right_Batch_Point = org;
g.FillEllipse(red, new Rectangle(Right_Batch_Point, pointsize));
int angle = 0;
if (labelParam.codeInfos.Count == 0)
{
labelAngle = 0;
return Point.Empty;
}
var feature = labelParam.codeInfos[0];
Point op = new Point(feature.X, feature.Y);
g.FillEllipse(blue, new Rectangle(op, pointsize));
var a = (int)getAngle(Right_Batch_Point, op);
angle = a < 0 ? 360 + a : a;
widthOffset = (int)Common.distance(org, op);
LogUtil.info($"angle:{a}, widthOffset:{widthOffset}");
if (labelParam.PlateW == 15)
{
labelAngle = angle + 120;
}
else if (labelParam.PlateW == 13)
{
labelAngle = angle + 180;
}
else
{
labelAngle = angle + 180;
}
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
LogUtil.info($"labelAngle1:{labelAngle}");
var p1 = PointWithAngle(org, labelAngle, widthOffset);
g.FillEllipse(tmpPlace, new Rectangle(p1, pointsize));
//匹配照片角度与贴标旋转轴的角度差--左右两侧角度
labelAngle += 83;
LogUtil.info($"labelAngle2:{labelAngle}");
//g.FillEllipse(red, new Rectangle(PointRotate(org, op, labelAngle), pointsize));
//贴标坐标
var p2 = PointWithAngle(org, labelAngle, widthOffset);
//贴标偏移
var p3 = PointWithAngle(p2, labelAngle, Label_R_Offset);
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
if (labelAngle <= 180)
{
labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
}
else
{
labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
}
//labelAngle = 360 - labelAngle;
////贴标位置相对于水平正方向的角度
//LogUtil.info($"labelAngle4:{labelAngle}");
////标签横向位置相对圆心的角度
//labelAngle += Label_R_Angle_Diff;
labelAngle = labelAngle < 0 ? labelAngle + 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
LogUtil.info($"labelAngle5:{labelAngle}");
g.FillEllipse(yeelow, new Rectangle(p2, pointsize));
g.Save();
g.Dispose();
string dir = "/image/labeldebug/";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string filepath = dir + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";
bitmap.Save(filepath, ImageFormat.Jpeg);
LabelResult?.Invoke(new LabelResult() { Bitmap = (Bitmap)bitmap.Clone() });
bitmap.Dispose();
return p2;
}
static Point calcMultiPrimaryLabelPoint(LabelParam labelParam, Point org, int Label_R_Offset, int Label_R_Angle_Diff, out int labelAngle)
{
labelAngle = 0;
//标签位置
SolidBrush blue = new SolidBrush(Color.DarkBlue);
//料盘中心点
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush tmpPlace = new SolidBrush(Color.BlueViolet);
//贴标位置
SolidBrush yeelow = new SolidBrush(Color.Yellow);
SolidBrush white = new SolidBrush(Color.White);
Font font = new Font(FontFamily.GenericSansSerif, 60);
Pen Greenpen = new Pen(Color.Green, 10);
Bitmap srcbitmap;
if (File.Exists(labelParam.bitmapfilename))
{
srcbitmap = new Bitmap(labelParam.bitmapfilename);
}
else
{
srcbitmap = new Bitmap(3648, 3648);
}
Bitmap bitmap = new Bitmap(srcbitmap.Width, srcbitmap.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(srcbitmap, 0, 0);
int srcbitmap_Width = srcbitmap.Width;
srcbitmap.Dispose();
Size pointsize = new Size(60, 60);
int widthOffset = 0;
Point Right_Batch_Point = org;
g.FillEllipse(red, new Rectangle(Right_Batch_Point, pointsize));
List<int> angles = new List<int>();
List<int> widthOffsets = new List<int>();
labelParam.codeInfos.ForEach((c) =>
{
if (true)
{
Point op = new Point(c.X, c.Y);
g.FillEllipse(blue, new Rectangle(op, pointsize));
g.DrawString($"{angles.Count + 1}", font, white, c.X + 5, c.Y);
var a = (int)getAngle(Right_Batch_Point, op);
a = a < 0 ? 360 + a : a;
angles.Add(a);
widthOffset = (int)Common.distance(org, op);
widthOffsets.Add(widthOffset);
LogUtil.info($"angles:{a}, widthOffset:{widthOffset}");
}
});
if (angles.Count == 0)
{
labelAngle = 0;
return Point.Empty;
}
angles.Sort();
for (int i = 0; i < angles.Count - 2; i++)
{
int min = angles[i];
int max = angles[i + 1];
if ((max - min) > labelAngle)
{
labelAngle = (max - min) / 2;
LogUtil.info($"labelAngle{i}:{labelAngle}");
}
}
int tmp = angles[angles.Count - 1] - angles[0];
if (tmp / 2 > labelAngle)
{
labelAngle = tmp;
LogUtil.info($"use 首尾labelAngle:{labelAngle}");
}
widthOffset = (widthOffsets.Min() + widthOffsets.Max()) / 2;
LogUtil.info($"use widthOffset:{widthOffset},angle:{labelAngle}");
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
var p1 = PointWithAngle(org, labelAngle, widthOffset);
g.FillEllipse(tmpPlace, new Rectangle(p1, pointsize));
//匹配照片角度与贴标旋转轴的角度差--左右两侧角度
labelAngle += 83;
LogUtil.info($"labelAngle2:{labelAngle}");
//g.FillEllipse(red, new Rectangle(PointRotate(org, op, labelAngle), pointsize));
//贴标坐标
var p2 = PointWithAngle(org, labelAngle, widthOffset);
//贴标偏移
var p3 = PointWithAngle(p2, labelAngle, Label_R_Offset);
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
if (labelAngle <= 180)
{
labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
}
else
{
labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
}
//labelAngle = 360 - labelAngle;
////贴标位置相对于水平正方向的角度
//LogUtil.info($"labelAngle4:{labelAngle}");
////标签横向位置相对圆心的角度
//labelAngle += Label_R_Angle_Diff;
labelAngle = labelAngle < 0 ? labelAngle + 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
LogUtil.info($"labelAngle5:{labelAngle}");
g.FillEllipse(yeelow, new Rectangle(p3, pointsize));
g.Save();
g.Dispose();
string dir = "/image/labeldebug/";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string filepath = dir + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";
bitmap.Save(filepath, ImageFormat.Jpeg);
LabelResult?.Invoke(new LabelResult() { Bitmap = (Bitmap)bitmap.Clone() });
bitmap.Dispose();
return p2;
}
/// <summary>
/// 判断料盘贴标等分来贴标
/// </summary>
/// <param name="labelParam"></param>
/// <param name="org"></param>
/// <param name="Label_R_Offset"></param>
/// <param name="Label_R_Angle_Diff"></param>
/// <param name="labelAngle"></param>
/// <returns></returns>
static Point calcMultiPrimaryLabelPoint2(LabelParam labelParam, Point org, int Label_R_Offset, int Label_R_Angle_Diff, out int labelAngle)
{
labelAngle = 0;
//标签位置
SolidBrush blue = new SolidBrush(Color.DarkBlue);
//料盘中心点
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush tmpPlace = new SolidBrush(Color.BlueViolet);
//贴标位置
SolidBrush yeelow = new SolidBrush(Color.Yellow);
SolidBrush white = new SolidBrush(Color.White);
Font font = new Font(FontFamily.GenericSansSerif, 60);
Pen Greenpen = new Pen(Color.Green, 10);
Bitmap srcbitmap;
if (File.Exists(labelParam.bitmapfilename))
{
srcbitmap = new Bitmap(labelParam.bitmapfilename);
}
else
{
srcbitmap = new Bitmap(3648, 3648);
}
Bitmap bitmap = new Bitmap(srcbitmap.Width, srcbitmap.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(srcbitmap, 0, 0);
int srcbitmap_Width = srcbitmap.Width;
srcbitmap.Dispose();
Size pointsize = new Size(60, 60);
int widthOffset = 0;
Point Right_Batch_Point = org;
g.FillEllipse(red, new Rectangle(Right_Batch_Point, pointsize));
List<int> angles = new List<int>();
List<int> widthOffsets = new List<int>();
labelParam.codeInfos.ForEach((c) =>
{
if (true)
{
Point op = new Point(c.X, c.Y);
g.FillEllipse(blue, new Rectangle(op, pointsize));
g.DrawString($"{angles.Count + 1}", font, white, c.X + 5, c.Y);
var a = (int)getAngle(Right_Batch_Point, op);
a = a < 0 ? 360 + a : a;
angles.Add(a);
widthOffset = (int)Common.distance(org, op);
widthOffsets.Add(widthOffset);
LogUtil.info($"angles:{a}, widthOffset:{widthOffset}");
}
});
if (angles.Count == 0)
{
labelAngle = 0;
return Point.Empty;
}
labelAngle=GetPasteAngle(angles, angles[0]);
widthOffset = (widthOffsets.Min() + widthOffsets.Max()) / 2;
LogUtil.info($"use widthOffset:{widthOffset},angle:{labelAngle}");
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
var p1 = PointWithAngle(org, labelAngle, widthOffset);
g.FillEllipse(tmpPlace, new Rectangle(p1, pointsize));
//匹配照片角度与贴标旋转轴的角度差--左右两侧角度
labelAngle += 83;
LogUtil.info($"labelAngle2:{labelAngle}");
//g.FillEllipse(red, new Rectangle(PointRotate(org, op, labelAngle), pointsize));
//贴标坐标
var p2 = PointWithAngle(org, labelAngle, widthOffset);
//贴标偏移
var p3 = PointWithAngle(p2, labelAngle, Label_R_Offset);
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
if (labelAngle <= 180)
{
labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
}
else
{
labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
}
//labelAngle = 360 - labelAngle;
////贴标位置相对于水平正方向的角度
//LogUtil.info($"labelAngle4:{labelAngle}");
////标签横向位置相对圆心的角度
//labelAngle += Label_R_Angle_Diff;
labelAngle = labelAngle < 0 ? labelAngle + 360 : labelAngle;
labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
LogUtil.info($"labelAngle3:{labelAngle}");
g.FillEllipse(yeelow, new Rectangle(p3, pointsize));
g.Save();
g.Dispose();
string dir = "/image/labeldebug/";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
else if(Directory.GetFiles(dir).Length>100)
{
try
{
Directory.Delete(dir, true);
}catch (Exception)
{
LogUtil.info($"删除失败:{dir}");
}
}
string filepath = dir + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";
bitmap.Save(filepath, ImageFormat.Jpeg);
LabelResult?.Invoke(new LabelResult() { Bitmap = (Bitmap)bitmap.Clone() });
bitmap.Dispose();
return p2;
}
static IdWorker id = new IdWorker(1, 1);
static string[] pnreglist;
static string[] qtyreglist;
public static void LoadMatchList()
{
pnreglist = File.ReadAllLines("config\\pn.list").ToList().FindAll((s) => { return !string.IsNullOrWhiteSpace(s); }).ToArray();
qtyreglist = File.ReadAllLines("config\\qty.list").ToList().FindAll((s) => { return !string.IsNullOrWhiteSpace(s); }).ToArray();
}
public static bool codeProcess(LabelParam labelParam, out string debugmsg)
{
debugmsg = "";
return labelParam.codeInfos.Count > 0;
if (pnreglist == null)
LoadMatchList();
debugmsg = "";
List<CodeInfo> newcodeInfos = new List<CodeInfo>();
labelParam.PN = "";
foreach (var ci in labelParam.codeInfos.ToArray())
{
//string[] pnreglist = new string[] {
// @"^P([1-9]\d+)$",
// @"^(\d+)$%Code 128",
// @"^([1-9]\d{7,9})$"
//};
foreach (var qtyreg in pnreglist)
{
var partrules = qtyreg.Split('%');
if (partrules.Length == 2)
{
if (ci.CodeType != partrules[1])
break;
}
var m = Regex.Match(ci.CodeStr, partrules[0]);
if (m.Success)
{
newcodeInfos.Add(ci);
//labelParam.codeInfos.Remove(ci);
labelParam.PN = m.Groups[1].Value;
debugmsg += $"匹配到PN:{labelParam.PN}\r\n命中规则:{qtyreg}\r\n";
break;
}
}
if (!string.IsNullOrEmpty(labelParam.PN))
break;
}
labelParam.QTY = 0;
foreach (var ci in labelParam.codeInfos.ToArray())
{
//string[] qtyreglist = new string[] {
// @"Q(\d{3,5})[^\d]",
// @"^Q(\d{3,5})$",
// @"^\w+\.\d([1-9]+0+)[1-9]+\d*$",
// @"^.+?;.+?;.+?;(\d{3,5}?);.+?;.+?$",
// @"^QUANTITY:(\d*)",
// @"^(\d{6})\s",
// @"\(\w*\s+(\d+)[a-zA-Z]",
//};
foreach (var qtyreg in qtyreglist)
{
var m = Regex.Match(ci.CodeStr, qtyreg);
if (m.Success)
{
newcodeInfos.Add(ci);
//labelParam.codeInfos.Remove(ci);
if (!int.TryParse(m.Groups[1].Value, out int qty))
break;
labelParam.QTY = qty;
debugmsg += $"匹配到QTY:{labelParam.QTY}\r\n命中规则:{qtyreg}\r\n";
break;
}
}
if (labelParam.QTY > 0)
break;
}
if (string.IsNullOrEmpty(labelParam.PN) || labelParam.QTY == 0)
return false;
//labelParam.codeInfos = newcodeInfos;
labelParam.FC = "985022";
labelParam.RI = "AL" + DateTime.Now.ToString("yyMMddHHmmssf");
labelParam.Batch = DateTime.Now.ToString("MMdd");
labelParam.WareCode = labelParam.RI;
LogUtil.info($"匹配成功:{labelParam.PN},{labelParam.QTY},{labelParam.RI}");
return true;
}
/// <summary>
/// 多标签贴标
/// </summary>
/// <param name="angles">所有标签角度</param>
/// <param name="rightangle">需要避开的码角度</param>
/// <returns></returns>
public static int GetPasteAngle(List<int> angles, int rightangle)
{
Dictionary<int, List<int>> angleblock = new Dictionary<int, List<int>>();
angleblock.Add(0, new List<int>());
angleblock.Add(120, new List<int>());
angleblock.Add(180, new List<int>());
angleblock.Add(240, new List<int>());
angles.ForEach(x =>
{
var a = rightangle - x;
if (a < 0)
a = a * -1;
else if (a > 0)
a = 360 - a;
foreach (var k in angleblock.Keys)
{
if (a > k - 50 && a < k + 50)
{
angleblock[k].Add(x);
}
}
});
foreach (var k in angleblock.Keys)
{
LogUtil.info($"{k}: {string.Join(",", angleblock[k])}");
}
if (angleblock[0].Count > 0)
rightangle = GetAngleAvg(angleblock[0]);
//angleblock.Remove(0);
if (angleblock[120].Count > 0 || angleblock[240].Count > 0)
{
if (angleblock[120].Count > 0)
return GetAngleAvg(angleblock[120]);
else
return rightangle + 120;
}
else if (angleblock[180].Count > 0)
{
return GetAngleAvg(angleblock[240]);
}
else
{
if (angleblock[180].Count == 0)
return rightangle + 120;
else
return GetAngleAvg(angleblock[180]);
}
}
static int GetAngleAvg(List<int> list)
{
if (list.Count() == 0)
return 0;
if (list.Count() == 1)
return list[0];
list.Sort();
int curagv = list[0];
for (int i = 0; i < list.Count() - 1; i++)
{
var a = list[i + 1] - list[i];
if (a > 180)
{
curagv = (curagv + 360 + list[i + 1]) / 2;
}
else
{
curagv = (curagv + list[i + 1]) / 2;
}
}
return curagv;
}
public T DeepClone<T>(T _object)
{
T dstobject;
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, _object);
mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
dstobject = (T)bf.Deserialize(mStream);
mStream.Close();
}
return dstobject;
}
}
public class LabelResult : EventArgs
{
public string ImgPath { get; set; }
public Bitmap Bitmap { get; set; }
}
public class Msg
{
public static List<Msg> msg = new List<Msg>();
public string msgtxt;
public MsgLevel msgLevel;
public DateTime datetime;
public static List<Msg> get()
{
if (_setlogones)
{
_setlogones = false;
foreach (var m in msg)
LogUtil.info(m.msgtxt);
}
List<Msg> mm = new List<Msg>(msg);
return mm;
}
public static void add(string m, MsgLevel ml)
{
msg.Add(new Msg { msgtxt = m, msgLevel = ml, datetime = DateTime.Now });
}
static bool _setlogones = false;
internal static void setlogones()
{
_setlogones = true;
}
public static void clear()
{
msg.Clear();
}
}
public enum MsgLevel
{
warning,
info
}
public class IdWorker
{
private long workerId;
private long datacenterId;
private long sequence = 0L;
private static long twepoch = 1288834974657L;
private static long workerIdBits = 5L;
private static long datacenterIdBits = 5L;
private static long maxWorkerId = -1L ^ (-1L << (int)workerIdBits);
private static long maxDatacenterId = -1L ^ (-1L << (int)datacenterIdBits);
private static long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << (int)sequenceBits);
private long lastTimestamp = -1L;
private static object syncRoot = new object();
public IdWorker(long workerId, long datacenterId)
{
// sanity check for workerId
if (workerId > maxWorkerId || workerId < 0)
{
throw new ArgumentException(string.Format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0)
{
throw new ArgumentException(string.Format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public long nextId()
{
lock (syncRoot)
{
long timestamp = timeGen();
if (timestamp < lastTimestamp)
{
throw new ApplicationException(string.Format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp)
{
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0)
{
timestamp = tilNextMillis(lastTimestamp);
}
}
else
{
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << (int)timestampLeftShift) | (datacenterId << (int)datacenterIdShift) | (workerId << (int)workerIdShift) | sequence;
}
}
protected long tilNextMillis(long lastTimestamp)
{
long timestamp = timeGen();
while (timestamp <= lastTimestamp)
{
timestamp = timeGen();
}
return timestamp;
}
protected long timeGen()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}