LabelMachine.claclabel.cs
6.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
using CodeLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceLibrary
{
public partial class LabelMachine
{
/// <summary>
///
/// </summary>
/// <param name="Label_Pix_Point"></param>
/// <returns>XY轴贴标点,R轴角度</returns>
public (Point,double,bool) ClacLabel(Point Label_Pix_Point)
{
bool ReverseLabel = false;
int Right_Batch_X = Config.Right_Batch_X;
if (MoveInfo.ReelParam.PlateW == 13)
{
Right_Batch_X = Right_Batch_X + Config.Label_Offset_Pixel_13;
}
else if (MoveInfo.ReelParam.PlateW == 15)
{
Right_Batch_X = Right_Batch_X + Config.Label_Offset_Pixel_15;
}
//照片中料盘中心点像素位置
Point Center_Pix_Point = new Point(Right_Batch_X, Config.Right_Batch_Y);
//二维码坐标
//Point Label_Pix_Point = new Point(X, Y);
//中心点到二维码角度,右侧为0度,+180转换到左侧为0度
var angle = Common.getAngle(Center_Pix_Point, Label_Pix_Point)+180;
//中心点到二维码距离
var distance = Common.distance(Center_Pix_Point, Label_Pix_Point);
//中心点到标签中心的角度
double rr = 20;
var lr = Math.Cos(rr * Math.PI / 180);
//中心点到标签中心的距离
var ll = lr * distance;
//标签中心到盘心的坐标
var labelCenterAngle = rr + angle;
if (labelCenterAngle < 0)
labelCenterAngle = 360 + labelCenterAngle;
//标签中心的坐标
var labelCenter =Common.PointWithAngle(Center_Pix_Point, labelCenterAngle - 180, ll);
/*
//从二维码坐标开始, 往中心点坐标推移一定的距离,获得推移后的坐标
Point AxisPixPoint = Common.PointWithAngle(Label_Pix_Point, angle, Config.Label_R_Offset_Pixel);
//匹配角度与贴标旋转轴的角度差
var labelAngle = angle - Config.Label_R_Angle_Diff+360;
if (labelAngle > 360)
labelAngle -= 360;
*/
//因为标签不统一暂时改为以二维码为中心贴标
labelCenter = Label_Pix_Point;
labelCenterAngle = angle;
//贴标位置轴的像素坐标
Point AxisPixPoint = Common.PointWithAngle(labelCenter, labelCenterAngle, Config.Label_R_Offset_Pixel);
//计算贴标角度的脉冲值
if (labelCenterAngle > 335)
{
ReverseLabel = true;
labelCenterAngle -= 180;
}
int labelAxisPos = (int)((double)Config.Label_R_360 / 335 * (double)labelCenterAngle);
MoveInfo.log($"计算结果,Label_R_360:{Config.Label_R_360},贴标角度:{labelCenterAngle},R轴:{labelAxisPos}");
//计算贴标像素点位与图像中心点的差
Point p1 = new Point(AxisPixPoint.X - Config.Right_Batch_X, AxisPixPoint.Y - Center_Pix_Point.Y);
//计算像素*脉冲像素比得到脉冲值+中心点基准脉冲
var x = (int)(p1.X * Config.Cam_Pixel_X_Ratio) + Config.Label_X_Base;
var y = (int)(p1.Y * Config.Cam_Pixel_Y_Ratio) + Config.Label_Y_Base;
p1.X = x;
p1.Y = y;
MoveInfo.log($"计算结果,图像像素中心{Center_Pix_Point},二维码像素坐标{Label_Pix_Point},角度{angle},贴标角度{labelCenterAngle},贴标像素点位为{AxisPixPoint},轴点位为{p1},R轴{labelAxisPos},盘宽{MoveInfo.ReelParam.PlateW}");
if (saveLabelDebugBmp)
{
IOMove(IO_Label_Type.Camera_Led, IO_VALUE.HIGH);
Task.Delay(10).Wait();
Bitmap bitmap = Camera._cam.GrabOneImage(Config.CameraName);
IOMove(IO_Label_Type.Camera_Led, IO_VALUE.LOW);
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
//Bitmap bitmap = new Bitmap("d:\\出料模块-20210424-043753-0162.bmp");
printLabelDebugBitmap(bitmap, Label_Pix_Point, Center_Pix_Point, AxisPixPoint, angle, p1, labelCenterAngle, labelAxisPos, labelCenter);
bitmap.Dispose();
}
return (p1, labelAxisPos, ReverseLabel);
}
void printLabelDebugBitmap(Bitmap srcbitmap, Point Label_Pix_Point, Point Center_Pix_Point, Point AxisPixPoint, double angle, Point AxisP1,double labelAngle,double labelAxisPos, Point labelCenter) {
SolidBrush blue = new SolidBrush(Color.BlueViolet);
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush yeelow = new SolidBrush(Color.Yellow);
SolidBrush AliceBlue = new SolidBrush(Color.AliceBlue);
Font font = new Font(FontFamily.GenericSansSerif, 60);
Pen Greenpen = new Pen(Color.Green, 10);
Bitmap bitmap = new Bitmap(srcbitmap.Width, srcbitmap.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Size pointsize = new Size(30, 30);
g.DrawImage(srcbitmap, 0, 0);
srcbitmap.Dispose();
g.FillEllipse(blue, new Rectangle(Label_Pix_Point, pointsize));
g.FillEllipse(yeelow, new Rectangle(Center_Pix_Point, pointsize));
g.FillEllipse(red, new Rectangle(AxisPixPoint, pointsize));
g.FillEllipse(AliceBlue, new Rectangle(labelCenter, pointsize));
g.DrawLine(Greenpen, AxisPixPoint, Label_Pix_Point);
g.DrawString($"angle:{angle}", font, blue, 0, 0);
g.DrawString($"AxisP1:{AxisP1}", font, red, 0, 80);
g.DrawString($"labelAngle:{labelAngle}/{labelAxisPos}", font, red, 0, 160);
g.Save();
Directory.CreateDirectory("/image/LabelDebug/");
bitmap.Save("/image/LabelDebug/" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")+".jpg",ImageFormat.Jpeg);
g.Dispose();
}
}
}