HikCamera.cs 9.9 KB

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