HikCamera.cs 7.6 KB

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