2025-12-27 07:05:07 +08:00
|
|
|
|
using OpenCvSharp;
|
2025-12-27 07:25:32 +08:00
|
|
|
|
namespace SHH.CameraSdk
|
2025-12-27 07:05:07 +08:00
|
|
|
|
{
|
2025-12-27 07:25:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// [图像缩放服务]
|
|
|
|
|
|
/// 实现:基于基类,专注于将原图缩放并挂载到 TargetMat
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ImageScaleCluster : BaseFrameProcessor<ScaleWorker>
|
2025-12-27 07:05:07 +08:00
|
|
|
|
{
|
2025-12-27 14:16:50 +08:00
|
|
|
|
public ImageScaleCluster(int count, ProcessingConfigManager configManager)
|
|
|
|
|
|
: base(count, "ScaleCluster", configManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2025-12-27 07:05:07 +08:00
|
|
|
|
|
2025-12-27 14:16:50 +08:00
|
|
|
|
protected override ScaleWorker CreateWorker(int id) => new ScaleWorker(this, _configManager);
|
2025-12-27 07:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-27 07:25:32 +08:00
|
|
|
|
public class ScaleWorker : BaseWorker
|
2025-12-27 07:05:07 +08:00
|
|
|
|
{
|
2025-12-27 07:25:32 +08:00
|
|
|
|
private readonly ImageScaleCluster _parent;
|
2025-12-27 14:16:50 +08:00
|
|
|
|
private readonly ProcessingConfigManager _configManager;
|
|
|
|
|
|
|
|
|
|
|
|
public ScaleWorker(ImageScaleCluster parent, ProcessingConfigManager configManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
_parent = parent;
|
|
|
|
|
|
_configManager = configManager;
|
|
|
|
|
|
}
|
2025-12-27 07:05:07 +08:00
|
|
|
|
|
2025-12-27 14:16:50 +08:00
|
|
|
|
protected override void PerformAction(long deviceId, SmartFrame frame, FrameDecision decision)
|
2025-12-27 07:05:07 +08:00
|
|
|
|
{
|
2025-12-27 14:16:50 +08:00
|
|
|
|
// 1. 获取实时配置 (极快,内存读取)
|
|
|
|
|
|
var options = _configManager.GetOptions(deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 原始尺寸
|
|
|
|
|
|
int srcW = frame.InternalMat.Width;
|
|
|
|
|
|
int srcH = frame.InternalMat.Height;
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 目标尺寸
|
|
|
|
|
|
int targetW = options.TargetWidth;
|
|
|
|
|
|
int targetH = options.TargetHeight;
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 判断是否需要缩放
|
|
|
|
|
|
bool needResize = false;
|
2025-12-27 07:05:07 +08:00
|
|
|
|
|
2025-12-27 14:16:50 +08:00
|
|
|
|
// 场景 A: 原始图比目标大,且允许缩小
|
|
|
|
|
|
if (options.EnableShrink && (srcW > targetW || srcH > targetH))
|
|
|
|
|
|
{
|
|
|
|
|
|
needResize = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 场景 B: 原始图比目标小,且允许放大 (通常为 False)
|
|
|
|
|
|
else if (options.EnableExpand && (srcW < targetW || srcH < targetH))
|
|
|
|
|
|
{
|
|
|
|
|
|
needResize = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 执行动作
|
|
|
|
|
|
if (needResize)
|
2025-12-27 07:05:07 +08:00
|
|
|
|
{
|
2025-12-27 07:25:32 +08:00
|
|
|
|
Mat targetMat = new Mat();
|
2025-12-27 14:16:50 +08:00
|
|
|
|
// 线性插值
|
2025-12-27 07:25:32 +08:00
|
|
|
|
Cv2.Resize(frame.InternalMat, targetMat, new Size(targetW, targetH), 0, 0, InterpolationFlags.Linear);
|
2025-12-27 07:05:07 +08:00
|
|
|
|
|
2025-12-27 14:16:50 +08:00
|
|
|
|
// 挂载
|
|
|
|
|
|
var scaleType = (srcW > targetW) ? FrameScaleType.Shrink : FrameScaleType.Expand;
|
|
|
|
|
|
frame.AttachTarget(targetMat, scaleType);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// [透传] 不需要缩放
|
|
|
|
|
|
// 此时 frame.TargetMat 为 null,代表“无变化”
|
2025-12-27 07:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-27 07:25:32 +08:00
|
|
|
|
protected override void NotifyFinished(long did, SmartFrame frame, FrameDecision dec)
|
|
|
|
|
|
{
|
|
|
|
|
|
_parent.PassToNext(did, frame, dec);
|
|
|
|
|
|
}
|
2025-12-27 07:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|