UsrCustomlabeling.cs
9.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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);
}
/// <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;
//}
}
}
}