HikCamera.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // 海康相机实例类
  2. using MvCameraControl;
  3. using MvFGCtrlC.NET;
  4. using MvvmScaffoldFrame48.DLL.ConfigTools;
  5. using MvvmScaffoldFrame48.DLL.LogTools;
  6. using MvvmScaffoldFrame48.Model.StorageModel.Configs;
  7. using MvvmScaffoldFrame48.Model.StorageModel.HikVisionCamera;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace MvvmScaffoldFrame48.DLL.CameraTools
  15. {
  16. /// <summary>
  17. /// 海康相机操作类
  18. /// </summary>
  19. public class HikCamera
  20. {
  21. #region 实例
  22. /// <summary>
  23. /// 相机实例
  24. /// </summary>
  25. private IDevice _device = null;
  26. public IDevice Device
  27. {
  28. get
  29. {
  30. return _device;
  31. }
  32. }
  33. /// <summary>
  34. /// 设备事件委托
  35. /// </summary>
  36. public event EventHandler<FrameGrabbedEventArgs> FrameGrabbedEvent;
  37. private DateTime _lastFrame = DateTime.MinValue;
  38. #endregion
  39. #region 变量
  40. /// <summary>
  41. /// 最后一次帧号记录
  42. /// </summary>
  43. private long lastframeNum = -1;
  44. /// <summary>
  45. /// 帧率限制下最小帧间隔(单位ms)
  46. /// </summary>
  47. private double minIntervalMs;
  48. /// <summary>
  49. /// 是否进行取图帧率限制
  50. /// </summary>
  51. private bool isFrameRateLimit = false;
  52. public bool IsFrameRateLimit
  53. {
  54. get
  55. {
  56. return isFrameRateLimit;
  57. }
  58. set
  59. {
  60. isFrameRateLimit = value;
  61. }
  62. }
  63. /// <summary>
  64. /// 取图帧率限制
  65. /// </summary>
  66. private int frameRateLimit;
  67. public int FrameRateLimit
  68. {
  69. get
  70. {
  71. return frameRateLimit;
  72. }
  73. set
  74. {
  75. frameRateLimit = value;
  76. minIntervalMs = 1000.0 / frameRateLimit;
  77. }
  78. }
  79. #endregion
  80. #region 构造函数
  81. /// <summary>
  82. /// 相机构造函数 不加载相机
  83. /// </summary>
  84. public HikCamera()
  85. {
  86. if (_device != null && _device.IsConnected)
  87. {
  88. _device.Close();
  89. _device.Dispose();
  90. }
  91. }
  92. /// <summary>
  93. /// 相机构造函数
  94. /// </summary>
  95. /// <param name="device">相机实例</param>
  96. public HikCamera(IDevice device)
  97. {
  98. if (_device != null && _device.IsConnected)
  99. {
  100. _device.Close();
  101. _device.Dispose();
  102. }
  103. this._device = device ?? throw new Exception("相机实例不能为空");
  104. }
  105. /// <summary>
  106. /// 相机构造函数
  107. /// </summary>
  108. /// <param name="deviceInfo">相机信息</param>
  109. public HikCamera(IDeviceInfo deviceInfo)
  110. {
  111. if (_device != null && _device.IsConnected)
  112. {
  113. _device.Close();
  114. _device.Dispose();
  115. }
  116. try
  117. {
  118. // 打开设备
  119. _device = DeviceFactory.CreateDevice(deviceInfo);
  120. }
  121. catch (Exception ex)
  122. {
  123. throw new Exception(ex.Message);
  124. }
  125. int result = _device.Open();
  126. if (result != MvError.MV_OK)
  127. {
  128. Console.WriteLine("打开相机失败");
  129. return;
  130. }
  131. // 判断是否为gige设备
  132. if (_device is IGigEDevice)
  133. {
  134. // 转换为gigE设备
  135. IGigEDevice gigEDevice = _device as IGigEDevice;
  136. // 探测网络最佳包大小(只对GigE相机有效)
  137. result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
  138. if (result != MvError.MV_OK)
  139. {
  140. //Log("Warning: Get Packet Size failed!", result);
  141. }
  142. else
  143. {
  144. result = _device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
  145. if (result != MvError.MV_OK)
  146. {
  147. //Log("Warning: Set Packet Size failed!", result);
  148. }
  149. }
  150. }
  151. }
  152. #endregion
  153. #region 共有方法
  154. /// <summary>
  155. /// 重新加载指定相机
  156. /// </summary>
  157. /// <param name="deviceInfo">相机信息</param>
  158. /// <returns>true为加载成功,false为加载失败</returns>
  159. public bool ReLoadCameraDevice(IDeviceInfo deviceInfo)
  160. {
  161. bool Blresult = false;
  162. if (_device != null && _device.IsConnected)
  163. {
  164. _device.Close();
  165. _device.Dispose();
  166. }
  167. try
  168. {
  169. // 打开设备
  170. _device = DeviceFactory.CreateDevice(deviceInfo);
  171. Blresult = true;
  172. }
  173. catch
  174. {
  175. return Blresult;
  176. }
  177. int result = _device.Open();
  178. if (result != MvError.MV_OK)
  179. {
  180. Blresult = false;
  181. return Blresult;
  182. }
  183. // 判断是否为gige设备
  184. if (_device is IGigEDevice)
  185. {
  186. // 转换为gigE设备
  187. IGigEDevice gigEDevice = _device as IGigEDevice;
  188. // 探测网络最佳包大小(只对GigE相机有效)
  189. result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
  190. if (result != MvError.MV_OK)
  191. {
  192. //Log("Warning: Get Packet Size failed!", result);
  193. }
  194. else
  195. {
  196. result = _device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
  197. if (result != MvError.MV_OK)
  198. {
  199. //Log("Warning: Set Packet Size failed!", result);
  200. }
  201. }
  202. }
  203. return Blresult;
  204. }
  205. /// <summary>
  206. /// 加载线阵相机参数
  207. /// </summary>
  208. /// <param name="cameraParameter"></param>
  209. /// <returns></returns>
  210. public int LoadLineCameraConfig(LinescanCameraParameterModel cameraParameter)
  211. {
  212. int result = 0;
  213. _device.Parameters.SetFloatValue("ExposureTime", cameraParameter.ExposureTimeValue);
  214. _device.Parameters.SetIntValue("AcquisitionLineRate", cameraParameter.AcquistionLineRateValue);
  215. _device.Parameters.SetIntValue("Width", cameraParameter.Width);
  216. _device.Parameters.SetIntValue("Height", cameraParameter.Height);
  217. _device.Parameters.SetIntValue("OffsetX", cameraParameter.OffsetX);
  218. return result;
  219. }
  220. /// <summary>
  221. /// 加载线阵相机参数(从序列化字符串读取)
  222. /// </summary>
  223. /// <param name="cameraParameter"></param>
  224. /// <returns></returns>
  225. public int LoadLineCameraConfig(string cameraParameter)
  226. {
  227. int result = 0;
  228. LinescanCameraParameterModel cameraParameterModel = XMLReadWrite.DeserializeFromString<LinescanCameraParameterModel>(cameraParameter);
  229. if (cameraParameterModel != null)
  230. {
  231. _device.Parameters.SetFloatValue("ExposureTime", cameraParameterModel.ExposureTimeValue);
  232. _device.Parameters.SetIntValue("AcquisitionLineRate", cameraParameterModel.AcquistionLineRateValue);
  233. _device.Parameters.SetIntValue("Width", cameraParameterModel.Width);
  234. _device.Parameters.SetIntValue("Height", cameraParameterModel.Height);
  235. _device.Parameters.SetIntValue("OffsetX", cameraParameterModel.OffsetX);
  236. }
  237. return result;
  238. }
  239. /// <summary>
  240. /// 回调型取图方法-开启采集
  241. /// </summary>
  242. /// <returns></returns>
  243. public bool StartReceiveFuntionSetFrameGrabedEvent()
  244. {
  245. bool result = false;
  246. try
  247. {
  248. if (_device == null) return result;
  249. _device.StreamGrabber.SetImageNodeNum(30);
  250. // 注册回调函数
  251. _device.StreamGrabber.FrameGrabedEvent += FrameGrabbedEventHandler;
  252. int ret = _device.StreamGrabber.StartGrabbing();
  253. if (ret == MvError.MV_OK)
  254. {
  255. result = true;
  256. }
  257. }
  258. catch
  259. {
  260. result = false;
  261. }
  262. return result;
  263. }
  264. /// <summary>
  265. /// 回调型取图方法-关闭采集
  266. /// </summary>
  267. /// <returns></returns>
  268. public bool StopReceiveFuntionSetFrameGrabedEvent()
  269. {
  270. bool result = false;
  271. try
  272. {
  273. if (_device == null) return result;
  274. _device.StreamGrabber.FrameGrabedEvent -= FrameGrabbedEventHandler;
  275. int ret = _device.StreamGrabber.StopGrabbing();
  276. if (ret == MvError.MV_OK)
  277. {
  278. result = true;
  279. }
  280. }
  281. catch
  282. {
  283. result = false;
  284. }
  285. return result;
  286. }
  287. /// <summary>
  288. /// 获取相机图像尺寸信息
  289. /// </summary>
  290. /// <returns></returns>
  291. public CameraImageSizeCModel GetCamereImageSize()
  292. {
  293. CameraImageSizeCModel cameraImageSize = null;
  294. if (_device != null)
  295. {
  296. try
  297. {
  298. _device.Parameters.GetIntValue("Width", out IIntValue PixWidth);
  299. _device.Parameters.GetIntValue("Height", out IIntValue PixHeight);
  300. cameraImageSize = new CameraImageSizeCModel()
  301. {
  302. Height = (int)PixHeight.CurValue,
  303. Width = (int)PixWidth.CurValue
  304. };
  305. }
  306. catch
  307. {
  308. return null;
  309. }
  310. }
  311. return cameraImageSize;
  312. }
  313. #endregion
  314. #region 私有方法
  315. /// <summary>
  316. /// 图像抓取事件处理方法
  317. /// </summary>
  318. private void FrameGrabbedEventHandler(object sender,FrameGrabbedEventArgs e)
  319. {
  320. if(isFrameRateLimit && frameRateLimit>0)
  321. {
  322. TimeSpan elapsed = DateTime.Now - _lastFrame;
  323. if (elapsed.TotalSeconds > minIntervalMs)
  324. {
  325. // 触发外部注册的事件
  326. FrameGrabbedEvent?.Invoke(this, e);
  327. _lastFrame = DateTime.Now;
  328. }
  329. }
  330. else
  331. {
  332. // 触发外部注册的事件
  333. FrameGrabbedEvent?.Invoke(this, e);
  334. if (lastframeNum == -1)
  335. {
  336. lastframeNum = e.FrameOut.FrameNum;
  337. }
  338. else if (lastframeNum == e.FrameOut.FrameNum - 1)
  339. {
  340. lastframeNum = e.FrameOut.FrameNum;
  341. }
  342. else
  343. {
  344. //丢帧记录
  345. TxtLog.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  346. e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum), 6);
  347. Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
  348. e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum);
  349. lastframeNum = e.FrameOut.FrameNum;
  350. }
  351. }
  352. e.FrameOut.Dispose();
  353. }
  354. #endregion
  355. }
  356. }