FrmTest.cs
7.0 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IDHIKCamera
{
public partial class FrmTest : Form
{
public FrmTest()
{
InitializeComponent();
pictureBox1.Show();
}
/// <summary>
/// 初始化摄像机名称和二维码类型
/// </summary>
public static void LoadConfig()
{
try
{
IDHIK.Instance.Load();
List<string> names = IDHIK.Instance.cameraName;
if (names != null)
{
// hikNameList.AddRange(names);
foreach (string name in names)
{
LibLogUtil.Info("加载到IDHIK相机:" + name);
}
}
//IDHIK.Instance.OpenAll();
}
catch (Exception ex)
{
LibLogUtil.Error("解析IDHIK出错:" + ex.StackTrace);
}
}
private void btnIDHIKInit_Click(object sender, EventArgs e)
{
LoadConfig();
foreach (string name in IDHIK.Instance.cameraName)
{
cbDeviceList.Items.Add(name);
}
if (cbDeviceList.Items.Count > 0)
{
cbDeviceList.SelectedIndex = 0;
btnIDHIKInit.Visible = false;
}
}
private Task scanTask = null;
private int count = 0;
private void btnIDHIKTest_Click(object sender, EventArgs e)
{
StopTest = true;
string cName = cbDeviceList.Text;
if (cName == "")
{
return;
}
ScanTest(cName);
count = 1;
if (checkBox1.Checked)
{
StopTest = false;
scanTask = Task.Factory.StartNew(() =>
{
while (checkBox1.Checked)
{
if (StopTest)
{
break;
}
count++;
ScanTest(cName);
Thread.Sleep(100);
}
});
}
}
private void ScanTest(string cName)
{
try
{
List<CodeInfo> codes = CameraScan(new string[] { cName });
lblCodeInfo.Text = "";
string str = $"扫码{count}:\r\n";
foreach (CodeInfo code in codes)
{
str += $"[{code.CodeType}][{code.X},{code.Y}]{code.CodeStr}\r\n";
}
lblCodeInfo.Text = str;
}
catch (Exception ex)
{
LibLogUtil.Error("出错:" + ex.ToString());
}
}
[HandleProcessCorruptedStateExceptions]
public List<CodeInfo> CameraScan(string[] cameraNameList)
{
HashSet<string> codestr = new HashSet<string>();
List<CodeInfo> codeList = new List<CodeInfo>();
if (cameraNameList == null || cameraNameList.Length <= 0)
{
throw new Exception("CameraScan方法没有传入相机名称.");
}
try
{
foreach (string cameraName in cameraNameList)
{
string r = "";
LibLogUtil.Info($"【" + cameraName + "】开始取图片");
if (cameraName.Trim().Equals(""))
{
continue;
}
DateTime startTime = DateTime.Now;
Image bmp = null;
try
{
bmp = IDHIK.Instance.GrabOne(cameraName, out List<CodeInfo> cc);
//HalconDotNet.HOperatorSet.RotateImage()
if (bmp == null)
{
LibLogUtil.Error(" 【" + cameraName + "】取图片失败[" + IDHIK.Instance._errInfo + "],关闭相机");
IDHIK.Instance.Close(cameraName);
continue;
}
LibLogUtil.Info(" 【" + cameraName + "】取图片扫码完成,数量【" + cc.Count + "】");
cc.ForEach((c) =>
{
if (!codestr.Contains(c.CodeStr))
{
codeList.Add(c);
codestr.Add(c.CodeStr);
r += "##" + c.CodeStr;
}
});
LibLogUtil.Info(" 【" + cameraName + "】" + " 扫码完成【" + (DateTime.Now - startTime).TotalSeconds.ToString() + "】 " + count + ":" + r);
pictureBox1.Image = (Image)bmp;
pictureBox1.Refresh();
}
catch (AccessViolationException e)
{
LibLogUtil.Error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
// GC.Collect();
}
catch (Exception ex)
{
LibLogUtil.Error(" 扫码出错:" + ex.ToString());
}
finally
{
if (bmp != null)
bmp.Dispose();
}
}
}
catch (AccessViolationException e)
{
LibLogUtil.Error(" 扫码出现AccessViolationException异常:" + e.ToString());
}
catch (Exception ex)
{
LibLogUtil.Error(" 扫码出错:" + ex.ToString());
}
return codeList;
}
private bool StopTest = false;
private void btnExit_Click(object sender, EventArgs e)
{
StopTest = true;
checkBox1.Checked = false;
this.Close();
}
private void FrmTest_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
LoadConfig();
foreach (string name in IDHIK.Instance.cameraName)
{
cbDeviceList.Items.Add(name);
}
if (cbDeviceList.Items.Count > 0)
{
cbDeviceList.SelectedIndex = 0;
}
else
{
btnIDHIKInit.Visible = true;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
}
}
}