using OpenCvSharp; using System.IO; namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Access { /// /// 获取From文件Handler /// public class ImageFromFileHandler : IVisionFlowHandler { /// /// 类型 /// public VisionFlowType Type => VisionFlowType.从文件获取图片; /// /// 执行 /// /// 视觉功能 /// 变量项 /// 返回布尔值 public bool Execute(VisionFunction func, VisionFlow item) { if (item.StringParams.TryGetValue("ImageBase64", out var embedded)) { func.Src = Cv2.ImDecode(Convert.FromBase64String(embedded), ImreadModes.Color); return !func.Src.Empty(); } if (item.StringParams.TryGetValue("Path", out string filePath)) { if (File.Exists(filePath)) { func.Src = Cv2.ImRead(filePath); return true; } else { throw new Exception($"文件路径不存在: {filePath}"); } } return false; } } }