HikCamera.cs 8.2 KB

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