FrmMain.cs
32.6 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
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 CodeSplice
{
public partial class FrmMain : Asa.Theme.FlatForm
{
private System.IO.Ports.SerialPort port;
private Asa.Theme.ImageDisplay display;
private List<LabelRect> label; //识别后的标签
private Dictionary<string, string> labelReplace = new Dictionary<string, string>();
private int tempIndex = -1;
private Bitmap printImage;
private Bitmap grabImage;
private Interface.IUserControl userControl;
/// <summary>
/// 没有相机
/// </summary>
private bool noCamera;
/// <summary>
/// 没有IO模块
/// </summary>
private bool noIO;
/// <summary>
/// 没有串口
/// </summary>
private bool noPort;
/// <summary>
/// 是否接触,用于判断DI触发时上一次是false
/// </summary>
private bool isTouch;
/// <summary>
/// 是否运行
/// </summary>
private bool isRun;
/// <summary>
/// 可以运行
/// </summary>
private bool canRun;
public FrmMain()
{
InitializeComponent();
}
/// <summary>
/// 识别条码
/// </summary>
/// <returns></returns>
private bool DiscernCode()
{
try
{
if (grabImage == null) return false;
Common.BarCode.Image = grabImage;
display.PolygonClear();
Common.BarCode.GetRectCode(out label);
display.CenterRedAdd(Common.LabelCenter(label));
display.Refresh();
string[] s = Common.LabelCode(label);
if (userControl != null)
{
userControl.SetCode(s);
//userControl.SetModeText()
}
return true;
}
catch (Exception ex)
{
Common.Log.OutError(ex.ToString());
return false;
}
}
/// <summary>
/// 匹配模板
/// </summary>
private void MatchTemplate()
{
//需要检测的标签的数量,标签内条码的数量,标签的大小
tempIndex = -1;
List<LabelRect> temp;
labelReplace.Clear();
for (int i = 0; i < Common.Mate.Count; i++) //每个模板进行匹配
{
bool findLabelAll = false;
temp = Common.Mate.GetLabel(i);
for (int j = 0; j < temp.Count; j++) //模板的每个标签
{
if (!temp[j].Check) continue; //不需要检测
bool findLabelOne = false;
for (int k = 0; k < label.Count; k++) //在识别的标签中找出相同的标签
{
if (temp[j].CodeCount != label[k].CodeCount) //标签中条码数量相同
{
//findLabelAll = false;
continue;
}
bool findCodeAll = false;
for (int a = 0; a < temp[j].CodeCount; a++)
{
bool findCodeOne = false;
for (int b = 0; b < label[k].CodeCount; b++)
{
if (Math.Abs(temp[j].Code[a].Width - label[k].Code[b].Width) < 30 &&
Math.Abs(temp[j].Code[a].Height - label[k].Code[b].Height) < 30)
{
findCodeOne = true;
if (temp[j].Code[a].Mode.Length > 0)
{
int startIndex = temp[j].Code[a].Start;
int length = temp[j].Code[a].Length;
string s = label[k].Code[b].Text;
if (startIndex >= s.Length) startIndex = 0;
if (startIndex + length > s.Length) length = s.Length - startIndex;
s = s.Substring(startIndex, length);
labelReplace.Add(temp[j].Code[a].Mode, s);
}
break;
}
}
if (findCodeOne)
{
findCodeAll = true;
}
else
{
findCodeAll = false;
break;
}
}
if (findCodeAll)
{
findLabelOne = true;
}
else
{
findLabelOne = false;
continue;
}
}
if (findLabelOne)
{
findLabelAll = true;
}
else
{
findLabelAll = false;
break;
}
}
if (findLabelAll)
{
tempIndex = i;
break;
}
else
{
tempIndex = -1;
labelReplace.Clear();
}
}
if (tempIndex == -1)
{
string[] text = null;
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
if (userControl != null)
userControl.SetOcrText(keyValuePairs);
}
else
{
Common.Mate.GetLabelOcr(tempIndex, out List<string> mode, out List<OcrSpace> space);
Dictionary<string, string> modeText = new Dictionary<string, string>();
Tesseract.TesseractEngine eng = new Tesseract.TesseractEngine("./tessdata", "eng", Tesseract.EngineMode.TesseractAndCube);
for (int i = 0; i < mode.Count; i++)
{
Bitmap bmp = CalcImage(space[i].CodeCenter, space[i].CodeAngle, space[i].OcrOffset, space[i].OcrSize);
Tesseract.Page page = eng.Process(bmp);
string txt = page.GetText();
page.Dispose();
modeText.Add(mode[i], txt.Trim('\n'));
//bmp.Save(@"C:\Users\Asa\Desktop\新建文件夹 (2)\" + i + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (userControl != null)
userControl.SetOcrText(modeText);
}
}
private Bitmap CalcImage(PointF codeCenter, float codeAngle, Point ocrOffset, Size ocrSize)
{
//PointF pt = codeCenter;
double aa = Math.Atan2(codeCenter.Y, codeCenter.X);
aa = aa * 180 / Math.PI; //转角度
double cc = Math.Sqrt(codeCenter.X * codeCenter.X + codeCenter.Y * codeCenter.Y);
aa += codeAngle;
aa = aa * Math.PI / 180; //转弧度
double x1 = Math.Cos(aa) * cc;
double y1 = Math.Sin(aa) * cc;
//int w = ocrSize.Width;
//int h = ocrSize.Height;
Bitmap ocrRotate = new Bitmap(ocrSize.Width, ocrSize.Height);
Graphics g = Graphics.FromImage(ocrRotate);
float codeCenterX = ocrSize.Width / 2f;
float codeCenterY = ocrSize.Height / 2f;
float dx = Convert.ToSingle(x1 - codeCenterX) + ocrOffset.X + codeCenterX;
float dy = Convert.ToSingle(y1 - codeCenterY) + ocrOffset.Y + codeCenterY;
g.TranslateTransform(-dx, -dy);
g.RotateTransform(codeAngle);
RectangleF src = new RectangleF(0, 0, display.Image.Width, display.Image.Height);
g.DrawImage(display.Image, src, src, GraphicsUnit.Pixel);
g.ResetTransform();
//g.DrawLine(Pens.Red, codeCenterX - 10, codeCenterY - 10, codeCenterX + 10, codeCenterY + 10);
//g.DrawLine(Pens.Red, codeCenterX - 10, codeCenterY + 10, codeCenterX + 10, codeCenterY - 10);
g.Save();
g.Dispose();
//ocrRotate.Save(@"C:\Users\Asa\Desktop\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return ocrRotate;
}
/// <summary>
/// 初始化相机
/// </summary>
private void InitCamera()
{
try
{
string s2;
Common.Camera = new Asa.Camera.Camera();
if(Common.Config.Camera=="Basler")
Common.Camera.Type = Asa.Camera.CameraType.Basler;
else if (Common.Config.Camera == "HIK")
Common.Camera.Type = Asa.Camera.CameraType.HIK;
bool bln = Common.Camera.Load();
Common.Log.Out("CameraCount = " + Common.Camera.Count);
if (Common.Camera.Count == 0)
{
noCamera = true;
s2 = DialogLang["NoCamera"];
}
else
{
noCamera = false;
if (Common.Config.MultiCamera)
s2 = string.Join("\r\n", Common.Camera.Name);
else
s2 = Common.Camera.Name[0];
}
Common.Log.Out(s2);
RtfLog.AppendText(s2 + "\r\n");
}
catch (Exception ex)
{
noCamera = true;
Common.Log.OutError(ex.ToString());
RtfLog.AppendText(ex.ToString() + "\r\n");
}
}
/// <summary>
/// 初始化灯光
/// </summary>
private void InitLightPort()
{
try
{
string s;
string[] name = System.IO.Ports.SerialPort.GetPortNames();
int idx = Array.FindIndex(name, ss => ss == Common.Config.LightPort);
if (idx == -1)
{
noPort = true;
s = DialogLang["NoPort"] + Common.Config.LightPort;
RtfLog.AppendText(s + "\r\n");
}
else
{
noPort = false;
port = new System.IO.Ports.SerialPort()
{
PortName = Common.Config.LightPort,
BaudRate = 19200,
DataBits = 8,
StopBits = System.IO.Ports.StopBits.One
};
port.Open();
System.Threading.Thread.Sleep(50);
port.Write(Common.Config.LightOpen);
port.Close();
s = "SerialPort Open Write Close"; //输出到日志文件,不用中英文
}
Common.Log.Out(s);
}
catch (Exception ex)
{
noPort = true;
Common.Log.OutError(ex.ToString());
RtfLog.AppendText(ex.ToString() + "\r\n");
}
}
/// <summary>
/// 初始化IO模块
/// </summary>
private void InitReg()
{
try
{
string s1, s2;
Common.IO = new Asa.IOModule.KND();
Common.IO.IP = Common.Config.IOAddress;
Common.IO.Connect();
if (Common.IO.IsConn)
{
noIO = false;
Common.IO.DI_Changed_Event += IO_DI_Changed_Event;
}
else
{
noIO = true;
s1 = Common.IO.ErrInfo;
s2 = DialogLang["NoIO"] + Common.Config.IOAddress;
Common.Log.Out(s1);
RtfLog.AppendText(s2 + "\r\n");
}
}
catch (Exception ex)
{
noIO = true;
Common.Log.OutError(ex.ToString());
RtfLog.AppendText(ex.ToString() + "\r\n");
}
}
/// <summary>
/// 初始化菜单控件位置
/// </summary>
/// <param name="btn"></param>
private void InitMenuControl(params Asa.Theme.FlatButton[] btn)
{
int space = 6;
int h = flatLang.Top - PicImage.Top;
h = (h - space * (btn.Length - 1)) / btn.Length;
int t = PicImage.Top;
for (int i = 0; i < btn.Length; i++)
{
btn[i].Top = t;
btn[i].Height = h;
t += h + space;
}
}
/// <summary>
/// 初始化操作面板
/// </summary>
private void InitOperPanel()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(Application.StartupPath + "\\Plugin\\OperPanel.dll");
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
if (t.BaseType == typeof(UserControl))
{
object mObject = Activator.CreateInstance(t);
Control value = (Control)mObject;
value.Location = new Point(6, flatPanel1.TitleHeight + 6);
value.Size = new Size(flatPanel1.Width - 12, flatPanel1.Height - flatPanel1.TitleHeight - 12);
value.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
flatPanel1.Controls.Add(value);
userControl = (Interface.IUserControl)value;
//userControl.LogOut += UserControl_LogOut;
userControl.Printing += UserControl_Printing;
break;
}
}
}
private string UserControl_Printing(string key)
{
Common.Label.FindIndex(Common.Config.LabelName);
Common.Label.PrintLast(labelReplace, out printImage, out string codeStr);
SaveHistory();
return codeStr;
}
/*
private void UserControl_LogOut(Interface.Type type, string lang, string msg, string log)
{
string val;
if (lang.Length == 0)
val = msg;
if (DialogLang.ContainsKey(lang))
val = DialogLang[lang] + msg;
else
val = msg;
switch (type)
{
case Interface.Type.True: break;
case Interface.Type.False:
Common.Log.Out(log);
RtfLog.AppendText(val + "\r\n");
break;
case Interface.Type.Error:
Common.Log.OutError(val);
RtfLog.AppendText(val + "\r\n");
break;
case Interface.Type.Message:
MessageBox.Show(val, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
Common.Log.Out(log);
break;
}
if (type == Interface.Type.False)
{
}
else if (type == Interface.Type.Error)
{
}
}
*/
/// <summary>
/// 保存历史记录
/// </summary>
private void SaveHistory()
{
if (grabImage != null) return;
int w = 1920;
int h = w * grabImage.Height / grabImage.Width;
Bitmap bmp = new Bitmap(w, h);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(grabImage, new Rectangle(0, 0, w, h), new Rectangle(0, 0, grabImage.Width, grabImage.Height), GraphicsUnit.Pixel);
g.Save();
string name = string.Format("{0:yyyy_MM_dd HH_mm_ss}", DateTime.Now);
TraceTemp temp = new TraceTemp();
temp.MateImage = bmp;
temp.MateImageName = name + " 1.jpg";
temp.MateTemp = Common.Mate.Name[tempIndex];
temp.LabelImage = printImage;
temp.LabelImageName = name + " 2.jpg";
temp.LabelTemp = Common.Config.LabelName;
for (int i = 0; i < label.Count; i++)
temp.Code.AddRange(label[i].Code);
Common.Trace.Add(temp);
temp.Name = Common.Trace.Count.ToString();
temp.MateImage.Save(Common.HISTORY_DIR + temp.MateImageName, System.Drawing.Imaging.ImageFormat.Jpeg);
temp.LabelImage.Save(Common.HISTORY_DIR + temp.LabelImageName, System.Drawing.Imaging.ImageFormat.Jpeg);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", Encoding.UTF8.BodyName, null));
System.Xml.XmlElement root = doc.CreateElement("Code");
doc.AppendChild(root);
root.SetAttribute("MaterialTemp", temp.MateTemp);
root.SetAttribute("MaterialImage", temp.MateImageName);
root.SetAttribute("LabelTemp", temp.LabelTemp);
root.SetAttribute("LabelImage", temp.LabelImageName);
for (int i = 0; i < temp.Code.Count; i++)
{
System.Xml.XmlElement ele = doc.CreateElement("Item");
root.AppendChild(ele);
ele.SetAttribute("Center", string.Format("{0},{1}", temp.Code[i].Center.X, temp.Code[i].Center.Y));
ele.SetAttribute("Text", temp.Code[i].Text);
}
doc.Save(Common.HISTORY_DIR + name + ".xml");
}
private void IO_DI_Changed_Event(Asa.IOModule.Status[] sta)
{
if (!isRun) return;
if (sta[0] == Asa.IOModule.Status.On)
{
if (isTouch) return;
Invoke(new Action(() =>
{
if (Common.Config.MultiCamera)
{
if (Common.Camera.Count == 1)
{
Common.Camera.GrabOne(0);
grabImage = Common.Camera.Image;
display.Image = grabImage;
}
else
{
Bitmap[] image = new Bitmap[Common.Camera.Count];
for (int i = 0; i < Common.Camera.Count; i++)
{
Common.Camera.GrabOne(i);
image[i] = Common.Camera.Image;
}
grabImage = Common.BarCode.MergeImage(image[0], image[1]);
display.Image = grabImage;
}
}
else
{
Common.Camera.GrabOne(0);
grabImage = Common.Camera.Image;
display.Image = grabImage;
}
DiscernCode();
MatchTemplate();
Common.Label.FindIndex(Common.Config.LabelName);
Common.Label.PrintLast(labelReplace, out printImage, out string codeStr);
SaveHistory();
//if (LstVender.SelectedIndex == -1)
// text.Add("MATERIALNO", "MATERIALNO");
//else
// text.Add("MATERIALNO", LstVender.Text);
//Bitmap bmp = SpliceQRCode();
//Common.Label.PrintQRcode = bmp;
//Common.Label.PrintLast(text);
}));
isTouch = true;
}
else
{
isTouch = false;
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
//InitOperPanel();
//Asa.Dog dog = new Asa.Dog();
//bool rtn = dog.Login(null, userControl.ID);
//if (!rtn)
//{
// MessageBox.Show(dog.ErrInfo);
// System.Diagnostics.Process.GetCurrentProcess().Kill();
//}
Asa.Theme.Control.SetColor(RtfLog);
Common.Log = new Asa.File.Log(Common.LOG_DIR);
Common.Config = new BLL.Config(Common.CONFIG_PATH);
Common.Label = new BLL.LabelEdit(Common.LABEL_DIR);
Common.Mate = new BLL.Material(Common.MATERIAL_DIR);
Common.Trace = new BLL.Traced(Common.HISTORY_DIR);
Common.BarCode = new BLL.BarCode();
display = new Asa.Theme.ImageDisplay(PicImage);
flatLang.LangDir = Common.LANGUAGE_DIR;
flatLang.Link(this);
flatLang.LoadLanguage(Common.Config.Language);
InitMenuControl(BtnWork, BtnCamera, BtnScan, BtnPrint, BtnLabel, BtnMate, BtnTrace, BtnLight, BtnMultiple, BtnSaveImage);
InitCamera();
InitLightPort();
if (Common.Config.Work)
InitReg();
else
BtnWork.Enabled = false;
if (userControl != null)
userControl.Connect();
if (noCamera || noIO || noPort)
{
canRun = false;
BtnWork.StateColor = Asa.Theme.Common.ERROR_COLOR;
}
else
{
canRun = true;
BtnWork.StateColor = Asa.Theme.Common.RUN_COLOR;
}
if (noCamera)
BtnCamera.StateColor = Asa.Theme.Common.ERROR_COLOR;
else
BtnCamera.StateColor = Asa.Theme.Common.RUN_COLOR;
}
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
if (port != null)
{
port.Open();
System.Threading.Thread.Sleep(50);
port.Write(Common.Config.LightClose);
port.Close();
}
}
private void FlatLang_LanguageChanged(object sender, string s)
{
Common.Config.Language = s;
}
private void BtnWork_Click(object sender, EventArgs e)
{
if (canRun)
{
isRun = false;
Asa.Theme.MessageBox.Show("", DialogLang["NoReady"], MessageBoxButtons.OK);
return;
}
if (isRun)
{
isRun = false;
if (Common.Config.MultiCamera)
{
if (Common.Camera.Count == 1)
Common.Camera.Close();
else
Common.Camera.CloseAll();
}
else
{
Common.Camera.Close();
}
BtnWork.Image = Properties.Resources.Start;
BtnCamera.Enabled = true;
BtnScan.Enabled = true;
BtnPrint.Enabled = true;
BtnLabel.Enabled = true;
BtnMate.Enabled = true;
BtnTrace.Enabled = true;
BtnLight.Enabled = true;
}
else
{
isRun = true;
if (Common.Config.MultiCamera)
{
if (Common.Camera.Count == 1)
Common.Camera.Open(0);
else
Common.Camera.OpenAll();
}
else
{
Common.Camera.Open(0);
}
BtnWork.Image = Properties.Resources.Stop;
BtnCamera.Enabled = false;
BtnScan.Enabled = false;
BtnPrint.Enabled = false;
BtnLabel.Enabled = false;
BtnMate.Enabled = false;
BtnTrace.Enabled = false;
BtnLight.Enabled = false;
}
}
private void BtnCamera_Click(object sender, EventArgs e)
{
//Bitmap bmp1 = new Bitmap(@"C:\Users\Asa\Desktop\1.bmp");
//Bitmap bmp2 = new Bitmap(@"C:\Users\Asa\Desktop\2.bmp");
//grabImage = Common.BarCode.MergeImage(bmp1, bmp2);
//grabImage.Save(@"C:\Users\Asa\Desktop\3.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
try
{
if (noCamera)
{
Asa.Theme.MessageBox.Show("", DialogLang["NoCamera"], MessageBoxButtons.OK);
}
else
{
bool rtn;
if (Common.Config.MultiCamera)
{
if (Common.Camera.Count == 1)
{
rtn = Common.Camera.Open(0);
Common.Camera.GrabOne(0);
grabImage = Common.Camera.Image;
display.Image = grabImage;
Common.Camera.Close();
}
else
{
rtn = Common.Camera.OpenAll();
Bitmap[] image = new Bitmap[Common.Camera.Count];
for (int i = 0; i < Common.Camera.Count; i++)
{
Common.Camera.GrabOne(i);
image[i] = Common.Camera.Image;
//image[i].Save("D:\\" + i + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Common.Camera.CloseAll();
grabImage = Common.BarCode.MergeImage(image[0], image[1]);
display.Image = grabImage;
}
}
else
{
rtn = Common.Camera.Open(0);
Common.Camera.GrabOne(0);
grabImage = Common.Camera.Image;
display.Image = grabImage;
Common.Camera.Close();
}
}
}
catch (Exception ex)
{
Common.Log.Out("Error: " + ex.ToString());
MessageBox.Show(ex.ToString());
}
}
private void BtnCamera_ClickRight(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog { Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif|All Files|*.*" };
if (dlg.ShowDialog() != DialogResult.OK) return;
grabImage = new Bitmap(dlg.FileName);
display.Image = grabImage;
}
private void BtnScan_Click(object sender, EventArgs e)
{
DiscernCode();
MatchTemplate();
}
private void BtnScan_ClickRight(object sender, EventArgs e)
{
DiscernCode();
}
private void BtnPrint_Click(object sender, EventArgs e)
{
Common.Label.FindIndex(Common.Config.LabelName);
Common.Label.PrintLast(labelReplace, out printImage, out string codeStr);
SaveHistory();
}
private void BtnPrint_ClickRight(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
dlg.PrinterSettings.PrinterName = Common.Config.Printer;
if (dlg.ShowDialog() == DialogResult.OK)
{
Common.Config.Printer = dlg.PrinterSettings.PrinterName;
Common.Label.FindIndex(Common.Config.LabelName);
Common.Label.PrintLast(new Dictionary<string, string>(), out printImage, out string codeStr);
SaveHistory();
}
}
private void BtnLabel_Click(object sender, EventArgs e)
{
FrmLabel frm = new FrmLabel();
frm.Location = PicImage.Location;
frm.Size = new Size(Width - frm.Left, Height - frm.Top);
flatLang.SetLanguage(frm);
frm.ShowDialog(this);
}
private void BtnMate_Click(object sender, EventArgs e)
{
FrmMate frm = new FrmMate();
frm.Location = PicImage.Location;
frm.Size = new Size(Width - frm.Left, Height - frm.Top);
flatLang.SetLanguage(frm);
FrmSubstring ff = new FrmSubstring();
flatLang.SetLanguage(ff);
frm.SetForm(ff);
frm.ShowDialog(this);
ff.Dispose();
}
private void BtnTrace_Click(object sender, EventArgs e)
{
FrmTrace frm = new FrmTrace();
frm.Location = PicImage.Location;
frm.Size = new Size(Width - frm.Left, Height - frm.Top);
flatLang.SetLanguage(frm);
frm.ShowDialog(this);
}
private void BtnLight_Click(object sender, EventArgs e)
{
FrmLight frm = new FrmLight();
frm.Location = PicImage.Location;
flatLang.SetLanguage(frm);
frm.Show();
}
private void BtnSaveImage_Click(object sender, EventArgs e)
{
if (grabImage == null) return;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "JPG文件|*.jpg|BMP文件|*.bmp|PNG文件|*.png|TIFF文件|*.tiff";
if (dlg.ShowDialog() == DialogResult.OK)
{
System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
switch (dlg.FilterIndex)
{
case 0: format = System.Drawing.Imaging.ImageFormat.Jpeg; break;
case 1: format = System.Drawing.Imaging.ImageFormat.Bmp; break;
case 2: format = System.Drawing.Imaging.ImageFormat.Png; break;
case 3: format = System.Drawing.Imaging.ImageFormat.Tiff; break;
}
grabImage.Save(dlg.FileName, format);
MessageBox.Show("Save completed.");
}
}
private void BtnMultiple_Click(object sender, EventArgs e)
{
FrmMultiple frm = new FrmMultiple();
frm.Location = PicImage.Location;
flatLang.SetLanguage(frm);
frm.ShowDialog();
//Bitmap bmp1 = new Bitmap(@"C:\Users\Asa\Desktop\1.bmp");
//Bitmap bmp2 = new Bitmap(@"C:\Users\Asa\Desktop\2.bmp");
//Bitmap bmp3 = Common.BarCode.MergeImage(bmp1, bmp2);
//bmp3.Save(@"C:\Users\Asa\Desktop\3.bmp");
}
//private void CalcImage()
//{
// PointF pt = newMate.LabelRect[labelIndex].Code[codeIndex].Center;
// double aa = Math.Atan2(pt.Y, pt.X);
// aa = aa * 180 / Math.PI; //转角度
// double cc = Math.Sqrt(pt.X * pt.X + pt.Y * pt.Y);
// aa += newMate.LabelRect[labelIndex].Code[codeIndex].Angle;
// aa = aa * Math.PI / 180; //转弧度
// double x1 = Math.Cos(aa) * cc;
// double y1 = Math.Sin(aa) * cc;
// int w = PicImage2.ClientSize.Width;
// int h = PicImage2.ClientSize.Height;
// ocrRotate = new Bitmap(w, h);
// Graphics g = Graphics.FromImage(ocrRotate);
// codeCenter.X = w / 2f + ocrOffset.X;
// codeCenter.Y = h / 2f + ocrOffset.Y;
// float dx = Convert.ToSingle(x1 - codeCenter.X);
// float dy = Convert.ToSingle(y1 - codeCenter.Y);
// g.TranslateTransform(-dx, -dy);
// g.RotateTransform(newMate.LabelRect[labelIndex].Code[codeIndex].Angle);
// RectangleF src = new RectangleF(0, 0, newMate.Image.Width, newMate.Image.Height);
// g.DrawImage(newMate.Image, src, src, GraphicsUnit.Pixel);
// g.ResetTransform();
// g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y - 10, codeCenter.X + 10, codeCenter.Y + 10);
// g.DrawLine(PEN_SELECT, codeCenter.X - 10, codeCenter.Y + 10, codeCenter.X + 10, codeCenter.Y - 10);
// g.Save();
// g.Dispose();
//}
}
}