FrmCodeEdit.cs
7.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
using CodeLibrary;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace CodeSplicing
{
public partial class FrmCodeEdit : Form
{
private List<CodeInfo> CodeResult = new List<CodeInfo>();
private String SplicingStr = "";
private HObject Ho_Image = null;
private PylonCamera camera = new PylonCamera();
private Stopwatch stopwatch = new Stopwatch();
public FrmCodeEdit()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void FrmMain_Load(object sender, EventArgs e)
{
string[] camerName = camera.GetName();
cmbCamera.Items.Clear();
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
if (camerName.Length > 0)
{
cmbCamera.SelectedIndex = 0;
}
HDLogUtil.logBox = this.richTextBox1;
}
private void btnSelImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "选择二维码图片";
openDialog.Filter = "图片(*.jpg)|*.jpg|图片(*.png)|*.png";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
txtPath.Text = openDialog.FileName;
string filename = txtPath.Text;
if (string.IsNullOrEmpty(filename))
{
MessageBox.Show("获取二维码图片为空");
}
pictureBox1.Image = null;
//读取图片内容
Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img;
Ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(new Bitmap(img));
ShowImage(Ho_Image);
}
private void ShowCode(List<CodeInfo> list)
{
cmbCodeType1.SelectedIndex = 0;
cmbCodeType2.SelectedIndex = 0;
cmbCodeType3.SelectedIndex = 0;
cmbCodeType4.SelectedIndex = 0;
cmbCodeType5.SelectedIndex = 0;
lblCode1.Text = "";
lblCode2.Text = "";
lblCode3.Text = "";
lblCode4.Text = "";
lblCode5.Text = "";
list = (from m in list orderby m.Y descending select m).ToList<CodeInfo>();
SplicingStr = "RealID:";
if (list.Count > 0)
{
HDLogUtil.info("耗时:" + stopwatch.Elapsed.ToString()+"识别到二维码:");
int i = 0;
foreach (CodeInfo code in list)
{
HDLogUtil.info(" (X:" + code.X + ",Y:" + code.Y + ",角度:"+code.Orientation+") " + code.CodeStr);
if (i.Equals(0))
{
lblCode1.Text = code.CodeStr;
}
else if (i.Equals(1))
{
lblCode2.Text = code.CodeStr;
}
else if (i.Equals(2))
{
lblCode3.Text = code.CodeStr;
}
else if (i.Equals(3))
{
lblCode4.Text = code.CodeStr;
}
else if (i.Equals(4))
{
lblCode5.Text = code.CodeStr;
}
else if (i.Equals(5))
{
}
i++;
}
}
else
{
HDLogUtil.info("未识别到二维码");
SplicingStr = "";
}
if (SplicingStr.EndsWith("*"))
{
SplicingStr = SplicingStr.Substring(0, SplicingStr.Length - 1);
}
}
private void button7_Click(object sender, EventArgs e)
{
HDLogUtil.ClearLog();
}
private void button8_Click(object sender, EventArgs e)
{
int index = cmbCamera.SelectedIndex;
if (index < 0)
{
MessageBox.Show("请先选择相机");
return;
}
camera.Open(index);
Bitmap bit = camera.syncShot();
if (bit != null)
{
pictureBox1.Image = bit;
}
camera.Close();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnHalconP_Click(object sender, EventArgs e)
{
if (Ho_Image == null)
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
HDLogUtil.ClearLog();
stopwatch.Restart();
HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
CodeResult = HDCodeHelper.DecodeBarCode(Ho_Image);
ShowCode(CodeResult);
}
public void ShowImage(HObject ho_Image)
{
HTuple width, height;
HOperatorSet.GetImageSize(ho_Image, out width, out height);
int dWidth = (int)width.D;
int dHeight = (int)height.D;
this.hWindowControl1.HalconWindow.SetPart(0, 0, dHeight, dWidth);
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
}
private Stopwatch createStopWarch = new Stopwatch();
private bool isInProcess = false;
private void btnSplicing_Click(object sender, EventArgs e)
{
string filePath = Application.StartupPath + @"\codeImage\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
if (lblCode1.Text.Equals(""))
{
MessageBox.Show("请先解析条码!", "提示");
return;
}
}
public string ReplaceName(string filename)
{
string str = filename;
str = str.Replace("\\", string.Empty);
str = str.Replace("/", string.Empty);
str = str.Replace(":", string.Empty);
str = str.Replace("*", string.Empty);
str = str.Replace("?", string.Empty);
str = str.Replace("\"", string.Empty);
str = str.Replace("<", string.Empty);
str = str.Replace(">", string.Empty);
str = str.Replace("|", string.Empty);
str = str.Replace(" ", string.Empty); //前面的替换会产生空格,最后将其一并替换掉
return str;
}
}
}