添加契约和网络传输类库

This commit is contained in:
2025-12-29 08:09:14 +08:00
parent 231247c80f
commit 8cd36f44ac
14 changed files with 748 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,82 @@
using System;
using Newtonsoft.Json;
namespace SHH.Contracts
{
/// <summary>
/// 视频数据传输契约(增强版)
/// </summary>
public class VideoPayload
{
// ==========================================
// 1. 基础元数据 (将被序列化到 JSON)
// ==========================================
public string CameraId { get; set; } // 摄像头唯一标记
// 时间信息 (建议使用 DateTime调试看日志更直观)
public DateTime CaptureTime { get; set; } // 采集时间 (SDK产生图的时间)
public DateTime DispatchTime { get; set; } // 分发时间 (Server发出图的时间)
// ==========================================
// 2. 图像规格信息
// ==========================================
public int OriginalWidth { get; set; } // 原始宽度
public int OriginalHeight { get; set; } // 原始高度
public int TargetWidth { get; set; } // 目标/处理后宽度
public int TargetHeight { get; set; } // 目标/处理后高度
// ==========================================
// 3. 核心二进制数据 (严禁序列化到 JSON)
// ==========================================
/// <summary>
/// 原始图像数据 (例如海康SDK出来的原始 JPG)
/// JsonIgnore 防止误操作导致序列化性能崩塌
/// </summary>
[JsonIgnore]
public byte[] OriginalImageBytes { get; set; }
/// <summary>
/// 处理后的目标图像 (例如 Yolo 画框后的图,或者缩放后的图)
/// 可为空
/// </summary>
[JsonIgnore]
public byte[] TargetImageBytes { get; set; }
// ==========================================
// 4. 辅助方法
// ==========================================
/// <summary>
/// 仅获取元数据的 JSON 字符串
/// </summary>
public string GetMetadataJson()
{
// 创建一个纯净的匿名对象用于序列化
var meta = new
{
CameraId,
CaptureTime,
DispatchTime,
OriginalWidth,
OriginalHeight,
TargetWidth,
TargetHeight,
// 标记一下是否有目标图方便接收端判断要不要读第3帧
HasTargetImage = (TargetImageBytes != null && TargetImageBytes.Length > 0)
};
return JsonConvert.SerializeObject(meta);
}
/// <summary>
/// 从 JSON 还原元数据 (还原出来的对象 ImageBytes 默认为空,需后续填充)
/// </summary>
public static VideoPayload FromMetadataJson(string json)
{
return JsonConvert.DeserializeObject<VideoPayload>(json);
}
}
}