FrmAoiSetting.cs
26.3 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
using AccAOI.camera;
using AOI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccAOI
{
public partial class FrmAoiSetting : FrmBase
{
private control.ABaseControl aoiControl = null;
public static Image BaseImg = null;
public static Image TestImage = null;
private AoiProject Project = null;
private bool CanSel = true;
private string DefaultPath = "";
private string currProjectFileName = "";
public FrmAoiSetting()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}
public FrmAoiSetting(string programPath, Image image = null, string defaultPath = "")
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
if (!programPath.Equals(""))
{
// BaseImg = image;
// Project = new AoiProject(image);
string result = "";
Project = AoiProject.Load(programPath, out result);
if (!result.Equals(""))
{
MyMessage.Show("加载项目" + programPath + "失败:\r\n" + result);
}
else
{
currProjectFileName = programPath;
}
}
if (image != null)
{
if (Project != null)
{
Project.standardImage = image;
}
}
DefaultPath = defaultPath;
}
private void FrmAoiSetting_Load(object sender, EventArgs e)
{
LoadTypes();
//string defaultImg = "F:\\电路板图片\\照片 2创建于2019年4月19日 11_34_53.jpg";
//if (System.IO.File.Exists(defaultImg))
//{
// //读取图片内容
// Image file = (Image)Image.FromFile(defaultImg);
// BaseImg = new Bitmap(file);
// file.Dispose();
// imageBox1.Image = BaseImg;
//}
foreach (string str in CameraManager.hikNameList)
{
cmbCameraList.ItemAdd(str);
}
if (CameraManager.hikNameList.Count > 0)
{
cmbCameraList.Text = CameraManager.hikNameList[0];
}
if (Project != null)
{
ShowPorject();
}
}
private void LoadTypes()
{
comType.Clear();
comType.ItemAdd(ControlType.Mark);
comType.ItemAdd(ControlType.AOIBlob);
comType.ItemAdd(ControlType.AOIRGB);
comType.ItemAdd(ControlType.Match);
comType.Text = ControlType.Mark;
}
private void btnOpenImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title =AOIResourceCulture.GetValue( "打开本地图片");
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
//读取图片内容
Image file = Image.FromFile(fileName);
// BaseImg = (Image)Image.FromFile(fileName).Clone();
BaseImg = new Bitmap(file);
file.Dispose();
if (Project != null)
{
Project.standardImage = BaseImg;
}
imageBox1.Image = BaseImg;
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void btnGetCameraImg_Click(object sender, EventArgs e)
{
try
{
string camera = cmbCameraList.Text;
if (camera.Equals(""))
{
MyMessage.Show("清先选择相机");
return;
} //将图片保存到本地重新加载
// string filePath = Application.StartupPath + @"\aimage\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + DateTime.Now.Millisecond.ToString().PadLeft(3, '0') ;
string filePath = Application.StartupPath + @"\aimage\";
string fileName = @"\base.bmp";
using (Bitmap img = CameraManager.GetCamerImage(camera))
{
if (img != null)
{
string path = Path.GetFullPath(filePath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
img.Save(filePath + fileName, ImageFormat.Bmp);
}
else
{
MyMessage.Show("获取图片失败");
return;
}
}
GC.Collect();
Image file = (Image)Image.FromFile(filePath + fileName);
BaseImg = new Bitmap(file);
file.Dispose();
// BaseImg = (Image)Image.FromFile(filePath+ file).Clone();
//读取图片内容
//BaseImg = (Image)img.Clone();
if (Project != null)
{
Project.standardImage = BaseImg;
}
imageBox1.Image = BaseImg;
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
catch (Exception ex)
{
Console.WriteLine("btnGetCameraImg_Click Error:" + ex.ToString());
MyMessage.Show(ex.ToString());
}
}
private void btnOpenPro_Click(object sender, EventArgs e)
{
if (Project != null)
{
DialogResult dialResult = MyMessage.Show("是否打开新项目?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialResult.Equals(DialogResult.Yes))
{
CloseCurrAoi();
aoiList.ItemClear();
Project = null;
if (TestImage != null)
{
TestImage.Dispose();
TestImage = null;
}
if (BaseImg != null)
{
BaseImg.Dispose();
BaseImg = null;
}
if (testImageBox1.Image != null)
{
testImageBox1.Image.Dispose();
testImageBox1.Image = null;
}
if (this.imageBox1.Image != null)
{
this.imageBox1.Image.Dispose();
this.imageBox1.Image = null;
}
GC.Collect();
}
else
{
return;
}
}
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = AOIResourceCulture.GetValue("打开项目");
openDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
if (!String.IsNullOrEmpty(DefaultPath))
{
openDialog.InitialDirectory = DefaultPath;
}
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
// Project = new AoiProject(BaseImg);
string msg = "";
Project = AoiProject.Load(fileName, out msg);
if (!msg.Equals(""))
{
MyMessage.Show("加载项目" + fileName + "失败:\r\n{0}", msg);
}
else
{
currProjectFileName = fileName;
}
ShowPorject();
}
private void btnSavePro_Click(object sender, EventArgs e)
{
if (this.Project == null)
{
MyMessage.Show("没有项目可保存");
return;
}
SaveCurrAoi();
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Title = AOIResourceCulture.GetValue("保存项目");
saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
if (!String.IsNullOrEmpty(currProjectFileName))
{
saveFileDialog.InitialDirectory = Path.GetFullPath(currProjectFileName);
saveFileDialog.FileName = Path.GetFileName(currProjectFileName);
}
else if (!String.IsNullOrEmpty(DefaultPath))
{
saveFileDialog.InitialDirectory = DefaultPath;
}
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = saveFileDialog.FileName;
Project.Save(fileName);
MyMessage.Show("保存成功");
}
private void btnNewAoi_Click(object sender, EventArgs e)
{
if (BaseImg == null)
{
MyMessage.Show("清先选择基准图片");
return;
}
if (this.Project == null)
{
Project = new AoiProject(BaseImg);
}
if (comType.Text.Equals(""))
{
MyMessage.Show("请选择类型");
comType.Focus();
return;
}
string text = comType.Text;
AoiMethod methodInfo = ControlUtil.GetMethod(text);
string defaultName = Project.methodMap.Values.Count.ToString().PadLeft(2, '0') + "_" + text;
FrmMethodName frmName = new FrmMethodName(defaultName, new List<string>(Project.methodMap.Keys));
frmName.Text = AOIResourceCulture.GetValue("请输入新【{0}】名称",text);
DialogResult result = frmName.ShowDialog();
if (result.Equals(DialogResult.OK))
{
methodInfo.MethodName = frmName.MethodName;
}
else
{
return;
}
CloseCurrAoi();
// methodInfo.MethodName = Project.methodMap.Values.Count.ToString().PadLeft(2, '0') + "_" + text;
Project.methodMap.Add(methodInfo.MethodName, methodInfo);
ShowAoi(methodInfo);
aoiList.ItemAdd(methodInfo.MethodName);
aoiList.SelectedIndex = Project.methodMap.Count - 1;
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void ShowAoi(AoiMethod method)
{
CanSel = false;
imageBox1.SelectNone();
imageBox1.Image = BaseImg;
CanSel = true;
if (aoiControl == null)
{
aoiControl = ControlUtil.GetControl(method);
}
this.aoiControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
aoiControl.FormBorderStyle = FormBorderStyle.None;
aoiControl.StartPosition = FormStartPosition.CenterParent;
aoiControl.Location = new Point(0, 0);
aoiControl.BImageBox = this.imageBox1;
aoiControl.AoiInfo = method;
aoiControl.Size = panAoi.Size;
aoiControl.TitleName = method.MethodName;
aoiControl.TopLevel = false;
aoiControl.Parent = panAoi;
if (method.RoiPath != null)
{
currPath = method.RoiPath;
imageBox1.SelectionRegion = method.RoiPath;
//Image threshImage = CutImage(imageBox1.Image, currPath);
//cutImage = threshImage;
aoiControl.SetCurrPath(currPath, true);
}
aoiControl.Show();
aoiControl.ShowAoiInfo();
}
private void SaveCurrAoi()
{
if (aoiControl != null && aoiControl.Visible)
{
AoiMethod method = aoiControl.GetAoiInfo();
//更新
if (Project.methodMap.ContainsKey(method.MethodName))
{
Project.methodMap[method.MethodName] = method;
}
}
}
private void CloseCurrAoi()
{
SaveCurrAoi();
if (aoiControl != null)
{
if (aoiControl.Visible)
{
aoiControl.Close();
}
aoiControl.Dispose();
aoiControl = null;
}
}
private void ShowPorject()
{
if (Project != null)
{
CloseCurrAoi();
aoiList.ItemClear();
BaseImg = Project.standardImage;
imageBox1.Image = Project.standardImage;
imageBox1.SelectionRegion = new GraphicsPath();
if (Project.methodMap.Count > 0)
{
int index = 0;
foreach (string key in Project.methodMap.Keys)
{
aoiList.ItemAdd(key);
if (index.Equals(0))
{
aoiList.SelectedIndex = index;
ShowAoi(Project.methodMap[key]);
}
index++;
}
}
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
}
private void aoiList_IndexChanged(object sender )
{
// int index = aoiList.SelectedIndex;
if (aoiList.SelectedIndex >= 0 && (Project != null))
{
string text = aoiList.Text;
// List<AoiMethod> methods = new List<AoiMethod>(Project.methodMap.Values);
if (Project.methodMap.ContainsKey(text ))
{
AoiMethod method = Project.methodMap[text];
if (aoiControl != null && aoiControl.TitleName.Equals(method.MethodName))
{
return;
}
else
{
CloseCurrAoi();
ShowAoi(method);
}
}
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
}
private Image cutImage = null;
private GraphicsPath currPath = null;
private void imageBox1_SelectionRegionChanged(object sender, EventArgs e)
{
try
{
if (!CanSel)
{
return;
}
if (imageBox1.Image == null || BaseImg == null || aoiControl == null)
{
return;
}
currPath = imageBox1.SelectionRegion;
aoiControl.SetCurrPath(currPath);
}
catch (Exception ex)
{
}
}
private void btnExcute_Click(object sender, EventArgs e)
{
SaveCurrAoi();
CanSel = false;
if (BaseImg == null)
{
return;
}
if (this.Project == null)
{
return;
}
if (TestImage == null)
{
MyMessage.Show("请选择测试图片");
return;
}
Image outImage = null;
List<ResultBean> result = Project.CheckAll(TestImage, out outImage);
if (outImage != null)
{
if (!this.testImageBox1.Visible)
{
btnImageChange_Click(null, null);
}
this.testImageBox1.SelectNone();
this.testImageBox1.Image = outImage;
}
GC.Collect();
CanSel = true;
}
private void btnDel_Click(object sender, EventArgs e)
{
if (BaseImg == null)
{
return;
}
if (this.Project == null)
{
return;
}
if (this.aoiControl != null)
{
DialogResult result = MyMessage.Show("确定删除{0} ?", "确认提示", MessageBoxButtons.OKCancel, aoiControl.AoiInfo.MethodName);
if (result.Equals(DialogResult.OK))
{
if (Project.methodMap.ContainsKey(aoiControl.AoiInfo.MethodName))
{
Project.methodMap.Remove(aoiControl.AoiInfo.MethodName);
ShowPorject();
}
}
}
}
private void btnImageChange_Click(object sender, EventArgs e)
{
if (testImageBox1.Visible)
{
testImageBox1.Visible = false;
imageBox1.Visible = true;
btnImageChange.Text = AOIResourceCulture.GetValue("显示测试图片");
lblCurrImage.Text = AOIResourceCulture.GetValue("基准图:");
lblCurrImage.ForeColor = Color.LawnGreen;
}
else
{
testImageBox1.Visible = true;
imageBox1.Visible = false;
btnImageChange.Text = AOIResourceCulture.GetValue("显示基准图片");
lblCurrImage.Text = AOIResourceCulture.GetValue("测试/效果图:");
lblCurrImage.ForeColor = Color.Orange;
}
}
private void flatButton2_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = AOIResourceCulture.GetValue("打开本地图片");
openDialog.Filter = "All Supported Images (*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png)|*.bmp;*.dib;*.rle;*.gif;*.jpg;*.png|Bitmaps (*.bmp;*.dib;*.rle)|*.bmp;*.dib;*.rle|Graphics Interchange Format (*.gif)|*.gif|Joint Photographic Experts (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|All Files (*.*)|*.*";
openDialog.DefaultExt = "png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openDialog.FileName;
//读取图片内容
// TestImage = (Image)Image.FromFile(fileName).Clone();
Image file = (Image)Image.FromFile(fileName);
TestImage = new Bitmap(file);
file.Dispose();
testImageBox1.Image = TestImage;
if (testImageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void FrmAoiSetting_Shown(object sender, EventArgs e)
{
testImageBox1.Size = imageBox1.Size;
testImageBox1.Location = imageBox1.Location;
testImageBox1.Visible = false;
}
private void flatButton1_Click(object sender, EventArgs e)
{
string camera = cmbCameraList.Text;
if (camera.Equals(""))
{
MyMessage.Show("清先选择相机");
return;
}
string filePath = Application.StartupPath + @"\aimage\";
string fileName = @"\test.bmp";
using (Bitmap img = CameraManager.GetCamerImage(camera))
{
if (img != null)
{
string path = Path.GetFullPath(filePath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
img.Save(filePath + fileName, ImageFormat.Bmp);
}
else
{
MyMessage.Show("获取图片失败");
return;
}
}
GC.Collect();
Image file = (Image)Image.FromFile(filePath + fileName);
TestImage = new Bitmap(file);
file.Dispose();
//读取图片内容
testImageBox1.Image = TestImage;
if (testImageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
private void btnUpdateName_Click(object sender, EventArgs e)
{
// int index = aoiList.SelectedIndex;
if (aoiList.SelectedIndex >= 0 && (Project != null)&&(aoiControl!=null))
{
string oldName = aoiList.Text;
if (Project.methodMap.ContainsKey(oldName))
{
// AoiMethod method = methods[index];
// string oldName = methods[index].MethodName;
List<string> allName = new List<string>(Project.methodMap.Keys);
allName.Remove(oldName);
FrmMethodName frmName = new FrmMethodName(oldName, allName);
frmName.Text = AOIResourceCulture.GetValue("请输入修改后的名称");
DialogResult result = frmName.ShowDialog();
if (result.Equals(DialogResult.OK))
{
string newName= frmName.MethodName;
if (newName.Equals(oldName))
{
return;
}
Project.methodMap[oldName].MethodName = newName;
List<AoiMethod> methods = new List<AoiMethod>(Project.methodMap.Values);
//Project.methodMap.Remove(oldName);
//Project.methodMap.Add(newName, method);
Project.methodMap.Clear();
aoiList.ItemClear();
foreach (AoiMethod m in methods)
{
Project.methodMap.Add(m.MethodName, m);
aoiList.ItemAdd(m.MethodName);
}
aoiControl.TitleName = newName;
}
else
{
return;
}
}
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
}
private void lblCurrImage_Click(object sender, EventArgs e)
{
try
{
Image needSaveImage = null;
if (testImageBox1.Visible)
{
needSaveImage = testImageBox1.Image;
}
else
{
needSaveImage = imageBox1.Image;
}
if (needSaveImage != null)
{
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Title = AOIResourceCulture.GetValue("保存项目");
saveFileDialog.Filter = "(*.bmp)|*.bmp";
System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = saveFileDialog.FileName;
needSaveImage.Save(fileName, ImageFormat.Bmp);
MyMessage.Show("保存成功");
}
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnLan_Click(object sender, EventArgs e)
{
if (AOIResourceCulture.CurrLanguage.Equals(AOIResourceCulture.China))
{
AOIResourceCulture.SetCurrentCulture(AOIResourceCulture.English);
}
else
{
AOIResourceCulture.SetCurrentCulture(AOIResourceCulture.China);
}
LanguageProcess();
LanguagePro();
if (aoiControl != null)
{
aoiControl.LanguageProcess();
aoiControl.LanguagePro();
}
}
public override void LanguagePro()
{
if (AOIResourceCulture.CurrLanguage.Equals(AOIResourceCulture.China))
{
btnLan.Text = AOIResourceCulture.GetValue("英文");
}
else
{
btnLan.Text = AOIResourceCulture.GetValue("中文");
}
LoadTypes();
}
}
}