CameraClass.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using CCDCount.DLL.AlarmTools;
  2. using CCDCount.MODEL.CameraClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using LogClass;
  5. using MvCameraControl;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Threading;
  14. namespace CCDCount.DLL
  15. {
  16. public class CameraClass
  17. {
  18. #region 常量
  19. readonly DeviceTLayerType enumTLayerType = DeviceTLayerType.MvGigEDevice | DeviceTLayerType.MvUsbDevice
  20. | DeviceTLayerType.MvGenTLGigEDevice | DeviceTLayerType.MvGenTLCXPDevice
  21. | DeviceTLayerType.MvGenTLCameraLinkDevice | DeviceTLayerType.MvGenTLXoFDevice;
  22. #endregion
  23. #region 变量
  24. private List<IDeviceInfo> CamList; // 相机列表
  25. private IDevice device = null; // 相机实例
  26. private long lastframeNum = -1;
  27. public int CamereNo { get { return _cameraNo; } }
  28. private int _cameraNo = -1;
  29. #endregion
  30. #region 公共方法
  31. /// <summary>
  32. /// 获取相机列表
  33. /// </summary>
  34. /// <param name="CamList"></param>
  35. /// <returns></returns>
  36. public void GetCameraList(out List<CameraInfoClass> CameraInfoList)
  37. {
  38. CamList = new List<IDeviceInfo>();
  39. CameraInfoList = new List<CameraInfoClass>();
  40. int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out CamList);
  41. if (nRet != MvError.MV_OK)
  42. {
  43. return;
  44. }
  45. else
  46. {
  47. //输出相机名称及SN码以方便选择设备
  48. foreach (var item in CamList)
  49. {
  50. CameraInfoList.Add(new CameraInfoClass
  51. {
  52. //DeviceName = item.UserDefinedName == "" ? item.ModelName : item.UserDefinedName,
  53. DeviceName = item.ModelName,
  54. DeviceSN = item.SerialNumber,
  55. });
  56. }
  57. }
  58. return;
  59. }
  60. /// <summary>
  61. /// 更新相机列表
  62. /// </summary>
  63. public void UpdateCameraList()
  64. {
  65. CamList = new List<IDeviceInfo>();
  66. int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out CamList);
  67. if (nRet != MvError.MV_OK)
  68. {
  69. Console.WriteLine("相机列表更新失败");
  70. return;
  71. }
  72. }
  73. /// <summary>
  74. /// 加载指定相机
  75. /// </summary>
  76. /// <param name="DeviceSN"></param>
  77. public bool LoadCamereDevice(CameraConfig DeviceConfig)
  78. {
  79. bool Blresult = false;
  80. if (device != null && device.IsConnected)
  81. {
  82. device.Close();
  83. device.Dispose();
  84. }
  85. UpdateCameraList();
  86. List<IDeviceInfo> deviceInfos = CamList.Where(o => o.SerialNumber == DeviceConfig.CameraSNNum).ToList();
  87. if(deviceInfos.Count == 0) return Blresult;
  88. IDeviceInfo deviceInfo = deviceInfos.First();
  89. try
  90. {
  91. // 打开设备
  92. device = DeviceFactory.CreateDevice(deviceInfo);
  93. }
  94. catch (Exception ex)
  95. {
  96. //FaultLog.RecordErrorMessage("Create Device fail!" + DeviceConfig.CameraSNNum + ":" + ex.Message);
  97. SystemAlarm.AlarmAlert(AlarmMessageList.相机实例创建失败,
  98. "Create Device fail!" + DeviceConfig.CameraSNNum + ":" + ex.Message,
  99. "相机实例创建失败" + DeviceConfig.CameraSNNum + ":" + ex.Message,
  100. "DLL:CameraClass-LoadCamereDevice");
  101. return Blresult;
  102. }
  103. SystemAlarm.AlarmCancel(AlarmMessageList.相机实例创建失败);
  104. int result = device.Open();
  105. if (result != MvError.MV_OK)
  106. {
  107. //FaultLog.RecordErrorMessage("Open Device fail!" + DeviceConfig.CameraSNNum + ":" + result);
  108. SystemAlarm.AlarmAlert(AlarmMessageList.开启相机失败,
  109. "Open Device fail!" + DeviceConfig.CameraSNNum + ":" + result,
  110. "开启相机失败" + DeviceConfig.CameraSNNum + ":" + result,
  111. "DLL:CameraClass-LoadCamereDevice");
  112. return Blresult;
  113. }
  114. SystemAlarm.AlarmCancel(AlarmMessageList.开启相机失败);
  115. // 判断是否为gige设备
  116. if (device is IGigEDevice)
  117. {
  118. // 转换为gigE设备
  119. IGigEDevice gigEDevice = device as IGigEDevice;
  120. // 探测网络最佳包大小(只对GigE相机有效)
  121. result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
  122. if (result != MvError.MV_OK)
  123. {
  124. //Log("Warning: Get Packet Size failed!", result);
  125. }
  126. else
  127. {
  128. result = device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
  129. if (result != MvError.MV_OK)
  130. {
  131. //Log("Warning: Set Packet Size failed!", result);
  132. }
  133. }
  134. }
  135. //判断是否从配置文件读取的参数,是则设置参数,否则加载相机默认参数
  136. if(DeviceConfig.IsLoadCanfig)
  137. {
  138. //设置从配置文件读取的参数
  139. device.Parameters.GetStringValue("DeviceModelName", out IStringValue deviceModelName);
  140. _cameraNo = DeviceConfig.CamerNo;
  141. if (DeviceConfig.CameraName != deviceModelName.CurValue)
  142. device.Parameters.SetStringValue("DeviceUserID", DeviceConfig.CameraName);
  143. device.Parameters.SetFloatValue("ExposureTime", DeviceConfig.ExposureTimeValue);
  144. device.Parameters.SetIntValue("AcquisitionLineRate", DeviceConfig.AcquistionLineRateValue);
  145. device.Parameters.GetIntValue("Height", out IIntValue height);
  146. device.Parameters.SetIntValue("Height", height.Min);
  147. device.Parameters.SetIntValue("Width", DeviceConfig.Width);
  148. device.Parameters.SetIntValue("OffsetX", DeviceConfig.OffsetX);
  149. }
  150. // 设置采集连续模式
  151. device.Parameters.SetEnumValueByString("AcquisitionMode", "Continuous");
  152. device.Parameters.SetEnumValueByString("TriggerMode", "Off");
  153. Blresult = true;
  154. return Blresult;
  155. }
  156. /// <summary>
  157. /// 重新加载指定相机
  158. /// </summary>
  159. /// <param name="CameraSN"></param>
  160. /// <returns></returns>
  161. public bool ReLoadCameraDevice(string CameraSN)
  162. {
  163. bool Blresult = false;
  164. if(device!=null && device.IsConnected)
  165. {
  166. device.Close();
  167. device.Dispose();
  168. }
  169. UpdateCameraList();
  170. List<IDeviceInfo> deviceInfos = CamList.Where(o => o.SerialNumber == CameraSN).ToList();
  171. if (deviceInfos.Count == 0) return Blresult;
  172. IDeviceInfo deviceInfo = deviceInfos.First();
  173. try
  174. {
  175. // 打开设备
  176. device = DeviceFactory.CreateDevice(deviceInfo);
  177. Blresult = true;
  178. }
  179. catch (Exception ex)
  180. {
  181. //FaultLog.RecordErrorMessage("Create Device fail!" + CameraSN + ":" + ex.Message);
  182. SystemAlarm.AlarmAlert(AlarmMessageList.相机实例创建失败,
  183. "Create Device fail!" + CameraSN + ":" + ex.Message,
  184. "相机实例创建失败" + CameraSN + ":" + ex.Message,
  185. "DLL:CameraClass-LoadCamereDevice");
  186. return Blresult;
  187. }
  188. SystemAlarm.AlarmCancel(AlarmMessageList.相机实例创建失败);
  189. int result = device.Open();
  190. if (result != MvError.MV_OK)
  191. {
  192. Blresult = false;
  193. //FaultLog.RecordErrorMessage("Open Device fail!" + CameraSN + ":" + result);
  194. SystemAlarm.AlarmAlert(AlarmMessageList.开启相机失败,
  195. "Open Device fail!" + CameraSN + ":" + result,
  196. "相机实例创建失败" + CameraSN + ":" + result,
  197. "DLL:CameraClass-LoadCamereDevice");
  198. return Blresult;
  199. }
  200. SystemAlarm.AlarmCancel(AlarmMessageList.开启相机失败);
  201. // 判断是否为gige设备
  202. if (device is IGigEDevice)
  203. {
  204. // 转换为gigE设备
  205. IGigEDevice gigEDevice = device as IGigEDevice;
  206. // 探测网络最佳包大小(只对GigE相机有效)
  207. result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
  208. if (result != MvError.MV_OK)
  209. {
  210. //Log("Warning: Get Packet Size failed!", result);
  211. }
  212. else
  213. {
  214. result = device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
  215. if (result != MvError.MV_OK)
  216. {
  217. //Log("Warning: Set Packet Size failed!", result);
  218. }
  219. }
  220. }
  221. return Blresult;
  222. }
  223. /// <summary>
  224. /// 重新加载相机参数
  225. /// </summary>
  226. /// <param name="RecameraConfig"></param>
  227. /// <returns></returns>
  228. public bool ReLoadCameraConfig(CameraConfig RecameraConfig)
  229. {
  230. bool Blresult = false;
  231. if (device != null && device.IsConnected)
  232. {
  233. //设置从配置文件读取的参数
  234. device.Parameters.GetStringValue("DeviceModelName", out IStringValue deviceModelName);
  235. _cameraNo = RecameraConfig.CamerNo;
  236. if (RecameraConfig.CameraName != deviceModelName.CurValue)
  237. device.Parameters.SetStringValue("DeviceUserID", RecameraConfig.CameraName);
  238. device.Parameters.SetFloatValue("ExposureTime", RecameraConfig.ExposureTimeValue);
  239. device.Parameters.SetIntValue("AcquisitionLineRate", RecameraConfig.AcquistionLineRateValue);
  240. device.Parameters.GetIntValue("Height", out IIntValue height);
  241. device.Parameters.SetIntValue("Height", height.Min);
  242. device.Parameters.SetIntValue("Width", RecameraConfig.Width);
  243. device.Parameters.SetIntValue("OffsetX", RecameraConfig.OffsetX);
  244. // 设置采集连续模式
  245. device.Parameters.SetEnumValueByString("AcquisitionMode", "Continuous");
  246. device.Parameters.SetEnumValueByString("TriggerMode", "Off");
  247. Blresult = true;
  248. }
  249. return Blresult;
  250. }
  251. /// <summary>
  252. /// 开启相机采集
  253. /// </summary>
  254. public bool StartCamera()
  255. {
  256. return StartReceiveFuntion();
  257. }
  258. /// <summary>
  259. /// 关闭相机采集
  260. /// </summary>
  261. public void StopCamera()
  262. {
  263. StopReceiveFuntion();
  264. //StopEventGetImage();
  265. }
  266. /// <summary>
  267. /// 释放相机资源
  268. /// </summary>
  269. public void DisPoseCamera()
  270. {
  271. if (device != null)
  272. {
  273. int result = device.StreamGrabber.StopGrabbing();
  274. device.Close();
  275. device.Dispose();
  276. if (result != MvError.MV_OK)
  277. {
  278. LOG.log(("Stop Grabbing Fail!", result));
  279. }
  280. }
  281. }
  282. public DateTime FromUnixTimestamp(long timestamp, bool isMilliseconds = false)
  283. {
  284. try
  285. {
  286. // 如果未指定单位,尝试自动检测
  287. if (!isMilliseconds)
  288. {
  289. // 如果时间戳看起来像毫秒级(13位数字)
  290. if (timestamp.ToString().Length > 10)
  291. {
  292. isMilliseconds = true;
  293. }
  294. }
  295. if (isMilliseconds)
  296. {
  297. // 验证毫秒级时间戳范围
  298. const long minMilliTimestamp = -62135596800000; // 0001-01-01 00:00:00 UTC
  299. const long maxMilliTimestamp = 253402300799999; // 9999-12-31 23:59:59 UTC
  300. if (timestamp < minMilliTimestamp || timestamp > maxMilliTimestamp)
  301. {
  302. throw new ArgumentOutOfRangeException(nameof(timestamp),
  303. "毫秒级时间戳超出有效范围");
  304. }
  305. DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(timestamp);
  306. return TimeZoneInfo.ConvertTimeFromUtc(origin, TimeZoneInfo.Local);
  307. }
  308. else
  309. {
  310. // 验证秒级时间戳范围
  311. const long minTimestamp = -62135596800;
  312. const long maxTimestamp = 253402300799;
  313. if (timestamp < minTimestamp || timestamp > maxTimestamp)
  314. {
  315. throw new ArgumentOutOfRangeException(nameof(timestamp),
  316. "秒级时间戳超出有效范围");
  317. }
  318. DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
  319. return TimeZoneInfo.ConvertTimeFromUtc(origin, TimeZoneInfo.Local);
  320. }
  321. }
  322. catch (ArgumentOutOfRangeException)
  323. {
  324. throw;
  325. }
  326. catch (Exception ex)
  327. {
  328. throw new ArgumentException($"无法转换时间戳 {timestamp}: {ex.Message}", ex);
  329. }
  330. }
  331. /// <summary>
  332. /// 获取一次图片
  333. /// </summary>
  334. /// <param name="IFrameData"></param>
  335. /// <returns></returns>
  336. public bool GetOnceImage(out IFrameOut IFrameData)
  337. {
  338. bool result = false;
  339. int BRet = device.StreamGrabber.GetImageBuffer(1000, out IFrameData);
  340. if (BRet == 0 && IFrameData != null) result = true;
  341. else return result;
  342. var QutuYanshiTime = (DateTime.Now - FromUnixTimestamp((long)IFrameData.HostTimeStamp)).TotalMilliseconds;
  343. if (QutuYanshiTime > 12)
  344. {
  345. Console.WriteLine($"GetOnceImage-识别延时超过了12ms,耗时{QutuYanshiTime}");
  346. }
  347. if (lastframeNum == -1)
  348. {
  349. lastframeNum = IFrameData.FrameNum;
  350. }
  351. else if (lastframeNum == IFrameData.FrameNum - 1)
  352. {
  353. lastframeNum = IFrameData.FrameNum;
  354. }
  355. else
  356. {
  357. //丢帧记录
  358. LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  359. IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum , lastframeNum), 6);
  360. Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  361. IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum , lastframeNum);
  362. lastframeNum = IFrameData.FrameNum;
  363. }
  364. return result;
  365. }
  366. /// <summary>
  367. /// 获取相机图像尺寸信息
  368. /// </summary>
  369. /// <returns></returns>
  370. public CameraImageSizeClass GetCamereImageSize()
  371. {
  372. CameraImageSizeClass cameraImageSize =new CameraImageSizeClass();
  373. if(device != null)
  374. {
  375. device.Parameters.GetIntValue("Width", out IIntValue PixWidth);
  376. device.Parameters.GetIntValue("Height", out IIntValue PixHeight);
  377. cameraImageSize.Height = (int)PixHeight.CurValue;
  378. cameraImageSize.Width = (int)PixWidth.CurValue;
  379. }
  380. return cameraImageSize;
  381. }
  382. /// <summary>
  383. /// 获取当前相机的参数生成Config信息
  384. /// </summary>
  385. /// <returns></returns>
  386. public CameraConfig GetConfigValue()
  387. {
  388. CameraConfig result;
  389. device.Parameters.GetFloatValue("ExposureTime", out IFloatValue exposureTime);
  390. device.Parameters.GetIntValue("AcquisitionLineRate", out IIntValue acquisitionLineRate);
  391. device.Parameters.GetIntValue("OffsetX", out IIntValue offsetX);
  392. device.Parameters.GetIntValue("Width", out IIntValue pixWidth);
  393. device.Parameters.GetStringValue("DeviceUserID", out IStringValue deviceUserID);
  394. device.Parameters.GetStringValue("DeviceModelName", out IStringValue deviceModelName);
  395. device.Parameters.GetStringValue("DeviceSerialNumber", out IStringValue deviceSerialNumber);
  396. result = new CameraConfig()
  397. {
  398. CameraSNNum = deviceSerialNumber.CurValue,
  399. ExposureTimeValue = exposureTime.CurValue,
  400. AcquistionLineRateValue = (int)acquisitionLineRate.CurValue,
  401. Width = (int)pixWidth.CurValue,
  402. OffsetX = (int)offsetX.CurValue,
  403. CameraName = deviceUserID.CurValue,
  404. DeviceName = deviceModelName.CurValue,
  405. CamerNo = _cameraNo
  406. };
  407. return result;
  408. }
  409. /// <summary>
  410. /// 保存参数
  411. /// </summary>
  412. public void SaveConfig()
  413. {
  414. CameraConfig camerasConfig = null;
  415. if (!Directory.Exists(".\\Config\\")) Directory.CreateDirectory(".\\Config\\");
  416. XmlStorage.SerializeToXml(camerasConfig, ".\\Config\\CameraConfig.xml");
  417. }
  418. public bool IsLoadCamera()
  419. {
  420. return device != null;
  421. }
  422. public bool IsConnectCamera()
  423. {
  424. if (device == null) return false;
  425. else
  426. {
  427. return device.IsConnected;
  428. }
  429. }
  430. #endregion
  431. #region 私有方法
  432. /// <summary>
  433. /// 开启采集
  434. /// </summary>
  435. private bool StartReceiveFuntion()
  436. {
  437. bool result = false;
  438. try
  439. {
  440. if(device == null) return result;
  441. device.StreamGrabber.SetImageNodeNum(300);
  442. device.StreamGrabber.StartGrabbing();
  443. result = true;
  444. SystemAlarm.AlarmCancel(AlarmMessageList.相机采集开始失败);
  445. }
  446. catch (Exception ex)
  447. {
  448. //FaultLog.RecordErrorMessage("Start thread failed!, " + ex.Message);
  449. SystemAlarm.AlarmAlert(AlarmMessageList.相机采集开始失败,
  450. "Start thread failed!, " + ex.Message,
  451. "相机采集开始失败, " + ex.Message,
  452. "DLL:CamerClass-StartReceiveFuntion");
  453. throw;
  454. }
  455. return result;
  456. }
  457. /// <summary>
  458. /// 关闭采集
  459. /// </summary>
  460. private void StopReceiveFuntion()
  461. {
  462. try
  463. {
  464. if (device == null) return;
  465. int ret = device.StreamGrabber.StopGrabbing();
  466. if (ret != MvError.MV_OK)
  467. {
  468. //FaultLog.RecordErrorMessage($"Stop grabbing failed:{ret:x8}");
  469. SystemAlarm.AlarmAlert(AlarmMessageList.相机采集停止失败,
  470. $"Stop grabbing failed:{ret:x8}",
  471. $"相机采集停止失败:{ret:x8}",
  472. "DLL:CamerClass-StopReceiveFuntion");
  473. return;
  474. }
  475. SystemAlarm.AlarmCancel(AlarmMessageList.相机采集停止失败);
  476. }
  477. catch (Exception ex)
  478. {
  479. //FaultLog.RecordErrorMessage("Stop thread failed!, " + ex.Message);
  480. SystemAlarm.AlarmAlert(AlarmMessageList.相机采集停止失败,
  481. "Stop thread failed!, " + ex.Message,
  482. "相机采集停止失败, " + ex.Message,
  483. "DLL:CamerClass-StopReceiveFuntion");
  484. throw;
  485. }
  486. }
  487. #endregion
  488. }
  489. }