探测器设置界面增加图像灰度直方图,用于显示实时采集图像的灰度信息,优化图像灰度直方图的显示方式(无图像提示)和优化资源释放。

This commit is contained in:
QI Mingxuan
2026-05-18 14:41:05 +08:00
parent a9d56ebfbd
commit ed0fe92cbe
9 changed files with 131 additions and 6 deletions
@@ -1,15 +1,82 @@
using System;
using System.Windows;
using System.Windows.Controls;
using Prism.Events;
using Prism.Ioc;
using XP.Hardware.Detector.Abstractions;
using XP.Hardware.Detector.Abstractions.Events;
namespace XP.Hardware.Detector.Views
{
/// <summary>
/// 面阵探测器配置视图 | Area detector configuration view
/// 订阅探测器图像采集事件,将图像数据传递给直方图控件
/// </summary>
public partial class DetectorConfigView : UserControl
{
private IEventAggregator _eventAggregator;
private SubscriptionToken _imageSubscriptionToken;
public DetectorConfigView()
{
InitializeComponent();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
/// <summary>
/// 加载时订阅图像采集事件 | Subscribe to image captured event on load
/// </summary>
private void OnLoaded(object sender, RoutedEventArgs e)
{
try
{
_eventAggregator = ContainerLocator.Current?.Resolve<IEventAggregator>();
if (_eventAggregator != null)
{
_imageSubscriptionToken = _eventAggregator.GetEvent<ImageCapturedEvent>()
.Subscribe(OnImageCaptured, ThreadOption.BackgroundThread);
}
}
catch
{
// 事件聚合器不可用时静默降级 | Silent degradation when event aggregator unavailable
}
}
/// <summary>
/// 卸载时取消订阅 | Unsubscribe on unload
/// </summary>
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_eventAggregator != null && _imageSubscriptionToken != null)
{
_eventAggregator.GetEvent<ImageCapturedEvent>().Unsubscribe(_imageSubscriptionToken);
_imageSubscriptionToken = null;
}
}
/// <summary>
/// 图像采集回调:将 ushort[] 转为 byte[] 后传给直方图控件 | Image captured callback
/// </summary>
private void OnImageCaptured(ImageCapturedEventArgs args)
{
if (args?.ImageData == null || args.Width == 0 || args.Height == 0)
return;
try
{
// 将 ushort[] 转换为 little-endian byte[] | Convert ushort[] to little-endian byte[]
var rawBytes = new byte[args.ImageData.Length * 2];
Buffer.BlockCopy(args.ImageData, 0, rawBytes, 0, rawBytes.Length);
// 调用直方图控件更新(控件内部支持从非 UI 线程调用)| Update histogram control (supports non-UI thread calls)
HistogramControl?.UpdateImage(rawBytes, (int)args.Width, (int)args.Height, 16);
}
catch
{
// 异常不影响主流程 | Exception does not affect main flow
}
}
}
}