75 lines
2.3 KiB
C#
75 lines
2.3 KiB
C#
using Check.Main.Common;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Check.Main.Result
|
||
{
|
||
/// <summary>
|
||
/// 代表一个待检测产品的类,存储来自多个相机的图像
|
||
/// </summary>
|
||
public class ProductResult
|
||
{
|
||
/// <summary>
|
||
/// 产品唯一ID,可以是时间戳或触发计数
|
||
/// </summary>
|
||
public long ProductID { get; }
|
||
|
||
/// <summary>
|
||
/// 存储每个相机名称和其拍摄到的图像
|
||
/// </summary>
|
||
public Dictionary<string, Bitmap> CapturedImages { get; }
|
||
|
||
public ProductResult(long productID)
|
||
{
|
||
ProductID = productID;
|
||
CapturedImages = new Dictionary<string, Bitmap>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加一张某个相机拍摄的图像
|
||
/// </summary>
|
||
/// <param name="cameraName">相机名称</param>
|
||
/// <param name="image">拍摄的图像</param>
|
||
public void AddImage(string cameraName, Bitmap image)
|
||
{
|
||
if (!CapturedImages.ContainsKey(cameraName))
|
||
{
|
||
CapturedImages.Add(cameraName, image);
|
||
}
|
||
else
|
||
{
|
||
// 如果这个键已经存在,说明发生了逻辑错误。
|
||
// 我们不应该持有这个新的 image 对象,必须释放它以防泄漏。
|
||
ThreadSafeLogger.Log($"[警告] 相机 {cameraName} 为产品 #{this.ProductID} 发送了重复的图像。多余的图像将被丢弃。");
|
||
image?.Dispose();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查是否所有预期的相机都已完成拍摄
|
||
/// </summary>
|
||
/// <param name="expectedCameraCount">预期的相机数量</param>
|
||
/// <returns></returns>
|
||
public bool IsComplete(int expectedCameraCount)
|
||
{
|
||
return CapturedImages.Count == expectedCameraCount;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 释放所有图像资源,防止内存泄漏
|
||
/// </summary>
|
||
public void Dispose()
|
||
{
|
||
foreach (var image in CapturedImages.Values)
|
||
{
|
||
image?.Dispose();
|
||
}
|
||
CapturedImages.Clear();
|
||
}
|
||
}
|
||
}
|