UsbCameraHDevelop.cs
8.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using HalconDotNet;
using TSA_V.Common;
namespace TSA_V.DeviceLibrary
{
public class CameraHDevelop
{
public static Dictionary<string, HTuple> cameraHtuple = new Dictionary<string, HTuple>();
public static bool OpenCamera(string cameraName)
{
try
{
if (IsOpen(cameraName))
{
return true;
}
HTuple hv_AcqHandle = null;
if (cameraName.Contains("USB")||cameraName.StartsWith("["))
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", cameraName, 0, -1, out hv_AcqHandle);
}
else
{
HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1,
"default", -1, "false", "default", cameraName, 0,
-1, out hv_AcqHandle);
}
//HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
// -1, "false", "default", cameraName, 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
cameraHtuple.Add(cameraName, hv_AcqHandle);
return true;
}
catch (Exception ex)
{
LogUtil.error("打开摄像机【" + cameraName + "】失败:" + ex.ToString() + ",直接关闭摄像机");
CloseCamera(cameraName);
return false;
}
}
public static void CloseAllCamera()
{
List<string> keys = new List<string>(cameraHtuple.Keys);
foreach (string key in keys)
{
CloseCamera(key);
}
}
public static HObject GrabImage(string cameraName)
{
HObject ho_Image = null;
try
{
if (cameraHtuple.ContainsKey(cameraName))
{
HTuple hv_AcqHandle = cameraHtuple[cameraName];
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
if (hv_AcqHandle != null)
{
HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
}
}
}
catch (Exception ex)
{
LogUtil.error("获取摄像机【"+ cameraName + "】的图片出错:" + ex.ToString());
}
return ho_Image;
}
public static bool IsOpen(string cameraStr)
{
return cameraHtuple.ContainsKey(cameraStr);
}
public static void CloseCamera(string cameraName)
{
try
{
if (cameraHtuple.ContainsKey(cameraName))
{
HTuple hv_AcqHandle = cameraHtuple[cameraName];
if (hv_AcqHandle != null)
{
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
hv_AcqHandle = null;
}
cameraHtuple.Remove(cameraName);
}
}
catch (Exception ex)
{
LogUtil.error("关闭摄像机【" + cameraName + "】失败:" + ex.ToString());
}
}
//public static List<string> GetCode(HObject ho_Image)
//{
// List<string> list = new List<string>();
// foreach (string codeType in GetCodeTypeList())
// {
// string[] array = GetQrCode(ho_Image, codeType);
// if (array != null)
// {
// list.AddRange(array);
// }
// }
// return list;
//}
//public static string GetCodeStr(HObject ho_Image)
//{
// List<string> codeList = GetCode(ho_Image);
// if (codeList.Count > 0)
// {
// return codeList[0];
// }
// return "";
//}
//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
// {
// 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;
// LogUtil.debug("GetQrCode:结束处理条码,已耗时:" + (DateTime.Now - date).ToString());
// return resultList;
// }
// catch (Exception ex)
// {
// return new string[] { };
// }
//}
//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 "";
// }
//}
}
}