增加了图像缩放的支持
This commit is contained in:
@@ -31,6 +31,26 @@ public class SmartFrame : IDisposable
|
||||
|
||||
#endregion
|
||||
|
||||
#region --- 衍生数据属性 (Derivative Properties) ---
|
||||
|
||||
/// <summary>
|
||||
/// [衍生] 目标图像副本(处理后的图像)
|
||||
/// <para>用途:存储经过缩放、增强后的图像,供 UI 预览或 Web 推流直接使用</para>
|
||||
/// <para>生命周期:完全跟随 SmartFrame,主帧销毁时自动释放</para>
|
||||
/// </summary>
|
||||
public Mat? TargetMat { get; private set; }
|
||||
|
||||
/// <summary> 变换类型(标记该 TargetMat 是被缩小了还是放大了) </summary>
|
||||
public FrameScaleType ScaleType { get; private set; } = FrameScaleType.None;
|
||||
|
||||
/// <summary> [快捷属性] 目标图像宽度 (若 TargetMat 为空则返回 0) </summary>
|
||||
public int TargetWidth => TargetMat?.Width ?? 0;
|
||||
|
||||
/// <summary> [快捷属性] 目标图像高度 (若 TargetMat 为空则返回 0) </summary>
|
||||
public int TargetHeight => TargetMat?.Height ?? 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region --- 构造与激活 (Constructor & Activation) ---
|
||||
|
||||
/// <summary>
|
||||
@@ -45,6 +65,9 @@ public class SmartFrame : IDisposable
|
||||
_pool = pool;
|
||||
// 预分配物理内存:内存块在帧池生命周期内复用,避免频繁申请/释放
|
||||
InternalMat = new Mat(height, width, type);
|
||||
|
||||
// 确保激活时清理旧的衍生数据(防止脏数据残留)
|
||||
ResetDerivatives();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,6 +84,40 @@ public class SmartFrame : IDisposable
|
||||
|
||||
#endregion
|
||||
|
||||
#region --- 衍生数据操作 (Derivatives Management) ---
|
||||
|
||||
/// <summary>
|
||||
/// 挂载处理后的目标图像
|
||||
/// </summary>
|
||||
/// <param name="processedMat">已处理的 Mat(所有权移交给 SmartFrame)</param>
|
||||
/// <param name="scaleType">变换类型(缩小/放大/无)</param>
|
||||
public void AttachTarget(Mat processedMat, FrameScaleType scaleType)
|
||||
{
|
||||
// 防御性编程:如果之前已有 TargetMat,先释放,防止内存泄漏
|
||||
if (TargetMat != null)
|
||||
{
|
||||
TargetMat.Dispose();
|
||||
}
|
||||
|
||||
TargetMat = processedMat;
|
||||
ScaleType = scaleType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部清理:释放衍生数据
|
||||
/// </summary>
|
||||
private void ResetDerivatives()
|
||||
{
|
||||
if (TargetMat != null)
|
||||
{
|
||||
TargetMat.Dispose();
|
||||
TargetMat = null;
|
||||
}
|
||||
ScaleType = FrameScaleType.None;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region --- 引用计数管理 (Reference Count Management) ---
|
||||
|
||||
/// <summary>
|
||||
@@ -86,7 +143,10 @@ public class SmartFrame : IDisposable
|
||||
// 原子递减:线程安全,确保计数准确
|
||||
if (Interlocked.Decrement(ref _refCount) <= 0)
|
||||
{
|
||||
// 引用归零:所有消费者均已释放,将帧归还池复用
|
||||
// 1. 彻底清理衍生数据(TargetMat 通常是 new 出来的,必须 Dispose)
|
||||
ResetDerivatives();
|
||||
|
||||
// 2. 归还到池中复用 (InternalMat 不释放,继续保留在内存池中)
|
||||
_pool.Return(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user