CamerEs.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace RailLocalMeter
  8. {
  9. /// <summary>
  10. /// 海康视频流获取及截图操作
  11. /// </summary>
  12. public class CamerEs
  13. {
  14. public CamerEs(string _ip, string _port, string _uid, string _pwd)
  15. {
  16. ip = _ip;
  17. port = _port;
  18. uid = _uid;
  19. pwd = _pwd;
  20. _dvr = new HkDvr();
  21. }
  22. private HkDvr _dvr;
  23. bool ret = false;
  24. private string ip = "", port = "", uid = "", pwd = "";
  25. /// <summary>
  26. /// 连接设备
  27. /// </summary>
  28. /// <param name="Ip">IP地址</param>
  29. /// <param name="Port">端口</param>
  30. /// <param name="Uid">用户名</param>
  31. /// <param name="Pwd">密码</param>
  32. public void Connection()
  33. {
  34. if (_dvr.UserId < 0)
  35. {
  36. //IP,PORT,UID,PWD
  37. ret = _dvr.Init(ip + "," + port + "," + uid + "," + pwd);
  38. ret = _dvr.Open();
  39. }
  40. }
  41. private bool startTalk = false;
  42. /// <summary>
  43. /// 打开视频
  44. /// </summary>
  45. /// <param name="picture"></param>
  46. /// <param name="i"></param>
  47. public void RealPlay(PictureBox picture, int i)
  48. {
  49. if (_dvr.UserId > -1)
  50. {
  51. _dvr.RealPlay(i, picture.Handle);
  52. }
  53. }
  54. /// <summary>
  55. /// 关闭视频
  56. /// </summary>
  57. /// <param name="i"></param>
  58. public void StopRealPlay(int i)
  59. {
  60. if (_dvr.UserId > -1)
  61. {
  62. _dvr.StopRealPlay(i);
  63. }
  64. }
  65. /// <summary>
  66. /// 关闭视频
  67. /// </summary>
  68. public void Close()
  69. {
  70. if (_dvr.UserId > -1)
  71. {
  72. _dvr.Close();
  73. }
  74. }
  75. /// <summary>
  76. /// 截图
  77. /// </summary>
  78. /// <param name="voiType">0抓拍机 1一般的摄像头</param>
  79. /// <param name="filePath">文件存储位置及文件名称,文件名称应该是 计量作业编号_计量点_第几张 or 计量作业编号_第几张</param>
  80. /// <returns>是否截图成功,true成功,false失败,异常的话会在logs中写入信息</returns>
  81. public bool CapPic(uint channel, string filePath)
  82. {
  83. return _dvr.CapturePicture(channel, filePath);
  84. /*
  85. _dvr.CapturePicture(1, 1, filePath);
  86. _dvr.CapturePicture(1, 2, filePath.Substring(0, filePath.Length - 5) + "2.jpg");
  87. _dvr.CapturePicture(1, 3, filePath.Substring(0, filePath.Length - 5) + "3.jpg");
  88. return true;
  89. //*/
  90. }
  91. public bool CapPicFromVedio(uint chanel, string filePath)
  92. {
  93. try
  94. {
  95. return _dvr.CapturePicture(chanel, filePath + chanel + ".jpg");
  96. }
  97. catch (Exception)
  98. {
  99. return false;
  100. }
  101. }
  102. /// <summary>
  103. /// 硬盘录像机截图
  104. /// </summary>
  105. /// <param name="filePath"></param>
  106. /// <returns></returns>
  107. public bool CapPicFromVedio(string filePath)
  108. {
  109. try
  110. {
  111. bool a1 = _dvr.CapturePicture(1, filePath + "1.jpg");
  112. bool a2 = _dvr.CapturePicture(2, filePath + "2.jpg");
  113. bool a3 = _dvr.CapturePicture(3, filePath + "3.jpg");
  114. bool a4 = _dvr.CapturePicture(4, filePath + "4.jpg");
  115. bool a5 = _dvr.CapturePicture(5, filePath + "5.jpg");
  116. bool a6 = _dvr.CapturePicture(6, filePath + "6.jpg");
  117. return true;
  118. }
  119. catch (Exception)
  120. {
  121. return false;
  122. }
  123. }
  124. /// <summary>
  125. /// 打开对讲
  126. /// </summary>
  127. /// <returns></returns>
  128. public bool StartTalk()
  129. {
  130. if (_dvr.UserId > -1 && !startTalk)
  131. {
  132. _dvr.StartTalk();
  133. startTalk = true;
  134. }
  135. return true;
  136. }
  137. /// <summary>
  138. /// 关闭对讲
  139. /// </summary>
  140. /// <returns></returns>
  141. public bool StopTalk()
  142. {
  143. if (_dvr.UserId > -1 && startTalk)
  144. {
  145. _dvr.StopTalk();
  146. startTalk = false;
  147. return true;
  148. }
  149. return true;
  150. }
  151. public bool SendVoice(string soundName)
  152. {
  153. bool ret = false;
  154. _dvr.SetVolume(0xffff);
  155. bool voiceOpend = false;
  156. if (_dvr.VoiceHandle > -1) //播放语音前,需要关闭对讲
  157. {
  158. _dvr.StopTalk();
  159. voiceOpend = true;
  160. }
  161. bool b = _dvr.OpenSound();
  162. string path = AppConfigCache.path + "\\Sound\\" + soundName + ".wav";
  163. ret = _dvr.SendVoiceData(path);
  164. _dvr.CloseSound();
  165. if (voiceOpend)
  166. {
  167. _dvr.StartTalk();
  168. }
  169. return ret;
  170. }
  171. public bool SendVoiceWrw(string soundName)
  172. {
  173. bool ret = false;
  174. _dvr.SetVolume(0xffff);
  175. bool voiceOpend = false;
  176. if (_dvr.VoiceHandle > -1) //播放语音前,需要关闭对讲
  177. {
  178. _dvr.StopTalk();
  179. voiceOpend = true;
  180. }
  181. bool b = _dvr.OpenSound();
  182. string path = AppConfigCache.path + "\\Sound\\" + soundName + ".wav";
  183. for (int i = 0; i < 5; i++)
  184. {
  185. ret = _dvr.SendVoiceDataWrw(path);
  186. if (ret) break;
  187. }
  188. _dvr.CloseSound();
  189. if (voiceOpend)
  190. {
  191. _dvr.StartTalk();
  192. }
  193. //LogController.GetInstance().WriteLog(33, $"发送语音\"{soundName}\",结果:{ret}");
  194. return ret;
  195. }
  196. }
  197. }