Files
CheckDevice/Check.Main/Result/ProductResult.cs
17860779768 2e46747ba9 视觉修改
2025-08-25 16:33:58 +08:00

75 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}