MainPage.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 
  2. using LogClass;
  3. using MvCameraControl;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Interop;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using static System.Net.Mime.MediaTypeNames;
  16. namespace CCDCountWpf.WpfPage
  17. {
  18. /// <summary>
  19. /// MainPage.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainPage : Page
  22. {
  23. #region 变量与实例
  24. bool IsShow = false;
  25. SystemMonitor monitor = new SystemMonitor();
  26. #endregion
  27. public MainPage()
  28. {
  29. InitializeComponent();
  30. StartUpdataShowDataThread();
  31. }
  32. /// <summary>
  33. /// 启动更新显示数据线程
  34. /// </summary>
  35. private void StartUpdataShowDataThread()
  36. {
  37. IsShow = true;
  38. Bitmap image = null;
  39. const int targetMsPerFrame = 100; // 20FPS=50ms
  40. Stopwatch renderSW = new Stopwatch();
  41. Task.Run(() =>
  42. {
  43. while (IsShow)
  44. {
  45. UpdataShuLiShowData();
  46. UpdateMonitorMessage();
  47. renderSW.Restart();
  48. if (MessageBus.mainThreadClass == null)
  49. {
  50. LOG.log(string.Format("{0}:当前加载相机空", "IdentifyCameraForm-StartShowImageThread"));
  51. Thread.Sleep(100);
  52. continue;
  53. }
  54. if (MessageBus.mainThreadClass.HistoryActiveNum > 0)
  55. {
  56. MessageBus.mainThreadClass.GetShowImage(800, out image);
  57. }
  58. else
  59. {
  60. MessageBus.mainThreadClass.GetNullShowImage(4096, 800, out image);
  61. Thread.Sleep(100);
  62. LOG.log(string.Format("{0}-{1}号相机线程启动异常", "IdentifyCameraForm-StartShowImageThread:", MessageBus.mainThreadClass.ThisCamerNo));
  63. }
  64. if (image == null)
  65. {
  66. LOG.log(string.Format("{0}:获取图片失败", "IdentifyCameraForm-StartShowImageThread"));
  67. continue;
  68. }
  69. System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  70. {
  71. var bitmapImage = ConvertToBitmapImage(image);
  72. ShowBox.Source = bitmapImage;
  73. }));
  74. //进行精准20帧控制
  75. int elapsed = (int)renderSW.ElapsedMilliseconds;
  76. if (elapsed < targetMsPerFrame)
  77. {
  78. Thread.Sleep(targetMsPerFrame - elapsed);
  79. }
  80. Thread.Sleep(50);
  81. }
  82. });
  83. }
  84. /// <summary>
  85. /// 停止更新显示数据线程
  86. /// </summary>
  87. private void StopUpdataShowDataThread()
  88. {
  89. IsShow = false;
  90. }
  91. /// <summary>
  92. /// 更新数粒显示数据
  93. /// </summary>
  94. private void UpdataShuLiShowData()
  95. {
  96. if (MessageBus.mainThreadClass == null) return;
  97. if (MessageBus.mainThreadClass.ShuLiState)
  98. {
  99. System.Windows.Application.Current.Dispatcher.Invoke(() =>
  100. {
  101. CamRunStaticLab.Foreground = MessageBus.mainThreadClass.CameraRunStatic == true ? System.Windows.Media.Brushes.Green : System.Windows.Media.Brushes.Red;
  102. CamRunStaticLab.Content = MessageBus.mainThreadClass.CameraRunStatic == true ? "运行中" : "未运行";
  103. });
  104. }
  105. else
  106. {
  107. System.Windows.Application.Current.Dispatcher.Invoke(() =>
  108. {
  109. CamRunStaticLab.Foreground = System.Windows.Media.Brushes.Yellow;
  110. CamRunStaticLab.Content = "视野遮挡";
  111. });
  112. }
  113. System.Windows.Application.Current.Dispatcher.Invoke(() =>
  114. {
  115. AllActiveNumTbx.Text = MessageBus.mainThreadClass.HistoryActiveNum.ToString();
  116. AllOkNumTbx.Text = MessageBus.mainThreadClass.NgHistoryNum.ToString();
  117. AllNgNumTbx.Text = MessageBus.mainThreadClass.OkHistoryNum.ToString();
  118. ShuLiSpeedTbx.Text = MessageBus.mainThreadClass.GetOneSecondActiveNum().ToString();
  119. });
  120. }
  121. /// <summary>
  122. /// 更新监控信息
  123. /// </summary>
  124. private void UpdateMonitorMessage()
  125. {
  126. System.Windows.Application.Current.Dispatcher.Invoke(() =>
  127. {
  128. RamMonitorLab.Content = $"可用内存: {monitor.GetAvailableMemory()}MB";
  129. CPUMonitorLab.Content = $"CPU: {monitor.GetCpuUsage().ToString("0.00")}%";
  130. });
  131. }
  132. // Bitmap 转 BitmapImage 的辅助方法
  133. private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  134. {
  135. using (MemoryStream memory = new MemoryStream())
  136. {
  137. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  138. memory.Position = 0;
  139. var bitmapImage = new BitmapImage();
  140. bitmapImage.BeginInit();
  141. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  142. bitmapImage.StreamSource = memory;
  143. bitmapImage.EndInit();
  144. return bitmapImage;
  145. }
  146. }
  147. private void DataClear_Click(object sender, RoutedEventArgs e)
  148. {
  149. MessageBus.mainThreadClass.ClearHistoryActive();
  150. }
  151. private void Page_Loaded(object sender, RoutedEventArgs e)
  152. {
  153. this.Width = double.NaN; // 等效于 Auto
  154. this.Height = double.NaN;
  155. }
  156. }
  157. }