HikCamera.cs 9.9 KB

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