ReelCheckCamera.cs
12.6 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
using AOI;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace DeviceLibrary
{
public class ReelCheckCamera
{
public static Dictionary<string, IntPtr> CamerHandleMap { get; set; } = null;
static object grabLoc = new object();
static string IPCameraServiceIP = ConfigHelper.Config.Get("IPCamera_ServiceIp", "localhost");
static int IPCameraServicePort = ConfigHelper.Config.Get("IPCamera_ServicePort", 8088);
public static bool GrabOneImg(string camerName, out Bitmap bitmap, out string imgPath)
{
bitmap = null;
imgPath = "";
// if (Monitor.TryEnter(grabLoc, 500))
// {
try
{
bitmap = null;
string url = $"http://{IPCameraServiceIP}:{IPCameraServicePort}/cam/grabOneImg?camName={camerName}";
string result = HttpHelper.Get(url);
if (!string.IsNullOrEmpty(result))
{
var res = JsonHelper.DeserializeJsonToObject<Result>(result);
if (res != null && res.code == 0)
{
using (FileStream fs = new FileStream(res.msg, FileMode.Open, FileAccess.Read))
{
bitmap = (Bitmap)Bitmap.FromStream(fs);
imgPath = res.msg;
return true;
}
}
else
{
LogUtil.LOGGER.Error($"{camerName} 图像采集失败");
}
}
else
{
LogUtil.LOGGER.Error($"{camerName} 图像采集失败");
}
}
finally
{ //Monitor.Exit(grabLoc);
}
//}
//else
//{
// LogUtil.LOGGER.Warn("GrabOneImg 未得到锁");
//}
return false;
//if (!VideoManager.IsOpen(camerName))
//{
// LogUtil.info($"CameraManager 相机{camerName}还未打开,打开相机 ");
// VideoManager.Open(CamerHandleMap);
//}
//if (!VideoManager.IsOpen(camerName))
//{
// LogUtil.info($"CameraManager 相机{camerName} 打开失败 ");
// return false;
//}
//return VideoManager.GrabOneImg(camerName, out bitmap);
return true;
}
public static bool CheckIPServiceAlive()
{
try
{
string url = $"http://{IPCameraServiceIP}:{IPCameraServicePort}/isAlive";
string result = HttpHelper.Get(url);
if (!string.IsNullOrEmpty(result))
{
var res = JsonHelper.DeserializeJsonToObject<Result>(result);
if (res != null && res.code == 0)
{
return true;
}
}
}
catch (Exception ex)
{
}
return false;
}
public static void CloseIPService()
{
try
{
string url = $"http://{IPCameraServiceIP}:{IPCameraServicePort}/closeApp";
HttpHelper.Get(url, Encoding.UTF8);
}
catch (Exception ex)
{
}
}
static Stream BitMapToStream(Bitmap bitmap)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, bitmap);
return ms;
}
}
catch (Exception e)
{
LogUtil.LOGGER.Error("BitMapToStream", e);
}
return null;
}
public static void Close()
{
//VideoManager.Close();
}
private static Dictionary<string, AoiProject> aoiProjectMap = new Dictionary<string, AoiProject>();
private static AoiProject GetAOIProject(string projectName, string machineSide)
{
try
{
string key = $"{machineSide.ToString()}-{projectName}";
if (!aoiProjectMap.ContainsKey(key))
{
string path = Application.StartupPath + $"\\Config\\{machineSide}\\{projectName}";
AoiProject aoiProject = AoiProject.Load(path, out string msg);
if (aoiProject == null || msg != "")
{
LogUtil.LOGGER.Error("加载 AoiProject " + path + " 失败:" + msg);
return null;
}
else
{
LogUtil.LOGGER.Info("加载 AoiProject " + path);
aoiProjectMap.Add(key, aoiProject);
}
}
return aoiProjectMap[key];
}
catch (Exception e)
{
LogUtil.LOGGER.Error("GetAOIProject 出错: " + e.ToString());
}
return null;
}
public static void ClearProject()
{
if (aoiProjectMap != null && aoiProjectMap.Count > 0)
{
aoiProjectMap.Clear();
LogUtil.info("重置 AoiProject");
}
}
static bool autoStart = ConfigHelper.Config.Get($"IPCamera_AutoStartIPCamera", true);
static object aoiLoc = new object();
/// <summary>
/// 检测是否有料
/// </summary>
/// <param name="cameraName"></param>
/// <param name="projectName"></param>
/// <returns>true:有料</returns>
public static bool AOICheck(string deviceName, string projectName, string machineSide, out string resList)
{
resList = "";
string cameraName= ConfigHelper.Config.Get($"IPCamera_CameraName", "cam1");
if (!ConfigHelper.Config.Get("IPCamera_EnableAOI", false))
{
//无料
return false;
}
if (Monitor.TryEnter(aoiLoc, 3000))
{
Bitmap bitmap = null;
try
{
if (autoStart)
{
if (!CheckIPServiceAlive())
{
CloseIPService();
Thread.Sleep(1000);
string baseDir = Application.StartupPath + @"\Modules\";
WindowManager.Start();
LogUtil.error("监控相机未启动,启动相机");
return true;
}
}
else if (!CheckIPServiceAlive())
{
//RobotManage.mainMachine.ShowMsg("检测到监控相机未启动", MsgLevel.warning);
}
bool result = GrabOneImg(cameraName, out _, out string imgPath);
if (result && !string.IsNullOrEmpty(imgPath))
{
AoiProject project = GetAOIProject(projectName, machineSide);
if (project != null)
{
using (FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
{
bitmap = (Bitmap)Bitmap.FromStream(fs);
if (bitmap != null)
{
List<ResultBean> resultBean = project.CheckAll(bitmap, out Image outImage);
bool checkResult = true;
string ngList = deviceName + " AOICheck: " + cameraName + $",{machineSide.ToString()}," + projectName + ",结果: ";
foreach (ResultBean bean in resultBean)
{
ngList += GetShowName(bean) + "\r\n";
if (bean.result.Equals(false))
{
checkResult = false;
}
}
resList = ngList;
LogUtil.LOGGER.Info(ngList);
if (!checkResult)//有料
{
SaveImageToFile("", cameraName, bitmap,"有料", true);
}
else
{
SaveImageToFile("", cameraName, bitmap, "无料", true);
}
//检测失败返回有料
return !checkResult;
}
else
{
LogUtil.LOGGER.Error(deviceName + " AOICheck: " + cameraName + "." + projectName + $", 加载[{imgPath}]图片失败");
return true;
}
}
}
else
{
LogUtil.LOGGER.Error(deviceName + " AOICheck: " + cameraName + "." + projectName + ", 获取AOIProject失败");
}
}
else
{
LogUtil.LOGGER.Error(deviceName + " AOICheck: " + cameraName + "." + projectName + ", 获取图片失败");
}
}
catch (Exception e)
{
LogUtil.LOGGER.Error(deviceName + " AOICheck", e);
}
finally
{
bitmap?.Dispose();
Monitor.Exit(aoiLoc);
}
}
return true;
}
static string SaveImageToFile(string deviceName, string cameraName, Bitmap bitmap,string result, bool deleteCcnt = false)
{
string date = DateTime.Now.ToString("HH-mm-ss-") + DateTime.Now.Millisecond+"-"+result;
string dire = @"\image\aoi\" + deviceName.Trim().Replace('_', '-') + @"\" + cameraName.Trim().Replace('_', '-').Replace(':', '-') + @"\";
string iamgeName = date + ".bmp";
try
{
if (Directory.Exists(dire).Equals(false))
{
Directory.CreateDirectory(dire);
}
int delCnt = ConfigHelper.Config.Get("IPCamera_aoi检测有料图片保存数量", 300);
if (Directory.GetFiles(dire).Length > delCnt)
{
if (deleteCcnt)
Directory.Delete(dire, true);
}
bitmap.Save(dire + iamgeName, ImageFormat.Jpeg);
//bitmap.Dispose();
LogUtil.info(deviceName + " 【" + cameraName + "】扫码完成,保存图片到【" + dire + iamgeName + "】成功");
}
catch (Exception ex)
{
LogUtil.error("保存" + deviceName + " 【" + cameraName + "】的图片到【" + dire + iamgeName + "】出错" + ex.ToString());
}
return dire + iamgeName;
}
private static string GetShowName(ResultBean bean)
{
return (bean.result ? "✔ " : "✘ ") + bean.MethodName + $"({bean.percentValue}%)";
}
}
class Result
{
/// <summary>
/// 状态码,0为正常
/// </summary>
public int code { get; set; } = 0;
/// <summary>
/// 返回数据
/// </summary>
public Dictionary<string, string> data
{
get { return _data; }
set { _data = value; }
}
private Dictionary<string, string> _data = new Dictionary<string, string>();
/// <summary>
/// 提示信息
/// </summary>
public string msg { get; set; } = "ok";
}
}