using OpenCvSharp; using Prism.Mvvm; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; namespace PLCSharp.VVMs.Vision { /// /// 图像数据 /// public class ImageData : BindableBase { /// /// 唯一标识 /// [Key] public Guid ID { get; set; } = Guid.NewGuid(); /// /// 配方标识 /// public Guid RecipeID { get; set; } private string _Name; /// /// 名称 /// public string Name { get { return _Name; } set { SetProperty(ref _Name, value); } } private string _Comment; /// /// 备注 /// public string Comment { get { return _Comment; } set { SetProperty(ref _Comment, value); } } private string _Prompt; /// /// 提示 /// [NotMapped] public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } private Mat _Mat; /// /// 图像矩阵 /// [NotMapped] [JsonIgnore] public Mat Mat { get { return _Mat; } set { SetProperty(ref _Mat, value); } } /// /// 序列化后的图像数据 /// [Column("Mat")] public byte[] SerializedMat { get { byte[] imageBytes = []; if (_Mat != null && !_Mat.Empty()) { Cv2.ImEncode(".png", _Mat, out imageBytes); } return imageBytes; }// 序列化 set { try { Mat = value != null ? Cv2.ImDecode(value, ImreadModes.Color) : new(); // 自动反序列化 } catch (global::System.Exception) { Mat = new(); } } } } public class LocalImageData : BindableBase { /// /// 唯一标识 /// [Key] public Guid ID { get; set; } = Guid.NewGuid(); /// /// 功能标识 /// public Guid FuncID { get; set; } private string _Name; /// /// 名称 /// public string Name { get { return _Name; } set { SetProperty(ref _Name, value); } } private string _Comment; /// /// 备注 /// public string Comment { get { return _Comment; } set { SetProperty(ref _Comment, value); } } private string _Prompt; /// /// 提示 /// [NotMapped] public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } private Mat _Mat; /// /// 图像矩阵 /// [NotMapped] [JsonIgnore] public Mat Mat { get { return _Mat; } set { SetProperty(ref _Mat, value); } } /// /// 序列化后的图像数据 /// [Column("Mat")] public byte[] SerializedMat { get { byte[] imageBytes = []; if (_Mat != null && !_Mat.Empty()) { Cv2.ImEncode(".png", _Mat, out imageBytes); } return imageBytes; }// 序列化 set { try { Mat = value != null ? Cv2.ImDecode(value, ImreadModes.Color) : new(); // 自动反序列化 } catch (global::System.Exception) { Mat = new(); } } } } }