HikCamera.cs 8.2 KB

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