FrmTemplate.cs
35.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodeSplicing
{
public partial class FrmTemplate : Form
{
private Bitmap bmpSrc;
private bool idxSelect = false;
private System.IO.Ports.SerialPort port;
private List<ToolStripItem> mnuCamera = new List<ToolStripItem>();
private Asa.ImageZoom imgZoom = new Asa.ImageZoom();
//private Asa.Template template = new Asa.Template();
private Asa.Halcon halcon = new Asa.Halcon();
//private Asa.ZXingCode code = new Asa.ZXingCode();
//private Asa.ImageZoom imgzoom = new Asa.ImageZoom();
//private List<Asa.AreaRect> ar;
//private List<List<Asa.BarCodeInfo>> info;
private List<Asa.TempArea> tempArea = new List<Asa.TempArea>();
private List<Panel> pnlCode = new List<Panel>();
private List<Label> lblID = new List<Label>();
private List<CheckBox> chkEnabled = new List<CheckBox>();
private List<Label> lblCode = new List<Label>();
//private List<ComboBox> cboCode = new List<ComboBox>();
//private Bitmap srcBmp;
public FrmTemplate()
{
InitializeComponent();
}
//====================自定义方法====================
/// <summary>
/// 语言改变时设为true
/// </summary>
public bool LanguageChanged { get; private set; }
private void WriteCmd()
{
if (!port.IsOpen) return;
string cmd = "S";
if (ChkCh1.Checked)
cmd += string.Format("{0:000}T", trackBar1.Value);
else
cmd += "001F";
if (ChkCh2.Checked)
cmd += string.Format("{0:000}T", trackBar2.Value);
else
cmd += "001F";
cmd += "001F001FC#";
port.Write(cmd);
}
private string FormatRid(object val)
{
string format = new string('0', Convert.ToInt32(NudLen.Value));
format = "{0:" + format + "}";
return string.Format(format, val);
}
private void InitLabel()
{
CboLabelTextField.Items.AddRange(Asa.Common.Config.Field.ToArray());
Asa.Common.Label.SetImage(PicLabel);
if (Asa.Common.Label.Count > 0)
{
CboLabel.Items.AddRange(Asa.Common.Label.Name);
int idx = Asa.Common.Label.NameFindIndex(Asa.Common.Config.LabelDefault);
if (idx == -1)
CboLabel.SelectedIndex = 0;
else
{
CboLabel.SelectedIndex = idx;
CboLabel.Items[idx] += "(默认)";
}
}
Asa.Common.Label.UpdateTextRect += UpdateTextRect;
Asa.Common.Label.UpdateTextString += UpdateTextString;
Asa.Common.Label.UpdateCodeRect += UpdateCodeRect;
TxtLabelTextPrefix.LostFocus += TxtLabelTextPrefix_LostFocus;
TxtLabelTextSuffix.LostFocus += TxtLabelTextSuffix_LostFocus;
}
private void InitQRcode()
{
LstCodeField.Items.AddRange(Asa.Common.Config.Field.ToArray());
LstCodeSplice.Items.AddRange(Asa.Common.Label.QRcodeSplice.ToArray());
NudStart.Value = Asa.Common.Config.RID[0];
NudLen.Value = Asa.Common.Config.RID[1];
LblCurrent.Text = FormatRid(Asa.Common.Config.RID[2]);
UpdateCodeSplice();
}
private void InitTemplate()
{
//模板列表
CboTemplate.Items.AddRange(Asa.Common.Template.Name);
if (Asa.Common.Template.Count > 0)
{
CboTemplate.SelectedIndex = 0;
}
}
private void InitCamera()
{
Asa.Common.LogOut("相机个数:" + Asa.Common.Camera.Count);
if (Asa.Common.Camera.Count == 0)
{
TssBar.Visible = false;
LstCamera.Items.Clear();
}
else
{
TssBar.Visible = true;
LstCamera.Items.AddRange(Asa.Common.Camera.CameraName);
for (int i = 0; i < Asa.Common.Camera.Count; i++)
{
ToolStripItem item = CmsCamera.Items.Add(Asa.Common.Camera.CameraName[i]);
item.Click += MnuCameraItem_Click;
mnuCamera.Add(item);
}
}
}
private void UpdateLabelSize()
{
LblLabelSize.Text = string.Format("{0}, {1}", Asa.Common.Label.Size.Width, Asa.Common.Label.Size.Height);
}
private void UpdateTextString(string s1, string s2, string field)
{
idxSelect = true;
TxtLabelTextPrefix.Text = s1;
TxtLabelTextSuffix.Text = s2;
CboLabelTextField.Text = field;
idxSelect = false;
}
private void UpdateTextRect(Rectangle rect)
{
LblLabelTextRect.Text = string.Format("{0}, {1}, {2}, {3}", rect.X, rect.Y, rect.Width, rect.Height);
}
private void UpdateCodeRect()
{
LblQRcodeRect.Text = string.Format("{0}, {1}, {2}, {3}", Asa.Common.Label.QRcodeRect.X, Asa.Common.Label.QRcodeRect.Y, Asa.Common.Label.QRcodeRect.Width, Asa.Common.Label.QRcodeRect.Height);
}
private void UpdateCodeSplice()
{
LblQRcodeSplice.Text = LblCodeSplice.Text = string.Join(Asa.Common.Config.Chars, Asa.Common.Label.QRcodeSplice);
}
/// <summary>
/// 清除模板控件
/// </summary>
private void ClearTemplateControl()
{
for (int i = 0; i < pnlCode.Count; i++)
panel1.Controls.Remove(pnlCode[i]);
lblCode.Clear();
lblID.Clear();
chkEnabled.Clear();
pnlCode.Clear();
}
/// <summary>
/// 加载模板控件,需要先调用 ClearTemplateControl
/// </summary>
private void LoadTemplateControl()
{
if (tempArea == null || tempArea.Count == 0) return;
int border = 12;
int itemHeight = 28;
for (int i = 0; i < tempArea.Count; i++)
{
Panel pnl = new Panel();
pnl.BorderStyle = BorderStyle.FixedSingle;
pnl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
int wPanel = panel1.ClientSize.Width - border * 2; //自动添加滚动条后宽度会改变
pnl.Size = new Size(wPanel, 40 + itemHeight * tempArea[i].Item.Count);
if (i == 0)
pnl.Location = new Point(border, 93);
else
pnl.Location = new Point(border, pnlCode[i - 1].Top + pnlCode[i - 1].Height + 6);
Label lbl = new Label();
lbl.Text = "ID:" + (i + 1);
lbl.ForeColor = Color.Red;
lbl.Font = new Font("微软雅黑", 12f);
lbl.Size = new Size(60, 20);
lbl.Location = new Point(6, 6);
lbl.TextAlign = ContentAlignment.MiddleLeft;
lblID.Add(lbl);
pnl.Controls.Add(lbl);
CheckBox chk = new CheckBox();
chk.Text = "启用";
chk.Checked = tempArea[i].Enabled;
chk.Font = new Font("宋体", 12f);
chk.Size = new Size(100, 20);
chk.Location = new Point(72, 6);
chk.CheckedChanged += Chk_CheckedChanged;
chkEnabled.Add(chk);
pnl.Controls.Add(chk);
for (int j = 0; j < tempArea[i].Item.Count; j++)
{
lbl = new Label();
if (tempArea[i].Item[j].Field.Length > 0)
lbl.Text = tempArea[i].Item[j].Field + ": ";
int start = tempArea[i].Item[j].Start;
int len = tempArea[i].Item[j].Length;
if (start >= tempArea[i].Item[j].Text.Length)
lbl.Text += tempArea[i].Item[j].Text;
else
{
if (start + len > tempArea[i].Item[j].Text.Length)
len = tempArea[i].Item[j].Text.Length - start;
lbl.Text += tempArea[i].Item[j].Text.Substring(start, len);
}
lbl.Font = new Font("微软雅黑", 12f);
lbl.Size = new Size(pnl.Width - 14, itemHeight);
lbl.TextAlign = ContentAlignment.MiddleLeft;
lbl.Location = new Point(6, 32 + itemHeight * j);
lbl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
lbl.DoubleClick += Lbl_DoubleClick;
lblCode.Add(lbl);
pnl.Controls.Add(lbl);
}
panel1.Controls.Add(pnl);
pnlCode.Add(pnl);
}
}
//====================窗口事件====================
private void FrmTemplate_Load(object sender, EventArgs e)
{
Asa.Common.Label.TextIndex = -1;
imgZoom.ShowControl = PicImage;
Asa.Common.Language.SetLanguage(this);
InitLabel();
InitQRcode();
InitTemplate();
InitCamera();
LstPrinter.Items.AddRange(Asa.Common.Config.PrinterList.ToArray());
LstPrinter.Text = Asa.Common.Config.Printer;
LstLanguage.Items.AddRange(Asa.Common.Language.Name);
idxSelect = true;
LstLanguage.Text = Asa.Common.Config.Language;
idxSelect = false;
LblValue1.Text = trackBar1.Value.ToString();
LblValue2.Text = trackBar2.Value.ToString();
port = new System.IO.Ports.SerialPort();
port.PortName = Asa.Common.Config.LightPort;
port.BaudRate = 19200;
port.DataBits = 8;
port.StopBits = System.IO.Ports.StopBits.One;
}
#region 标签
private void CboLabel_SelectedIndexChanged(object sender, EventArgs e)
{
Asa.Common.Label.Load(CboLabel.SelectedIndex);
Asa.Common.Label.Update();
UpdateLabelSize();
UpdateCodeRect();
UpdateCodeSplice();
UpdateTextRect(new Rectangle());
UpdateTextString("", "", "");
}
private void BtnLabelAdd_Click(object sender, EventArgs e)
{
string s = Microsoft.VisualBasic.Interaction.InputBox("输入新的标签名称:", "");
if (s.Length == 0) return;
Asa.Common.Label.Create(s);
CboLabel.Items.Add(s);
CboLabel.SelectedIndex = CboLabel.Items.Count - 1;
CboLabelTextField.SelectedIndex = -1;
}
private void BtnLabelDel_Click(object sender, EventArgs e)
{
if (CboLabel.SelectedIndex == -1) return;
DialogResult dr = MessageBox.Show("确定要删除" + CboLabel.Text + "吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
int idx = CboLabel.SelectedIndex;
Asa.Common.Label.Delete(idx);
CboLabel.Items.RemoveAt(idx);
CboLabelTextField.SelectedIndex = -1;
if (CboLabel.Items.Count > 0)
CboLabel.SelectedIndex = 0;
}
private void LblLabelSize_DoubleClick(object sender, EventArgs e)
{
FrmResize frm = new FrmResize(Asa.Common.Label.Size);
if (frm.ShowDialog() != DialogResult.OK) return;
Asa.Common.Label.Size = frm.Rect.Size;
Asa.Common.Label.Update();
UpdateLabelSize();
}
private void LblQRcodeRect_DoubleClick(object sender, EventArgs e)
{
FrmResize frm = new FrmResize(Asa.Common.Label.QRcodeRect);
if (frm.ShowDialog() != DialogResult.OK) return;
Asa.Common.Label.QRcodeRect = frm.Rect;
Asa.Common.Label.Update();
UpdateCodeRect();
}
private void LblLabelTextRect_DoubleClick(object sender, EventArgs e)
{
FrmResize frm = new FrmResize(Asa.Common.Label.CurrTextRect);
if (frm.ShowDialog() != DialogResult.OK) return;
Asa.Common.Label.CurrTextRect = frm.Rect;
Asa.Common.Label.Update();
UpdateTextRect(frm.Rect);
}
private void BtnLabelTextAdd_Click(object sender, EventArgs e)
{
Asa.Common.Label.AddText();
UpdateTextRect(Asa.Common.Label.LastTextRect);
}
private void BtnLabelTextDel_Click(object sender, EventArgs e)
{
if (Asa.Common.Label.TextIndex < 0) return;
DialogResult dr = MessageBox.Show("确定要删除选中的文本吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
Asa.Common.Label.DelText();
}
private void BtnLabelTextFont_Click(object sender, EventArgs e)
{
if (Asa.Common.Label.TextIndex < 0) return;
FontDialog dlg = new FontDialog();
dlg.ShowEffects = false;
dlg.Font = Asa.Common.Label.CurrTextFont;
if (dlg.ShowDialog() != DialogResult.OK) return;
Asa.Common.Label.CurrTextFont = dlg.Font;
Asa.Common.Label.Update();
}
private void TxtLabelTextPrefix_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Asa.Common.Label.UpdateText(TxtLabelTextPrefix.Text, TxtLabelTextSuffix.Text, CboLabelTextField.Text);
}
}
private void TxtLabelTextSuffix_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Asa.Common.Label.UpdateText(TxtLabelTextPrefix.Text, TxtLabelTextSuffix.Text, CboLabelTextField.Text);
}
}
private void TxtLabelTextSuffix_LostFocus(object sender, EventArgs e)
{
Asa.Common.Label.UpdateText(TxtLabelTextPrefix.Text, TxtLabelTextSuffix.Text, CboLabelTextField.Text);
}
private void TxtLabelTextPrefix_LostFocus(object sender, EventArgs e)
{
Asa.Common.Label.UpdateText(TxtLabelTextPrefix.Text, TxtLabelTextSuffix.Text, CboLabelTextField.Text);
}
private void CboLabelTextField_SelectedIndexChanged(object sender, EventArgs e)
{
if (idxSelect) return;
Asa.Common.Label.UpdateText(TxtLabelTextPrefix.Text, TxtLabelTextSuffix.Text, CboLabelTextField.Text);
}
private void BtnLabelSave_Click(object sender, EventArgs e)
{
Asa.Common.Label.Save();
Asa.Common.Config.RID[0] = Convert.ToInt32(NudStart.Value);
Asa.Common.Config.RID[1] = Convert.ToInt32(NudLen.Value);
Asa.Common.Config.UpdateRid();
MessageBox.Show("保存完成。", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void BtnLabelDefault_Click(object sender, EventArgs e)
{
string[] name = Asa.Common.Label.Name;
int idx = Asa.Common.Label.NameFindIndex(Asa.Common.Config.LabelDefault);
CboLabel.Items[idx] = name[idx];
Asa.Common.Config.UpdateLabel(name[CboLabel.SelectedIndex]);
CboLabel.Items[CboLabel.SelectedIndex] = name[CboLabel.SelectedIndex] + "(默认)";
}
#endregion
#region 二维码
private void BtnCodeAdd_Click(object sender, EventArgs e)
{
string s = Microsoft.VisualBasic.Interaction.InputBox("输入字段名:");
if (s.Length == 0) return;
LstCodeField.Items.Add(s);
Asa.Common.Config.FieldAdd(s);
Asa.Common.LogOut("Add Field " + s);
}
private void BtnCodeDelete_Click(object sender, EventArgs e)
{
if (LstCodeField.SelectedIndex == -1) return;
string s = LstCodeField.Text;
DialogResult dr = MessageBox.Show("确定要删除 " + s + " 吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
int n = LstCodeField.SelectedIndex;
LstCodeField.Items.RemoveAt(n);
Asa.Common.Config.FieldDel(n);
if (LstCodeField.Items.Count > 0)
LstCodeField.SelectedIndex = 0;
do
{
n = Asa.Common.Label.QRcodeSplice.FindIndex(ss => ss == s);
if (n == -1) continue;
LstCodeSplice.Items.RemoveAt(n);
Asa.Common.Label.QRcodeSplice.RemoveAt(n);
} while (n > -1);
UpdateCodeSplice();
Asa.Common.LogOut("Del Field " + s);
}
private void BtnCodeAppend_Click(object sender, EventArgs e)
{
if (LstCodeField.SelectedIndex == -1) return;
LstCodeSplice.Items.Add(LstCodeField.Text);
Asa.Common.Label.QRcodeSplice.Add(LstCodeField.Text);
UpdateCodeSplice();
}
private void BtnCodeUp_Click(object sender, EventArgs e)
{
int idx = LstCodeSplice.SelectedIndex;
if (idx < 1) return;
object item = LstCodeSplice.Items[idx];
LstCodeSplice.Items.RemoveAt(idx);
LstCodeSplice.Items.Insert(idx - 1, item);
string s = Asa.Common.Label.QRcodeSplice[idx];
Asa.Common.Label.QRcodeSplice.RemoveAt(idx);
Asa.Common.Label.QRcodeSplice.Insert(idx - 1, s);
LstCodeSplice.SelectedIndex = idx - 1;
UpdateCodeSplice();
}
private void BtnCodeDown_Click(object sender, EventArgs e)
{
int idx = LstCodeSplice.SelectedIndex;
if (idx > LstCodeSplice.Items.Count - 2) return;
object item = LstCodeSplice.Items[idx];
LstCodeSplice.Items.RemoveAt(idx);
LstCodeSplice.Items.Insert(idx + 1, item);
string s = Asa.Common.Label.QRcodeSplice[idx];
Asa.Common.Label.QRcodeSplice.RemoveAt(idx);
Asa.Common.Label.QRcodeSplice.Insert(idx + 1, s);
LstCodeSplice.SelectedIndex = idx + 1;
UpdateCodeSplice();
}
private void BtnCodeRemove_Click(object sender, EventArgs e)
{
int idx = LstCodeSplice.SelectedIndex;
if (idx < 0) return;
LstCodeSplice.Items.RemoveAt(idx);
Asa.Common.Label.QRcodeSplice.RemoveAt(idx);
if (LstCodeSplice.Items.Count > 0)
LstCodeSplice.SelectedIndex = 0;
UpdateCodeSplice();
}
private void NudStart_ValueChanged(object sender, EventArgs e)
{
LblExample.Text = FormatRid(NudStart.Value);
}
private void NudLen_ValueChanged(object sender, EventArgs e)
{
LblExample.Text = FormatRid(NudStart.Value);
LblCurrent.Text = FormatRid(Asa.Common.Config.RID[2]);
}
private void BtnInitRid_Click(object sender, EventArgs e)
{
Asa.Common.Config.RID[2] = Convert.ToInt32(NudStart.Value);
LblCurrent.Text = FormatRid(Asa.Common.Config.RID[2]);
}
#endregion
private void PicImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
CmsCamera.Show((Control)sender, e.X, e.Y);
}
private void MnuCameraItem_Click(object sender, EventArgs e)
{
string s = ((ToolStripMenuItem)sender).Text;
bool bln = Asa.Common.Camera.Open(s);
if (!bln)
{
Asa.Common.LogOut("相机" + s + "打开失败");
return;
}
Asa.Common.Camera.GrabOne();
bmpSrc = Asa.Common.Camera.Image;
imgZoom.Update(bmpSrc);
Asa.Common.Camera.Close();
}
private void TsmLocalImage_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|所有文件|*.*";
if (dlg.ShowDialog() != DialogResult.OK) return;
bmpSrc = new Bitmap(dlg.FileName);
imgZoom.Update(bmpSrc);
}
private void TsmReload_Click(object sender, EventArgs e)
{
foreach (ToolStripItem item in mnuCamera)
CmsCamera.Items.Remove(item);
mnuCamera.Clear();
Asa.Common.Camera.Load();
InitCamera();
}
private void CboTemplate_SelectedIndexChanged(object sender, EventArgs e)
{
if (CboTemplate.SelectedIndex == -1) return;
Asa.Common.Template.Load(CboTemplate.SelectedIndex);
tempArea = Asa.Common.Template.currArea;
bmpSrc = new Bitmap(Asa.Common.Template.BmpPath);
imgZoom.Update(bmpSrc);
imgZoom.ClearOutline();
for (int i = 0; i < tempArea.Count; i++)
imgZoom.AddOutline(tempArea[i].Outline, tempArea[i].Center, (i + 1).ToString());
imgZoom.Update();
ClearTemplateControl();
LoadTemplateControl();
}
private void BtnBarcode_Click(object sender, EventArgs e)
{
Asa.Common.LogOut("模板,识别条码");
if (bmpSrc == null)
{
Asa.Common.LogOut("模板,条码识别,没有图像");
return;
}
FindArea();
imgZoom.ClearOutline();
for (int i = 0; i < tempArea.Count; i++)
imgZoom.AddOutline(tempArea[i].Outline, tempArea[i].Center, (i + 1).ToString());
imgZoom.Update();
Asa.Common.LogOut("更新显示图像控件");
CboTemplate.SelectedIndex = -1;
ClearTemplateControl();
LoadTemplateControl();
Asa.Common.LogOut("加载模板控件");
}
private void BtnTemplateDel_Click(object sender, EventArgs e)
{
if (CboTemplate.SelectedIndex == -1) return;
DialogResult dr = MessageBox.Show("确定要删除 " + CboTemplate.Text + " 吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == DialogResult.No) return;
int idx = CboLabel.SelectedIndex;
Asa.Common.Template.Del(idx);
CboTemplate.Items.RemoveAt(idx);
if (CboTemplate.Items.Count > 0)
CboTemplate.SelectedIndex = 0;
}
private void Lbl_DoubleClick(object sender, EventArgs e)
{
Label lbl = sender as Label;
int idx = lblCode.FindIndex(ll => ll == lbl);
int n = 0;
while (idx >= tempArea[n].Item.Count)
{
idx -= tempArea[n].Item.Count;
n++;
if (n == tempArea.Count) break;
}
FrmCodeText frm = new FrmCodeText(tempArea[n].Item[idx].Text, tempArea[n].Item[idx].Field, tempArea[n].Item[idx].Start, tempArea[n].Item[idx].Length);
if (frm.ShowDialog() != DialogResult.OK) return;
tempArea[n].Item[idx].Text = frm.Texts;
tempArea[n].Item[idx].Field = frm.Field;
tempArea[n].Item[idx].Start = frm.Start;
tempArea[n].Item[idx].Length = frm.Len;
if (tempArea[n].Item[idx].Field.Length > 0)
lbl.Text = tempArea[n].Item[idx].Field + ": ";
if (tempArea[n].Item[idx].Start >= tempArea[n].Item[idx].Text.Length)
lbl.Text += tempArea[n].Item[idx].Text;
else
{
if (tempArea[n].Item[idx].Start + tempArea[n].Item[idx].Length > tempArea[n].Item[idx].Text.Length)
tempArea[n].Item[idx].Length = tempArea[n].Item[idx].Text.Length - tempArea[n].Item[idx].Start;
lbl.Text += tempArea[n].Item[idx].Text.Substring(tempArea[n].Item[idx].Start, tempArea[n].Item[idx].Length);
}
}
private void Chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
int n = chkEnabled.FindIndex(tt => tt == chk);
if (n == -1) return;
tempArea[n].Enabled = chk.Checked;
}
private void BtnExit_Click(object sender, EventArgs e)
{
port.Close();
Close();
}
private void BtnOpen_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
BtnOpen.Text = "打开";
}
else
{
port.Open();
BtnOpen.Text = "关闭";
}
}
private void ChkCh1_CheckedChanged(object sender, EventArgs e)
{
WriteCmd();
}
private void ChkCh2_CheckedChanged(object sender, EventArgs e)
{
WriteCmd();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
WriteCmd();
LblValue1.Text = trackBar1.Value.ToString();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
WriteCmd();
LblValue2.Text = trackBar2.Value.ToString();
}
private void BtnTemplateSave_Click(object sender, EventArgs e)
{
if (CboTemplate.SelectedIndex == -1)
{
string name = Microsoft.VisualBasic.Interaction.InputBox("输入模板的名称:", "模板名称", "");
if (name.Length == 0) return;
Asa.Common.Template.Save(name, bmpSrc, tempArea);
CboTemplate.Items.Add(name);
CboTemplate.SelectedIndex = CboTemplate.Items.Count - 1;
}
else
{
Asa.Common.Template.Save(tempArea);
}
MessageBox.Show("保存完成。", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void FindArea()
{
tempArea = new List<Asa.TempArea>();
List<Asa.AreaRect> rect = new List<Asa.AreaRect>();
List<List<Asa.BarCodeInfo>> info = new List<List<Asa.BarCodeInfo>>();
//找出所有区域
Asa.OpenCV opencv = new Asa.OpenCV();
List<Asa.AreaRect> area = opencv.extractRect(bmpSrc);
if (area.Count == 0) return;
//整个区域条码
List<Asa.BarCodeInfo> codeInfo = new List<Asa.BarCodeInfo>();
halcon.DecodeBarCode(bmpSrc, out codeInfo);
if (codeInfo.Count == 0) return;
//每个区域扫码
foreach (Asa.AreaRect ar in area)
{
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddPolygon(ar.Points);
Region region = new Region();
region.MakeEmpty();
region.Union(path);
//查找条码的中点是否在区域中
bool inside;
List<Asa.BarCodeInfo> code = new List<Asa.BarCodeInfo>();
foreach (Asa.BarCodeInfo bc in codeInfo)
{
inside = region.IsVisible(bc.centerX, bc.centerY);
if (inside) code.Add(bc);
}
if (code.Count > 0)
{
rect.Add(ar);
info.Add(code);
}
}
//排序
for (int i = 0; i < info.Count; i++)
{
//点到直线的距离
if (info[i].Count < 2) continue;
List<double> distance = new List<double>();
for (int j = 0; j < info[i].Count; j++)
{
double angle = info[i][j].angle > 0 ? 0 - info[i][j].angle : info[i][j].angle; //条码角度对应于数学坐标轴,C#坐标轴Y轴为向下
angle = angle * Math.PI / 180; //角度转弧度
double d = Math.Tan(angle) * info[i][j].centerX - info[i][j].centerY; //分子
d /= Math.Sqrt(Math.Pow(Math.Tan(angle), 2) + 1); //分母
d = Math.Abs(d); //绝对值
distance.Add(d);
}
//根据距离排序
for (int j = 0; j < distance.Count; j++)
{
int idx = j;
double min = distance[j];
for (int k = j; k < distance.Count; k++)
{
if (min > distance[k])
{
min = distance[k];
idx = k;
}
}
if (idx == j) continue;
double dbl = distance[idx];
distance.RemoveAt(idx);
distance.Insert(j, dbl);
Asa.BarCodeInfo bci = info[i][idx];
info[i].RemoveAt(idx);
info[i].Insert(j, bci);
}
}
for (int i = 0; i < rect.Count; i++)
{
Asa.TempArea temp = new Asa.TempArea();
temp.Enabled = false;
temp.Size = rect[i].Size;
temp.Angle = rect[i].Angle;
temp.Center = rect[i].Center;
temp.Outline = rect[i].Points;
for (int j = 0; j < info[i].Count; j++)
{
Asa.TempItem item = new Asa.TempItem();
item.Angle = info[i][j].angle;
item.Text = info[i][j].text;
item.Start = 0;
item.Length = item.Text.Length;
item.Center = new PointF(info[i][j].centerX, info[i][j].centerY);
temp.Item.Add(item);
}
tempArea.Add(temp);
}
}
private void LstLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
if (idxSelect) return;
Asa.Common.Language.Index = LstLanguage.SelectedIndex;
Asa.Common.Language.SetLanguage(this);
Asa.Common.Config.UpdateLanguage(LstLanguage.Text);
LanguageChanged = true;
}
//private void LoadTemplateControl(List<List<Asa.BarCodeInfo>> info)
//{
// if (info == null || info.Count == 0) return;
// pnlCode = new Panel[info.Count];
// lblID = new Label[info.Count];
// chkEnabled = new CheckBox[info.Count];
// int wPanel = panel2.ClientSize.Width - Asa.Common.FIELD_BORDER * 2;
// int wLabel = (wPanel - 20) / 2;
// for (int i = 0; i < info.Count; i++)
// {
// pnlCode[i] = new Panel();
// pnlCode[i].BorderStyle = BorderStyle.FixedSingle;
// pnlCode[i].Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
// wPanel = panel2.ClientSize.Width - Asa.Common.FIELD_BORDER * 2;
// pnlCode[i].Size = new Size(wPanel, (Asa.Common.FIELD_HEIGHT + 6) * info[i].Count + 9 + 26);
// if (i == 0)
// pnlCode[i].Location = new Point(Asa.Common.FIELD_BORDER, Asa.Common.FIELD_BORDER_TOP);
// else
// pnlCode[i].Location = new Point(Asa.Common.FIELD_BORDER, pnlCode[i - 1].Top + pnlCode[i - 1].Height + 6);
// panel2.Controls.Add(pnlCode[i]);
// lblID[i] = new Label();
// lblID[i].Text = string.Format("ID:{0:00}", i + 1);
// lblID[i].ForeColor = Color.Red;
// lblID[i].Font = new Font("微软雅黑", 12f);
// lblID[i].Size = new Size(60, 20);
// lblID[i].Location = new Point(6, 6);
// lblID[i].TextAlign = ContentAlignment.MiddleLeft;
// pnlCode[i].Controls.Add(lblID[i]);
// chkEnabled[i] = new CheckBox();
// chkEnabled[i].Text = "启用";
// chkEnabled[i].Checked = false;
// chkEnabled[i].Font = new Font("宋体", 12f);
// chkEnabled[i].Size = new Size(100, 20);
// chkEnabled[i].Location = new Point(72, 6);
// pnlCode[i].Controls.Add(chkEnabled[i]);
// for (int j = 0; j < info[i].Count; j++)
// {
// //Asa.TempItem item = template.area[i].Item[j];
// Label lbl = new Label();
// lbl.Text = info[i][j].text;
// lbl.Font = new Font("微软雅黑", 12f);
// lbl.Size = new Size(wLabel, Asa.Common.FIELD_HEIGHT);
// lbl.TextAlign = ContentAlignment.MiddleLeft;
// lbl.Location = new Point(6, 32 + (Asa.Common.FIELD_HEIGHT + 6) * j);
// lbl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
// ComboBox cbo = new ComboBox();
// cbo.Font = new Font("微软雅黑", 12f);
// cbo.Location = new Point(6 + lbl.Width + 6, lbl.Top);
// cbo.Size = new Size(pnlCode[i].Width - wLabel - 20, Asa.Common.FIELD_HEIGHT);
// cbo.Items.AddRange(Asa.Common.SetParam.Field.ToArray());
// cbo.DropDownStyle = ComboBoxStyle.DropDownList;
// cbo.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
// pnlCode[i].Controls.Add(lbl);
// pnlCode[i].Controls.Add(cbo);
// lblCode.Add(lbl);
// cboCode.Add(cbo);
// }
// }
//}
///// <summary>
///// 获取选中的区域
///// </summary>
///// <param name="areaRect"></param>
///// <returns></returns>
//private bool GetSelectArea(out Asa.AreaRect areaRect)
//{
// areaRect = new Asa.AreaRect();
// //获取指定点所在的区域
// Asa.OpenCV opencv = new Asa.OpenCV();
// List<Asa.AreaRect> area = opencv.extractRect(srcBmp);
// System.Drawing.Drawing2D.GraphicsPath path;
// List<int> find = new List<int>();
// for (int i = 0; i < area.Count; i++)
// {
// path = new System.Drawing.Drawing2D.GraphicsPath();
// path.AddPolygon(area[i].Points);
// Region myRegion = new Region();
// myRegion.MakeEmpty();
// myRegion.Union(path);
// //bool myPoint = myRegion.IsVisible(imgzoom.drawPoint);
// //if (myPoint) find.Add(i);
// }
// if (find.Count == 0)
// {
// return false;
// }
// else if (find.Count == 1)
// {
// areaRect = area[find[0]];
// return true;
// }
// else if (find.Count > 1)
// {
// areaRect = area[find[0]];
// return true;
// }
// else
// {
// return false;
// }
//}
}
}