HikCamera.cs 7.6 KB

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