MainWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. using System.Windows.Interop;
  13. using System.Xml.Linq;
  14. using VideoSurveillanceAdapter;
  15. using VideoSurveillanceAdapter.Model;
  16. namespace VideoTest
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. bool isInit = false;
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. Closed += MainWindow_Closed;
  28. System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
  29. IntPtr hwnd = new WindowInteropHelper(this).Handle; //----this就是要获取句柄的窗体的类名;
  30. //IntPtr hwnd1 = ((HwndSource)PresentationSource.FromVisual(uielement)).Handle; //----uielement就是要获取句柄的控件,该控件必须继承自UIElement。
  31. }
  32. private void MainWindow_Closed(object sender, EventArgs e)
  33. {
  34. if (VideoPlayer == null) return;
  35. VideoPlayer.StopRealPlay();
  36. VideoPlayer.LogoutCamera();
  37. VideoPlayer.DisposeSDK();
  38. isInit = false;
  39. }
  40. private VideoPlayer VideoPlayer { get; set; }
  41. private void Login_Click(object sender, RoutedEventArgs e)
  42. {
  43. try
  44. {
  45. if (!isInit)
  46. isInit = VideoPlayer.InitSDK();
  47. if (VideoPlayer == null)
  48. {
  49. string DVRIPAddress = textBoxIP.Text; //设备IP地址或者域名
  50. int DVRPortNumber = int.Parse(textBoxPort.Text);//设备服务端口号
  51. string DVRUserName = textBoxUserName.Text;//设备登录用户名
  52. string DVRPassword = textBoxPassword.Text;//设备登录密码
  53. VideoPlayer = new VideoPlayer();
  54. string brandStr = cbBrand.SelectedValue.ToString();
  55. CameraBrand brand = CameraBrand.None;
  56. Enum.TryParse(brandStr, false, out brand);
  57. if (brand == CameraBrand.None) return;
  58. VideoPlayer.CameraConfigure = new CameraConfigure
  59. {
  60. ID = Guid.NewGuid().ToString(),
  61. Name = "相机1",
  62. Ip = DVRIPAddress,
  63. Port = DVRPortNumber,
  64. UserName = DVRUserName,
  65. Password = DVRPassword,
  66. IsRecordVideo = false,
  67. VideoPath = AppDomain.CurrentDomain.BaseDirectory + @"Video\",
  68. CapturePath = AppDomain.CurrentDomain.BaseDirectory + @"\Capture\",
  69. CameraBrand = brand,
  70. PlayFrame = PlayFrame.MainFrame
  71. };
  72. if (!string.IsNullOrWhiteSpace(cbPlayChannel.Text))
  73. {
  74. VideoPlayer.CameraConfigure.PlayChannel = Convert.ToInt32(cbPlayChannel.Text);
  75. }
  76. VideoPlayer.PlayWndHandle = ((HwndSource)PresentationSource.FromVisual(this.WinBox)).Handle;
  77. }
  78. Button btn = sender as Button;
  79. if (btn.Content.ToString() == "登录")
  80. {
  81. bool isSuccess = VideoPlayer.LoginCamera();
  82. LogText.Text = isSuccess ? "登录成功!" : "登录失败!";
  83. btn.Content = isSuccess ? "登出" : "登录";
  84. }
  85. else
  86. {
  87. bool isSuccess = VideoPlayer.StopRealPlay();
  88. LogText.Text = isSuccess ? "停止成功!" : "停止失败!";
  89. isSuccess = VideoPlayer.LogoutCamera();
  90. LogText.Text = isSuccess ? "登出成功!" : "登出失败!";
  91. btn.Content = isSuccess ? "登录" : "登出";
  92. if (isSuccess)
  93. VideoPlayer = null;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.ToString());
  99. }
  100. }
  101. private void VideoPlay_Click(object sender, RoutedEventArgs e)
  102. {
  103. try
  104. {
  105. if (VideoPlayer == null) return;
  106. Button btn = sender as Button;
  107. if (btn.Content.ToString() == "播放")
  108. {
  109. VideoPlayer.CameraConfigure.PlayChannel =
  110. Convert.ToInt32(String.IsNullOrWhiteSpace(cbPlayChannel.Text) ? "0" : cbPlayChannel.Text);
  111. var channelArr = this.channels.Text.Split(new char[] { ',', ',' }).Select(t => int.Parse(t));
  112. if (channelArr.Count() > 0)
  113. {
  114. int index = 0;
  115. IntPtr intPtrBox = VideoPlayer.PlayWndHandle = ((HwndSource)PresentationSource.FromVisual(this.WinBox)).Handle;
  116. foreach (var item in channelArr)
  117. {
  118. if (index == 1)
  119. {
  120. intPtrBox = this.WinBox1.Handle;
  121. }
  122. bool isSuccess = VideoPlayer.StartRealNVRPlay(item, intPtrBox);
  123. //VideoPlayer.StartRealPlayM4(VideoPlayer.CameraConfigure.PlayChannel, WinBox1.Handle);
  124. if (!Directory.Exists(VideoPlayer.CameraConfigure.VideoPath))
  125. Directory.CreateDirectory(VideoPlayer.CameraConfigure.VideoPath);
  126. string fullName = VideoPlayer.CameraConfigure.VideoPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4";
  127. VideoPlayer.RecordVideo(fullName);
  128. LogText.Text = isSuccess ? "播放成功!" : "播放失败!";
  129. btn.Content = isSuccess ? "停止" : "播放";
  130. index++;
  131. }
  132. }
  133. }
  134. else
  135. {
  136. VideoPlayer.StopRecordVideo();
  137. bool isSuccess = VideoPlayer.StopRealPlay();
  138. LogText.Text = isSuccess ? "停止成功!" : "停止失败!";
  139. btn.Content = isSuccess ? "播放" : "停止";
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. MessageBox.Show(ex.ToString());
  145. }
  146. }
  147. private void Capture_Click(object sender, RoutedEventArgs e)
  148. {
  149. string filePath = VideoPlayer.CameraConfigure.CapturePath;
  150. if (!Directory.Exists(filePath))
  151. Directory.CreateDirectory(filePath);
  152. var channels = this.channels.Text.Split(',');
  153. LogText.Text = "";
  154. foreach (var item in channels)
  155. {
  156. //Task.Run(() =>
  157. //{
  158. string fileName = VideoPlayer.CameraConfigure.Name + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "channel" + item + ".jpg";
  159. bool isSuccess = VideoPlayer.CaptureJPEGPicture(filePath + fileName, int.Parse(item));
  160. Dispatcher.Invoke(() => LogText.Text += isSuccess ? "截图成功!" : "截图失败!");
  161. //});
  162. }
  163. }
  164. private void MeasureTemperature_Click(object sender, RoutedEventArgs e)
  165. {
  166. if (VideoPlayer == null) return;
  167. RstText.Text = "正在获取温度,请等待... ...";
  168. int tNo = 0;
  169. if (int.TryParse(cbMeasureTemperature.Text, out _))
  170. {
  171. tNo = Convert.ToInt32(cbMeasureTemperature.Text);
  172. }
  173. int pNo = Convert.ToInt32(cbPreset.Text);
  174. Task<bool> goTask = Task.Run(() =>
  175. {
  176. bool isSccess = VideoPlayer.GotoPreset(pNo);
  177. Thread.Sleep(1000);
  178. return isSccess;
  179. });
  180. int channel = int.Parse(cbPlayChannel.Text);
  181. goTask.GetAwaiter().OnCompleted(() =>
  182. {
  183. if (goTask.Result)
  184. {
  185. for (int i = 0; i < 1000; i++)
  186. {
  187. //Task<List<RealTimeTemperature>> task = Task.Run(() =>
  188. //{
  189. // return VideoPlayer.GetRealTimeTemperature(pNo, tNo, channel);
  190. //});
  191. List<RealTimeTemperature> tRst = VideoPlayer.GetRealTimeTemperature(pNo, tNo, channel);
  192. StringBuilder rstSB = new StringBuilder();
  193. foreach (var rst in tRst)
  194. {
  195. rstSB.Append("--------------------" + "\n");
  196. rstSB.Append("时间:" + rst.MeasureTime.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
  197. rstSB.Append("预置点号:" + rst.PresetNo + "\n");
  198. rstSB.Append("规则ID:" + rst.RuleID + "\n");
  199. rstSB.Append("规则名称:" + rst.RuleName + "\n");
  200. rstSB.Append("规则标定类型:" + rst.RuleType + "\n");
  201. rstSB.Append("测温信息:" + rst.TemperatureValue + "(" + rst.TemperatureUnit + ")" + "\n");
  202. rstSB.Append("--------------------");
  203. }
  204. Dispatcher.Invoke(() =>
  205. {
  206. RstText.Text = rstSB.ToString();
  207. Debug.WriteLine(rstSB.ToString());
  208. });
  209. //task.GetAwaiter().OnCompleted(() =>
  210. //{
  211. //});
  212. Task.Delay(1000).Wait();
  213. }
  214. }
  215. });
  216. }
  217. //private void cbPreset_SelectionChanged(object sender, SelectionChangedEventArgs e)
  218. //{
  219. // if (VideoPlayer == null) return;
  220. // int pNo = Convert.ToInt32(cbPreset.Text);
  221. // VideoPlayer.GotoPreset(pNo);
  222. //}
  223. #region
  224. private void Up_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  225. {
  226. PTZControl(PTZCategory.Up, 0);
  227. }
  228. private void Up_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  229. {
  230. PTZControl(PTZCategory.Up, 1);
  231. }
  232. private void Down_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  233. {
  234. PTZControl(PTZCategory.Down, 0);
  235. }
  236. private void Down_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  237. {
  238. PTZControl(PTZCategory.Down, 1);
  239. }
  240. private void Left_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  241. {
  242. PTZControl(PTZCategory.Left, 0);
  243. }
  244. private void Left_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  245. {
  246. PTZControl(PTZCategory.Left, 1);
  247. }
  248. private void Right_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  249. {
  250. PTZControl(PTZCategory.Right, 0);
  251. }
  252. private void Right_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  253. {
  254. PTZControl(PTZCategory.Right, 1);
  255. }
  256. private void PTZControl(PTZCategory category, uint dwStop)
  257. {
  258. if (VideoPlayer == null) return;
  259. VideoPlayer.PTZControl(category, dwStop);
  260. }
  261. #endregion
  262. private void TurnTopreset_Click(object sender, RoutedEventArgs e)
  263. {
  264. if (VideoPlayer == null) return;
  265. int pNo = Convert.ToInt32(cbPreset.Text);
  266. var success = VideoPlayer.GotoPreset(pNo);
  267. Console.WriteLine(success);
  268. }
  269. private void Setpreset_Click(object sender, RoutedEventArgs e)
  270. {
  271. if (VideoPlayer == null) return;
  272. int pNo = Convert.ToInt32(cbPreset.Text);
  273. int channel = int.Parse(cbPlayChannel.Text);
  274. var success = VideoPlayer.SetPreset(pNo, channel, "测试预置位名");
  275. Console.WriteLine(success);
  276. }
  277. /// <summary>
  278. ///
  279. /// </summary>
  280. /// <param name="sender"></param>
  281. /// <param name="e"></param>
  282. private void DownloadPlayback_Click(object sender, RoutedEventArgs e)
  283. {
  284. if (VideoPlayer == null) return;
  285. bool isSccess = VideoPlayer.DownloadFileByTime(Path.GetFullPath("./video/video.mp4"),
  286. DateTime.Now - TimeSpan.FromMinutes(120),
  287. DateTime.Now - TimeSpan.FromMinutes(18),
  288. new DownloadFileCallBack(arg =>
  289. {
  290. Debug.WriteLine("Sccess");
  291. }), int.Parse(cbPlayChannel.Text));
  292. Dispatcher.Invoke(() =>
  293. {
  294. RstText.Text = isSccess.ToString();
  295. });
  296. }
  297. private void Playbackt_Click(object sender, RoutedEventArgs e)
  298. {
  299. //VideoPlayer.st
  300. try
  301. {
  302. var channelArr = this.channels.Text.Split(new char[] { ',', ',' }).Select(t => int.Parse(t));
  303. if (channelArr.Count() > 0)
  304. {
  305. int index = 0;
  306. IntPtr intPtrBox = ((HwndSource)PresentationSource.FromVisual(this.WinBox)).Handle;//.Handle;
  307. foreach (var item in channelArr)
  308. {
  309. if (index == 1)
  310. {
  311. intPtrBox = this.WinBox1.Handle;
  312. }
  313. var startDate = DateTime.Parse(startTime.Text);
  314. var endDate = DateTime.Parse(endTime.Text);
  315. bool isSuccess = VideoPlayer.StartPlayBackByTime(startDate, endDate, item, intPtrBox);
  316. LogText.Text = isSuccess ? "播放成功!" : "播放失败!";
  317. index++;
  318. }
  319. }
  320. }
  321. catch (Exception ex)
  322. {
  323. MessageBox.Show(ex.Message);
  324. }
  325. }
  326. private void StartRealPlayM4_Click(object sender, RoutedEventArgs e)
  327. {
  328. VideoPlayer.CameraConfigure.PlayChannel = Convert.ToInt32(cbPlayChannel.Text);
  329. bool isSuccess = VideoPlayer.StartRealPlayM4(VideoPlayer.CameraConfigure.PlayChannel, WinBox1.Handle);
  330. }
  331. private void ShowOSD_Click(object sender, RoutedEventArgs e)
  332. {
  333. VideoPlayer.ShowOSDInfo(true);
  334. }
  335. private void CloseOSD_Click(object sender, RoutedEventArgs e)
  336. {
  337. VideoPlayer.ShowOSDInfo(false);
  338. }
  339. }
  340. }