HDevelopExport.cs
16.1 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
using HalconDotNet;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class HDevelopExport
{
/// <summary>
/// 摄像机类别,只保存已经打开的摄像机
/// </summary>
public static Dictionary<string, HTuple> cameraHtuple = new Dictionary<string, HTuple>();
public static List<string> cameraNameList = new List<string>();
public static List<string> codeTypeList = new List<string>();
private static char spiltChar = '#';
/// <summary>
/// 初始化摄像机名称和二维码类型
/// </summary>
/// <param name="nameStr">摄像机名称,多个用#分割</param>
/// <param name="codeStr">二维码类型,多个用#分割</param>
public static void LoadConfig(string nameStr, string codeStr)
{
cameraNameList = new List<string>();
codeTypeList = new List<string>();
try
{
string[] nameArray = nameStr.Split(spiltChar);
foreach (string str in nameArray)
{
LogUtil.info("加载到摄像机名称:" + str.Trim());
cameraNameList.Add(str.Trim());
}
string[] codeArray = codeStr.Split(spiltChar);
foreach (string str in codeArray)
{
LogUtil.info("加载到二维码类型:" + str.Trim());
codeTypeList.Add(str.Trim());
}
}
catch (Exception ex)
{
LogUtil.error("解析摄像机配置出错:" + ex.ToString());
}
}
/// <summary>
/// 扫取二维码,会打开摄像机,正常情况下不关闭摄像机
/// </summary>
/// <returns></returns>
public static List<string> CameraScan(out string msg)
{
List<string> allCodeList = new List<string>();
msg = "";
try
{
//HDevelopExport.CloseAllCamera();
//打开所有摄像机
HDevelopExport.OpenAllCamera();
foreach (string cameraName in HDevelopExport.cameraNameList)
{
HObject ho_Image = HDevelopExport.GrabImage(cameraName);
List<string> codeList = HDevelopExport.GetCode(ho_Image);
if (codeList.Count > 0)
{
Bitmap bitmap = HObject2Bpp24(ho_Image);
//DateTime time = DateTime.Now;
//try
//{
// string name = time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString() + ".jpg";
// bitmap.Save(name);
//}
//catch(Exception ex)
//{
//}
if (Exist(bitmap))
{
msg = "盘尺寸错误";
LogUtil.error("盘尺寸错误!");
}
}
allCodeList.AddRange(codeList);
}
}
catch (Exception ex)
{
LogUtil.error("从摄像机获取二维码出错:" + ex.ToString());
LogUtil.error("关闭摄像机");
HDevelopExport.CloseAllCamera();
}
return allCodeList;
}
/// <summary>
/// 料盘是否存在,true在,false不在
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
private static bool Exist(Bitmap bmp)
{
//8个点,距离边界1/10
Point[] pt = new Point[8];
int w = bmp.Width / 10;
int h = bmp.Height / 10;
pt[0] = new Point(10, 20); //左上1
pt[1] = new Point(20, 10); //左上2
pt[2] = new Point(bmp.Width - 10, 20); //右上1
pt[3] = new Point(bmp.Width - 20, 10); //右上2
pt[4] = new Point(10, bmp.Height - 20); //左下1
pt[5] = new Point(20, bmp.Height - 10); //左下2
pt[6] = new Point(bmp.Width - 10, bmp.Height - 20); //右下1
pt[7] = new Point(bmp.Width - 20, bmp.Height - 10); //右下2
//数量
int count = 0;
//颜色的范围,小于即不存在
Color range = Color.FromArgb(50, 50, 50);
for (int i = 0; i < pt.Length; i++)
{
Color cc = bmp.GetPixel(pt[i].X, pt[i].Y);
if (cc.R < range.R && cc.G < range.G && cc.B < range.B)
count++;
}
if (count >= 7) //满7个即可
return false;
else
return true;
}
public static Bitmap HObject2Bpp24(HObject image)
{
Bitmap res = null;
try
{
HTuple hred, hgreen, hblue, type, width, height;
HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* bptr = (byte*)bitmapData.Scan0;
byte* r = ((byte*)hred.I);
byte* g = ((byte*)hgreen.I);
byte* b = ((byte*)hblue.I);
for (int i = 0; i < width * height; i++)
{
bptr[i * 4] = (b)[i];
bptr[i * 4 + 1] = (g)[i];
bptr[i * 4 + 2] = (r)[i];
bptr[i * 4 + 3] = 255;
}
}
res.UnlockBits(bitmapData);
}catch(Exception ex)
{
LogUtil.info("Hobject to BitMap 出错"+ex.ToString());
}
return res;
}
public static List<string> GetCode(HObject ho_Image, string symbolType)
{
List<string> list = new List<string>();
string[] array = GetQrCode(ho_Image, symbolType);
if (array != null)
{
list.AddRange(array);
}
return list;
}
public static List<string> GetCode(HObject ho_Image)
{
List<string> list = new List<string>();
foreach (string codeType in codeTypeList)
{
string[] array = GetQrCode(ho_Image, codeType);
if (array != null)
{
list.AddRange(array);
}
}
return list;
}
public static string GetCodeParamFilePath(string codePath)
{
string appPath = Application.StartupPath;
string path = appPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
string filePath = path + codePath + ".dcm";
if (File.Exists(filePath))
{
return filePath;
}
else
{
return "";
}
}
// Procedures
//private static HTuple hv_DataCodeHandle = null;
private static string[] GetQrCode(HObject ho_Image, string symbolType)
{
try
{
DateTime date = DateTime.Now;
// Local iconic variables
HObject ho_SymbolXLDs;
// Local control variables
HTuple hv_ResultHandles = null;
//HTuple hv_DataCodeHandle = null, hv_ResultHandles = null;
HTuple hv_DecodedDataStrings = null;
HTuple hv_Area = null;
HTuple hv_Row1 = null;
HTuple hv_Column = null;
HTuple hv_PointOrder = null;
HTuple hv_DataCodeHandle = null;
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
//LogUtil.info("GetQrCode:结束HOperatorSet.GenEmptyObj,已耗时:" + (DateTime.Now - date).ToString());
HOperatorSet.CreateDataCode2dModel(symbolType, "default_parameters", "maximum_recognition",
out hv_DataCodeHandle);
string hv_model_path = GetCodeParamFilePath(symbolType);
if (!hv_model_path.Equals(""))
{
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
}
else
{
if (symbolType.Equals("Data Matrix ECC 200"))
{
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 500);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "mirrored", "no");
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "contrast_min", 50);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "symbol_cols_min", 40);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "symbol_rows_min", 40);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "symbol_cols_max", 40);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "symbol_rows_max", 40);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "symbol_shape", "square");
}
else
{
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 300);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "mirrored", "no");
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "model_type", 2);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "module_gap_max", "no");
}
}
//LogUtil.info("GetQrCode:结束HOperatorSet.CreateDataCode2dModel,已耗时:" + (DateTime.Now - date).ToString());
ho_SymbolXLDs.Dispose();
LogUtil.debug("GetQrCode:结束ho_SymbolXLDs.Dispose,已耗时:" + (DateTime.Now - date).ToString());
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, "stop_after_result_num",
3, out hv_ResultHandles, out hv_DecodedDataStrings);
LogUtil.debug("GetQrCode:结束HOperatorSet.FindDataCode2d,已耗时:" + (DateTime.Now - date).ToString());
HOperatorSet.AreaCenterXld(ho_SymbolXLDs, out hv_Area, out hv_Row1, out hv_Column,
out hv_PointOrder);
//LogUtil.info("GetQrCode:结束HOperatorSet.AreaCenterXld,已耗时:" + (DateTime.Now - date).ToString());
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
//LogUtil.info("GetQrCode:结束HOperatorSet.ClearDataCode2dModel,已耗时:" + (DateTime.Now - date).ToString());
string[] resultList = hv_DecodedDataStrings.SArr;
if (resultList.Length > 0)
{
for (int i = 0; i < hv_DecodedDataStrings.SArr.Length; i++)
{
try
{
int x = (int)Math.Round(hv_Column.DArr[i]);
int y = (int)Math.Round(hv_Row1.DArr[i]);
string str = "=" + x + "x" + y + "=" + hv_DecodedDataStrings.SArr[i];
hv_DecodedDataStrings[i] = str;
}
catch (Exception ex)
{
LogUtil.error("处理二维码出错:索引=" + i + "," + resultList.ToString());
}
}
}
LogUtil.debug("GetQrCode:结束处理二维码,已耗时:" + (DateTime.Now - date).ToString());
return resultList;
}
catch (Exception ex)
{
return new string[] { };
}
}
public static void OpenAllCamera()
{
foreach (string camera in cameraNameList)
{
OpenCamera(camera);
}
}
public static void CloseAllCamera()
{
foreach (string camera in cameraNameList)
{
CloseCamera(camera);
}
}
/// <summary>
/// 截取二维码,不需要位置
/// </summary>
public static string SubStrCode(string str)
{
string[] array = str.Split('=');
if (array.Length >= 3)
{
return array[2];
}
return "";
}
public static void OpenCamera(string cameraAddr)
{
try
{
//Local iconic variables
HTuple hv_AcqHandle = null;
if (cameraHtuple.ContainsKey(cameraAddr))
{
return;
//CloseCamera(cameraAddr);
}
// HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
//-1, "false", "default", "[0] Integrated Camera", 0, -1, out hv_AcqHandle);
HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
"default", -1, "false", "default", cameraAddr, 0,
-1, out hv_AcqHandle);
//003053242c50_Basler_acA250014gm
//HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
cameraHtuple.Add(cameraAddr, hv_AcqHandle);
// IsOpen = true;
}
catch (Exception ex)
{
LogUtil.error("打开摄像机失败:" + ex.ToString() + ",调用关闭摄像头");
CloseCamera(cameraAddr);
}
}
public static void CloseCamera(string cameraStr)
{
try
{
if (cameraHtuple.ContainsKey(cameraStr))
{
HTuple hv_AcqHandle = cameraHtuple[cameraStr];
if (hv_AcqHandle != null)
{
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
hv_AcqHandle = null;
}
cameraHtuple.Remove(cameraStr);
//IsOpen = false;
}
}
catch (Exception ex)
{
LogUtil.error("关闭摄像机失败:" + ex.ToString());
}
}
public static HObject GrabImage(string cameraStr)
{
HObject ho_Image = null;
try
{
if (cameraHtuple.ContainsKey(cameraStr))
{
HTuple hv_AcqHandle = cameraHtuple[cameraStr];
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
// HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle,-1);
HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
}
}
catch (Exception ex)
{
LogUtil.error("获取摄像机【" + cameraStr + "】的图片出错:" + ex.ToString());
}
return ho_Image;
}
public static bool IsOpen(string cameraStr)
{
return cameraHtuple.ContainsKey(cameraStr);
}
}
}