Material.cs
19.7 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
namespace BLL
{
/// <summary>
/// 物料类
/// </summary>
public class Material
{
private XmlDocument _doc;
private List<CodeSplice.MaterialTemplate> _mate;
/// <summary>
/// 物料类
/// </summary>
/// <param name="path">文件路径</param>
public Material(string path)
{
string[] arr;
List<string> itemFile = new List<string>();
List<string> itemImage = new List<string>();
string[] file = System.IO.Directory.GetFiles(path); //searchOption无法完全读取文件,设置*.xml,xmla、xmlb、xmlc都会获取
foreach (string s in file)
{
if (s.ToUpper().EndsWith(".XML")) itemFile.Add(s);
else if (s.ToUpper().EndsWith(".JPG")) itemImage.Add(s);
}
_doc = new XmlDocument();
_mate = new List<CodeSplice.MaterialTemplate>();
for (int i = 0; i < itemFile.Count; i++) //文件循环
{
CodeSplice.MaterialTemplate temp = new CodeSplice.MaterialTemplate();
try
{
_doc.Load(itemFile[i]);
XmlNode root = _doc.LastChild;
temp.Name = ((XmlElement)root).GetAttribute("Name");
temp.FilePath = itemFile[i];
temp.ImagePath = itemImage[i];
temp.Image = new Bitmap(itemImage[i]);
int n = root.ChildNodes.Count;
for (int j = 0; j < n; j++) //标签区域循环
{
XmlElement ele1 = (XmlElement)root.ChildNodes[j];
CodeSplice.LabelRect label = new CodeSplice.LabelRect();
label.AvgAngle = Convert.ToSingle(ele1.GetAttribute("AvgAngle"));
label.CodeCount = Convert.ToInt32(ele1.GetAttribute("CodeCount"));
label.Check = Convert.ToBoolean(ele1.GetAttribute("Check"));
int count = ele1.ChildNodes.Count;
for (int k = 0; k < count; k++) //标签内条码循环
{
XmlElement ele2 = (XmlElement)ele1.ChildNodes[k];
if (ele2.LocalName == "Code")
{
CodeSplice.BarCode code = new CodeSplice.BarCode();
code.ID = Convert.ToInt32(ele2.GetAttribute("ID"));
code.Angle = Convert.ToSingle(ele2.GetAttribute("Angle"));
arr = ele2.GetAttribute("Center").Split(',');
code.Center = new PointF(Convert.ToSingle(arr[0]), Convert.ToSingle(arr[1]));
code.Text = ele2.GetAttribute("Text");
code.CodeType = ele2.GetAttribute("CodeType");
code.Mode = ele2.GetAttribute("Mode");
code.Width = Convert.ToSingle(ele2.GetAttribute("Width"));
code.Height = Convert.ToSingle(ele2.GetAttribute("Height"));
code.Distance = Convert.ToDouble(ele2.GetAttribute("Distance"));
code.Start = Convert.ToInt32(ele2.GetAttribute("Start"));
code.Length = Convert.ToInt32(ele2.GetAttribute("Length"));
label.Code.Add(code);
}
else if (ele2.LocalName == "Ocr")
{
CodeSplice.OcrRect ocr = new CodeSplice.OcrRect();
ocr.CodeID = Convert.ToInt32(ele2.GetAttribute("CodeID"));
arr = ele2.GetAttribute("Offset").Split(',');
ocr.Offset = new Point(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]));
arr = ele2.GetAttribute("Size").Split(',');
ocr.Size = new Size(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]));
ocr.Mode = ele2.GetAttribute("Mode");
label.Ocr.Add(ocr);
}
}
temp.LabelRect.Add(label);
}
}
catch (Exception ex)
{
//temp.ErrInfo = ex.ToString();
}
_mate.Add(temp);
}
}
/// <summary>
/// 物料模板名称
/// </summary>
public string[] Name
{
get
{
string[] s = new string[_mate.Count];
for (int i = 0; i < s.Length; i++)
s[i] = _mate[i].Name;
return s;
}
}
/// <summary>
/// 物料模板总数
/// </summary>
public int Count
{
get
{
if (_mate == null)
return -1;
else
return _mate.Count;
}
}
/// <summary>
/// 选中的物料模板的索引
/// </summary>
public int TempIndex { get; set; } = -1;
/// <summary>
/// 当前模板的图片
/// </summary>
public Bitmap Image
{
get
{
if (TempIndex < 0 || TempIndex >= _mate.Count)
return null;
else
return _mate[TempIndex].Image;
}
}
/// <summary>
/// 当前模板的标签位置信息
/// </summary>
public List<CodeSplice.LabelRect> Label
{
get
{
if (TempIndex < 0 || TempIndex >= _mate.Count)
return null;
else
return _mate[TempIndex].LabelRect;
}
}
public List<CodeSplice.LabelRect> GetLabel(int index)
{
return _mate[index].LabelRect;
}
public void GetLabelOcr(int index, out List<string> mode, out List<CodeSplice.OcrSpace> space)
{
mode = new List<string>();
space = new List<CodeSplice.OcrSpace>();
List<CodeSplice.LabelRect> label = _mate[index].LabelRect;
for (int i = 0; i < label.Count; i++)
{
if (!label[i].Check) continue;
for (int j = 0; j < label[i].Ocr.Count; j++)
{
if (label[i].Ocr[j].Mode.Length > 0)
{
CodeSplice.OcrSpace os = new CodeSplice.OcrSpace
{
ID = label[i].Ocr[j].CodeID,
OcrOffset = label[i].Ocr[j].Offset,
OcrSize = label[i].Ocr[j].Size
};
space.Add(os);
mode.Add(label[i].Ocr[j].Mode);
}
}
}
if (space.Count == 0) return;
for (int i = 0; i < label.Count; i++)
{
if (!label[i].Check) continue;
for (int j = 0; j < label[i].CodeCount; j++)
{
List<CodeSplice.OcrSpace> os = space.FindAll(id => id.ID == label[i].Code[j].ID);
if (os.Count == 0) continue;
for (int k = 0; k < os.Count; k++)
{
os[k].CodeCenter = label[i].Code[j].Center;
os[k].CodeAngle = label[i].Code[j].Angle;
}
}
}
}
/// <summary>
/// 当前的物料模板的副本
/// </summary>
public CodeSplice.MaterialTemplate TempCopy()
{
if (TempIndex < 0 || TempIndex >= _mate.Count)
return null;
CodeSplice.MaterialTemplate temp = new CodeSplice.MaterialTemplate();
temp.FilePath = _mate[TempIndex].FilePath;
temp.ImagePath = _mate[TempIndex].ImagePath;
temp.Image = new Bitmap(_mate[TempIndex].Image);
temp.Name = _mate[TempIndex].Name;
for (int i = 0; i < _mate[TempIndex].LabelRect.Count; i++)
{
CodeSplice.LabelRect rect = new CodeSplice.LabelRect();
rect.AvgAngle = _mate[TempIndex].LabelRect[i].AvgAngle;
rect.CodeCount = _mate[TempIndex].LabelRect[i].CodeCount;
for (int j = 0; j < rect.CodeCount; j++)
{
CodeSplice.BarCode code = new CodeSplice.BarCode();
code.ID = _mate[TempIndex].LabelRect[i].Code[j].ID;
code.Text = _mate[TempIndex].LabelRect[i].Code[j].Text;
code.Center = _mate[TempIndex].LabelRect[i].Code[j].Center;
code.Angle = _mate[TempIndex].LabelRect[i].Code[j].Angle;
code.Width = _mate[TempIndex].LabelRect[i].Code[j].Width;
code.Height = _mate[TempIndex].LabelRect[i].Code[j].Height;
code.Distance = _mate[TempIndex].LabelRect[i].Code[j].Distance;
code.Mode = _mate[TempIndex].LabelRect[i].Code[j].Mode;
code.Start = _mate[TempIndex].LabelRect[i].Code[j].Start;
code.Length = _mate[TempIndex].LabelRect[i].Code[j].Length;
code.CodeType = _mate[TempIndex].LabelRect[i].Code[j].CodeType;
rect.Code.Add(code);
}
for (int j = 0; j < _mate[TempIndex].LabelRect[i].Ocr.Count; j++)
{
CodeSplice.OcrRect ocr = new CodeSplice.OcrRect();
ocr.CodeID = _mate[TempIndex].LabelRect[i].Ocr[j].CodeID;
ocr.Offset = _mate[TempIndex].LabelRect[i].Ocr[j].Offset;
ocr.Size = _mate[TempIndex].LabelRect[i].Ocr[j].Size;
ocr.Mode = _mate[TempIndex].LabelRect[i].Ocr[j].Mode;
rect.Ocr.Add(ocr);
}
temp.LabelRect.Add(rect);
}
return temp;
}
public CodeSplice.BarCode FindCode(int index)
{
CodeSplice.BarCode code = null;
if (index == -1) return code;
int n = 0;
for (int i = 0; i < _mate[TempIndex].LabelRect.Count; i++)
{
int c = _mate[TempIndex].LabelRect[i].CodeCount;
if (c + n < index + 1)
{
n += c;
}
else
{
for (int j = 0; j < _mate[TempIndex].LabelRect[i].CodeCount; j++)
{
if (n + j == index)
{
code = _mate[TempIndex].LabelRect[i].Code[j];
break;
}
}
}
if (code != null) break;
}
return code;
}
public void FindCode(CodeSplice.MaterialTemplate temp, int index, out int labelIndex, out int codeIndex)
{
labelIndex = -1;
codeIndex = -1;
if (index == -1) return;
int n = 0;
bool end = false;
for (int i = 0; i < temp.LabelRect.Count; i++)
{
int c = temp.LabelRect[i].CodeCount;
if (c + n < index + 1)
{
n += c;
}
else
{
for (int j = 0; j < temp.LabelRect[i].CodeCount; j++)
{
if (n + j == index)
{
labelIndex = i;
codeIndex = j;
end = true;
break;
}
}
}
if (end) break;
}
}
public void FindCodeID(CodeSplice.MaterialTemplate temp, int id, out int labelIndex, out int codeIndex)
{
labelIndex = -1;
codeIndex = -1;
bool end = false;
for (int i = 0; i < temp.LabelRect.Count; i++)
{
for (int j = 0; j < temp.LabelRect[i].CodeCount; j++)
{
if (temp.LabelRect[i].Code[j].ID == id)
{
labelIndex = i;
codeIndex = j;
end = true;
break;
}
}
if (end) break;
}
}
/// <summary>
/// 删除
/// </summary>
public void Delete()
{
if (TempIndex == -1) return;
_mate[TempIndex].Image.Dispose();
System.IO.File.Delete(_mate[TempIndex].FilePath);
System.IO.File.Delete(_mate[TempIndex].ImagePath);
_mate.RemoveAt(TempIndex);
TempIndex = -1;
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="name"></param>
/// <param name="temp"></param>
public void SaveAs(string name, CodeSplice.MaterialTemplate temp)
{
string path = CodeSplice.Common.MATERIAL_DIR + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
temp.ImagePath = path + ".jpg";
temp.FilePath = path + ".xml";
temp.Image.Save(temp.ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
_mate.Add(temp);
_doc = new XmlDocument();
_doc.AppendChild(_doc.CreateXmlDeclaration("1.0", System.Text.Encoding.UTF8.BodyName, null));
XmlElement root = _doc.CreateElement("Material");
_doc.AppendChild(root);
root.SetAttribute("Name", name);
for (int i = 0; i < temp.LabelRect.Count; i++)
{
XmlElement ele = _doc.CreateElement("Rect");
root.AppendChild(ele);
ele.SetAttribute("AvgAngle", temp.LabelRect[i].AvgAngle.ToString());
ele.SetAttribute("CodeCount", temp.LabelRect[i].CodeCount.ToString());
bool check = false;
XmlElement ele2;
for (int j = 0; j < temp.LabelRect[i].CodeCount; j++)
{
ele2 = _doc.CreateElement("Code");
ele.AppendChild(ele2);
ele2.SetAttribute("ID", temp.LabelRect[i].Code[j].ID.ToString());
ele2.SetAttribute("Center", string.Format("{0},{1}", temp.LabelRect[i].Code[j].Center.X, temp.LabelRect[i].Code[j].Center.Y));
ele2.SetAttribute("Angle", temp.LabelRect[i].Code[j].Angle.ToString());
ele2.SetAttribute("Text", temp.LabelRect[i].Code[j].Text);
ele2.SetAttribute("CodeType", temp.LabelRect[i].Code[j].CodeType);
ele2.SetAttribute("Width", temp.LabelRect[i].Code[j].Width.ToString());
ele2.SetAttribute("Height", temp.LabelRect[i].Code[j].Height.ToString());
ele2.SetAttribute("Distance", temp.LabelRect[i].Code[j].Distance.ToString());
ele2.SetAttribute("Mode", temp.LabelRect[i].Code[j].Mode);
if (temp.LabelRect[i].Code[j].Mode.Length > 0) check = true;
ele2.SetAttribute("Start", temp.LabelRect[i].Code[j].Start.ToString());
ele2.SetAttribute("Length", temp.LabelRect[i].Code[j].Length.ToString());
}
if (temp.LabelRect[i].Ocr.Count > 0) check = true;
for (int j = 0; j < temp.LabelRect[i].Ocr.Count; j++)
{
ele2 = _doc.CreateElement("Ocr");
ele.AppendChild(ele2);
ele2.SetAttribute("CodeID", temp.LabelRect[i].Ocr[j].CodeID.ToString());
ele2.SetAttribute("Offset", string.Format("{0},{1}", temp.LabelRect[i].Ocr[j].Offset.X, temp.LabelRect[i].Ocr[j].Offset.Y));
ele2.SetAttribute("Size", string.Format("{0},{1}", temp.LabelRect[i].Ocr[j].Size.Width, temp.LabelRect[i].Ocr[j].Size.Height));
ele2.SetAttribute("Mode", temp.LabelRect[i].Ocr[j].Mode);
}
temp.LabelRect[i].Check = check;
ele.SetAttribute("Check", check.ToString());
}
_doc.Save(temp.FilePath);
}
/// <summary>
/// 保存
/// </summary>
public void Save(CodeSplice.MaterialTemplate temp)
{
_mate[TempIndex] = temp;
_doc = new XmlDocument();
_doc.Load(_mate[TempIndex].FilePath);
XmlElement root = (XmlElement)_doc.LastChild;
root.RemoveAll();
root.SetAttribute("Name", temp.Name);
for (int i = 0; i < temp.LabelRect.Count; i++)
{
XmlElement ele = _doc.CreateElement("Rect");
root.AppendChild(ele);
ele.SetAttribute("AvgAngle", temp.LabelRect[i].AvgAngle.ToString());
ele.SetAttribute("CodeCount", temp.LabelRect[i].CodeCount.ToString());
bool check = false;
XmlElement ele2;
for (int j = 0; j < temp.LabelRect[i].CodeCount; j++)
{
ele2 = _doc.CreateElement("Code");
ele.AppendChild(ele2);
ele2.SetAttribute("ID", temp.LabelRect[i].Code[j].ID.ToString());
ele2.SetAttribute("Center", string.Format("{0},{1}", temp.LabelRect[i].Code[j].Center.X, temp.LabelRect[i].Code[j].Center.Y));
ele2.SetAttribute("Angle", temp.LabelRect[i].Code[j].Angle.ToString());
ele2.SetAttribute("Text", temp.LabelRect[i].Code[j].Text);
ele2.SetAttribute("CodeType", temp.LabelRect[i].Code[j].CodeType);
ele2.SetAttribute("Width", temp.LabelRect[i].Code[j].Width.ToString());
ele2.SetAttribute("Height", temp.LabelRect[i].Code[j].Height.ToString());
ele2.SetAttribute("Distance", temp.LabelRect[i].Code[j].Distance.ToString());
ele2.SetAttribute("Mode", temp.LabelRect[i].Code[j].Mode);
if (temp.LabelRect[i].Code[j].Mode.Length > 0) check = true;
ele2.SetAttribute("Start", temp.LabelRect[i].Code[j].Start.ToString());
ele2.SetAttribute("Length", temp.LabelRect[i].Code[j].Length.ToString());
}
if (temp.LabelRect[i].Ocr.Count > 0) check = true;
for (int j = 0; j < temp.LabelRect[i].Ocr.Count; j++)
{
ele2 = _doc.CreateElement("Ocr");
ele.AppendChild(ele2);
ele2.SetAttribute("CodeID", temp.LabelRect[i].Ocr[j].CodeID.ToString());
ele2.SetAttribute("Offset", string.Format("{0},{1}", temp.LabelRect[i].Ocr[j].Offset.X, temp.LabelRect[i].Ocr[j].Offset.Y));
ele2.SetAttribute("Size", string.Format("{0},{1}", temp.LabelRect[i].Ocr[j].Size.Width, temp.LabelRect[i].Ocr[j].Size.Height));
ele2.SetAttribute("Mode", temp.LabelRect[i].Ocr[j].Mode);
}
temp.LabelRect[i].Check = check;
ele.SetAttribute("Check", check.ToString());
}
_doc.Save(_mate[TempIndex].FilePath);
}
}
}