| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
-
- using LogClass;
- using MvCameraControl;
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Interop;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using static System.Net.Mime.MediaTypeNames;
- namespace CCDCountWpf.WpfPage
- {
- /// <summary>
- /// MainPage.xaml 的交互逻辑
- /// </summary>
- public partial class MainPage : Page
- {
- #region 变量与实例
- bool IsShow = false;
- SystemMonitor monitor = new SystemMonitor();
- #endregion
- public MainPage()
- {
- InitializeComponent();
- StartUpdataShowDataThread();
- }
- /// <summary>
- /// 启动更新显示数据线程
- /// </summary>
- private void StartUpdataShowDataThread()
- {
- IsShow = true;
- Bitmap image = null;
- const int targetMsPerFrame = 100; // 20FPS=50ms
- Stopwatch renderSW = new Stopwatch();
- Task.Run(() =>
- {
- while (IsShow)
- {
- UpdataShuLiShowData();
- UpdateMonitorMessage();
- renderSW.Restart();
- if (MessageBus.mainThreadClass == null)
- {
- LOG.log(string.Format("{0}:当前加载相机空", "IdentifyCameraForm-StartShowImageThread"));
- Thread.Sleep(100);
- continue;
- }
- if (MessageBus.mainThreadClass.HistoryActiveNum > 0)
- {
- MessageBus.mainThreadClass.GetShowImage(800, out image);
- }
- else
- {
- MessageBus.mainThreadClass.GetNullShowImage(4096, 800, out image);
- Thread.Sleep(100);
- LOG.log(string.Format("{0}-{1}号相机线程启动异常", "IdentifyCameraForm-StartShowImageThread:", MessageBus.mainThreadClass.ThisCamerNo));
- }
- if (image == null)
- {
- LOG.log(string.Format("{0}:获取图片失败", "IdentifyCameraForm-StartShowImageThread"));
- continue;
- }
- System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- var bitmapImage = ConvertToBitmapImage(image);
- ShowBox.Source = bitmapImage;
- }));
- //进行精准20帧控制
- int elapsed = (int)renderSW.ElapsedMilliseconds;
- if (elapsed < targetMsPerFrame)
- {
- Thread.Sleep(targetMsPerFrame - elapsed);
- }
- Thread.Sleep(50);
- }
- });
- }
- /// <summary>
- /// 停止更新显示数据线程
- /// </summary>
- private void StopUpdataShowDataThread()
- {
- IsShow = false;
- }
- /// <summary>
- /// 更新数粒显示数据
- /// </summary>
- private void UpdataShuLiShowData()
- {
- if (MessageBus.mainThreadClass == null) return;
- if (MessageBus.mainThreadClass.ShuLiState)
- {
- System.Windows.Application.Current.Dispatcher.Invoke(() =>
- {
- CamRunStaticLab.Foreground = MessageBus.mainThreadClass.CameraRunStatic == true ? System.Windows.Media.Brushes.Green : System.Windows.Media.Brushes.Red;
- CamRunStaticLab.Content = MessageBus.mainThreadClass.CameraRunStatic == true ? "运行中" : "未运行";
- });
- }
- else
- {
- System.Windows.Application.Current.Dispatcher.Invoke(() =>
- {
- CamRunStaticLab.Foreground = System.Windows.Media.Brushes.Yellow;
- CamRunStaticLab.Content = "视野遮挡";
- });
- }
- System.Windows.Application.Current.Dispatcher.Invoke(() =>
- {
- AllActiveNumTbx.Text = MessageBus.mainThreadClass.HistoryActiveNum.ToString();
- AllOkNumTbx.Text = MessageBus.mainThreadClass.NgHistoryNum.ToString();
- AllNgNumTbx.Text = MessageBus.mainThreadClass.OkHistoryNum.ToString();
- ShuLiSpeedTbx.Text = MessageBus.mainThreadClass.GetOneSecondActiveNum().ToString();
- });
- }
- /// <summary>
- /// 更新监控信息
- /// </summary>
- private void UpdateMonitorMessage()
- {
- System.Windows.Application.Current.Dispatcher.Invoke(() =>
- {
- RamMonitorLab.Content = $"可用内存: {monitor.GetAvailableMemory()}MB";
- CPUMonitorLab.Content = $"CPU: {monitor.GetCpuUsage().ToString("0.00")}%";
- });
- }
- // Bitmap 转 BitmapImage 的辅助方法
- private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
- {
- using (MemoryStream memory = new MemoryStream())
- {
- bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
- memory.Position = 0;
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
- bitmapImage.StreamSource = memory;
- bitmapImage.EndInit();
- return bitmapImage;
- }
- }
- private void DataClear_Click(object sender, RoutedEventArgs e)
- {
- MessageBus.mainThreadClass.ClearHistoryActive();
- }
- private void Page_Loaded(object sender, RoutedEventArgs e)
- {
- this.Width = double.NaN; // 等效于 Auto
- this.Height = double.NaN;
- }
- }
- }
|