FrmCodeDecode.cs
9.8 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
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;
namespace CodeLibrary
{
public partial class FrmCodeDecode : Form
{
private Stopwatch stopwatch = new Stopwatch();
private List<string> baslerNameList = new List<string>();
private List<string> hikNameList = new List<string>();
public FrmCodeDecode()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
cmbCount.SelectedIndex = 0;
LoadCamera();
cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
if (HDCodeLearnHelper.codeTypeList.Count > 0)
{
cmbCodeType.SelectedIndex = 0;
}
else
{
cmbCodeType.Items.Add("QR Code");
cmbCodeType.SelectedIndex = 0;
}
}
private void LoadCamera()
{
string[] camerName = BaslerCamera.Instance.CameraName;
baslerNameList.AddRange(camerName);
cmbCamera.Items.Clear();
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
camerName = HIKCamera.Instance.CameraName;
hikNameList.AddRange(camerName);
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
if (camerName.Length > 0)
{
cmbCamera.SelectedIndex = 0;
}
}
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|图片(*.bmp)|*.bmp";
//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;
}
private void btnErZhi_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show("请先选择图片","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
Bitmap newMap = ImageHelper.ConvertTo1Bpp1(map);
pictureBox1.Image = newMap;
}
private void btnGray_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
Bitmap newMap = ImageHelper.ToGray(map);
pictureBox1.Image = newMap;
}
private void ShowCode(List<CodeInfo> list)
{
if (list.Count > 0)
{
txtResult.Text += "\r\n识别到二维码:";
foreach (CodeInfo code in list)
{
txtResult.Text += "\r\n" + ""+code.CodeType+" (X:" + code.X + ",Y:" + code.Y + ") " + code.CodeStr;
}
}
else
{
txtResult.Text += "\r\n未识别到二维码";
}
}
private void btnbarCode_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null )
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int count = cmbCount.SelectedIndex + 1;
txtResult.Text = "";
stopwatch.Restart();
HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
Bitmap map = new Bitmap(pictureBox1.Image);
List<CodeInfo> result = HDCodeHelper.DecodeBarCode(map);
ShowCode(result);
txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString();
}
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 void btnLearn_Click(object sender, EventArgs e)
{
FrmCodeLearn frm = new FrmCodeLearn();
frm.ShowDialog();
}
private void btnDCode_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null)
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int count = cmbCount.SelectedIndex + 1;
txtResult.Text = "";
stopwatch.Restart();
Bitmap map = new Bitmap(pictureBox1.Image);
HObject ho_image = HDCodeHelper.Bitmap2HObjectBpp24(map);
txtResult.Text += "\r\n转换图片耗时:" + stopwatch.Elapsed.ToString();
hWindowControl1.HalconWindow.SetPart(0, 0, map.Height, map.Width);
HOperatorSet.DispObj(ho_image, hWindowControl1.HalconWindow);
ShowImage(ho_image);
HDCodeHelper.HalconWindow = this.hWindowControl1.HalconWindow;
string codeParamPath = HDCodeHelper.GetCodeParamFilePath(cmbCodeType.Text);
if (chbUseParam.Checked.Equals(false))
{
codeParamPath = "";
}
List<CodeInfo> codeList = new List<CodeInfo>();
if (cmbCodeType.Text.ToLower().Equals("barcode"))
{
codeList = HDCodeHelper.DecodeBarCode(ho_image);
}
else
{
codeList = HDCodeHelper.DecodeCode(ho_image, count, codeParamPath, cmbCodeType.Text);
}
ShowCode(codeList);
txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString();
}
private void btnClearLog_Click(object sender, EventArgs e)
{
HDLogUtil.ClearLog();
txtResult.Text = "";
}
private Bitmap GetCameraBitmap()
{
int index = cmbCamera.SelectedIndex;
string camerName = cmbCamera.Text;
if (index < 0)
{
MessageBox.Show("请先选择相机");
return null;
}
if (baslerNameList.Contains(camerName))
{
BaslerCamera.Instance.Open(camerName);
BaslerCamera.Instance.GrabOne();
Bitmap bitmap = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
return bitmap;
}
else
{
HIKCamera.Instance.Open(camerName);
HIKCamera.Instance.GrabOne();
Bitmap bitmap = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
return bitmap;
}
}
private void btnCameraImage_Click(object sender, EventArgs e)
{
Bitmap bitmap = GetCameraBitmap();
if (bitmap != null)
{
HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
pictureBox1.Image = bitmap;
HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = hoImage;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnLigth_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
Bitmap newMap = ImageHelper.KiLighten(map,10);
pictureBox1.Image = newMap;
}
private void btnAn_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap map = new Bitmap(pictureBox1.Image);
Bitmap newMap = ImageHelper.KiLighten(map, -10);
pictureBox1.Image = newMap;
}
private void cmbCodeType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbCodeType.SelectedIndex >= 0)
{
txtParamPath.Text = HDCodeHelper.GetCodeParamFilePath(cmbCodeType.Text);
}
}
}
}