61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using OpenCvSharp;
|
|
using System.IO;
|
|
|
|
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Access
|
|
{
|
|
/// <summary>
|
|
/// 保存图像To文件Handler
|
|
/// </summary>
|
|
public class ImageToFileHandler : IVisionFlowHandler
|
|
{
|
|
/// <summary>
|
|
/// 类型
|
|
/// </summary>
|
|
public VisionFlowType Type => VisionFlowType.存到文件;
|
|
|
|
/// <summary>
|
|
/// 执行
|
|
/// </summary>
|
|
/// <param name="func">视觉功能</param>
|
|
/// <param name="item">变量项</param>
|
|
/// <returns>返回布尔值</returns>
|
|
public bool Execute(VisionFunction func, VisionFlow item)
|
|
{
|
|
if (item.StringParams.TryGetValue("Path", out string filePath))
|
|
{
|
|
try
|
|
{
|
|
if (item.BoolParams.TryGetValue("DateTimePath", out bool useDateTime))
|
|
{
|
|
if (useDateTime)
|
|
{
|
|
filePath += DateTime.Now.ToString("yyyy_MM_dd");
|
|
filePath += "\\";
|
|
}
|
|
}
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
var fileName = filePath + DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss_ff") + ".png";
|
|
if (Cv2.ImWrite(fileName, func.Src))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"保存图像失败");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"保存图像失败: {ex.Message}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("未配置文件路径,请重新配置!");
|
|
}
|
|
}
|
|
}
|
|
} |