From 08d304e7544deea0fbf9f243baddf2981d92ade8 Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Thu, 16 Jul 2026 12:11:11 +0800
Subject: [PATCH] =?UTF-8?q?=E6=97=8B=E8=BD=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../VVMs/Vision/VisionConfigViewModel.cs | 7 +++
.../Processing/ImageRotate.xaml | 36 +++++++++++++
.../Processing/ImageRotate.xaml.cs | 15 ++++++
.../Processing/ImageRotateHandler.cs | 54 +++++++++++++++++++
PLCSharpWpf/VVMs/Vision/VisionFunction.cs | 1 +
PLCSharpWpf/VVMs/Vision/VisionType.cs | 2 +
PLCSharpWpf/VVMs/Vision/VisionsModel.cs | 1 +
7 files changed, 116 insertions(+)
create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml
create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml.cs
create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotateHandler.cs
diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs
index 87a0cb7..0fbd78c 100644
--- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs
+++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs
@@ -349,6 +349,10 @@ namespace PLCSharp.VVMs.Vision
case VisionFlowType.图像翻转:
newFlow.IntParams["FlipDirection"] = 0;
break;
+ case VisionFlowType.图像旋转:
+ newFlow.DoubleParams["RotateAngle"] = 0;
+ newFlow.IntParams["ResizeMode"] = 0;
+ break;
case VisionFlowType.ROI解码:
newFlow.IntParams["DecodeType"] = 0;
newFlow.BoolParams["EnableMirror"] = false;
@@ -586,6 +590,9 @@ namespace PLCSharp.VVMs.Vision
case VisionFlowType.图像翻转:
ContentRegion = new ImageFlip();
break;
+ case VisionFlowType.图像旋转:
+ ContentRegion = new ImageRotate();
+ break;
case VisionFlowType.ROI解码:
ContentRegion = new BarcodeDecode();
break;
diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml
new file mode 100644
index 0000000..7542205
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml
@@ -0,0 +1,36 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml.cs
new file mode 100644
index 0000000..c579856
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotate.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows.Controls;
+
+namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
+{
+ ///
+ /// ImageRotate.xaml 的交互逻辑
+ ///
+ public partial class ImageRotate : UserControl
+ {
+ public ImageRotate()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotateHandler.cs b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotateHandler.cs
new file mode 100644
index 0000000..3906b47
--- /dev/null
+++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/ImageRotateHandler.cs
@@ -0,0 +1,54 @@
+using OpenCvSharp;
+
+namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
+{
+ ///
+ /// 图像旋转Handler — 按指定角度旋转图像,可选择保持原尺寸或扩展画布
+ ///
+ public class ImageRotateHandler : IVisionFlowHandler
+ {
+ public VisionFlowType Type => VisionFlowType.图像旋转;
+
+ public bool Execute(VisionFunction func, VisionFlow item)
+ {
+ var src = func.Src ?? throw new Exception("请先获取图片!");
+
+ // 读取旋转角度
+ double angle = item.DoubleParams.TryGetValue("RotateAngle", out double a) ? a : 0;
+
+ // 读取尺寸模式:0=保持原尺寸,1=扩展画布(完整显示)
+ int resizeMode = item.IntParams.TryGetValue("ResizeMode", out int rm) ? rm : 0;
+
+ int w = src.Width, h = src.Height;
+ var center = new Point2f(w / 2f, h / 2f);
+
+ var rot = Cv2.GetRotationMatrix2D(center, angle, 1.0);
+
+ Size dstSize;
+ if (resizeMode == 1)
+ {
+ // 计算旋转后最小外接矩形尺寸
+ double rad = angle * Math.PI / 180.0;
+ double cos = Math.Abs(Math.Cos(rad));
+ double sin = Math.Abs(Math.Sin(rad));
+ int newW = (int)(w * cos + h * sin);
+ int newH = (int)(w * sin + h * cos);
+ dstSize = new Size(newW, newH);
+
+ // 调整平移量使图像居中
+ rot.Set(0, 2, rot.Get(0, 2) + (newW - w) / 2.0);
+ rot.Set(1, 2, rot.Get(1, 2) + (newH - h) / 2.0);
+ }
+ else
+ {
+ dstSize = new Size(w, h);
+ }
+
+ using Mat dst = new Mat();
+ Cv2.WarpAffine(src, dst, rot, dstSize);
+
+ dst.CopyTo(src);
+ return true;
+ }
+ }
+}
diff --git a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs
index d49cea9..b6d2c6e 100644
--- a/PLCSharpWpf/VVMs/Vision/VisionFunction.cs
+++ b/PLCSharpWpf/VVMs/Vision/VisionFunction.cs
@@ -338,6 +338,7 @@ namespace PLCSharp.VVMs.Vision
[VisionFlowType.颜色面积] = new ColorAreaHandler(),
[VisionFlowType.灰度面积] = new GrayAreaHandler(),
[VisionFlowType.图像翻转] = new ImageFlipHandler(),
+ [VisionFlowType.图像旋转] = new ImageRotateHandler(),
};
[NotMapped]
diff --git a/PLCSharpWpf/VVMs/Vision/VisionType.cs b/PLCSharpWpf/VVMs/Vision/VisionType.cs
index da93ae3..a4f0f45 100644
--- a/PLCSharpWpf/VVMs/Vision/VisionType.cs
+++ b/PLCSharpWpf/VVMs/Vision/VisionType.cs
@@ -48,6 +48,8 @@
阈值 = 17,
[FlowCategory("图像处理")]
图像翻转 = 19,
+ [FlowCategory("图像处理")]
+ 图像旋转 = 20,
[FlowCategory("图像处理")]
坐标转换 = 18,
diff --git a/PLCSharpWpf/VVMs/Vision/VisionsModel.cs b/PLCSharpWpf/VVMs/Vision/VisionsModel.cs
index 4cfdb8d..a62aa12 100644
--- a/PLCSharpWpf/VVMs/Vision/VisionsModel.cs
+++ b/PLCSharpWpf/VVMs/Vision/VisionsModel.cs
@@ -154,6 +154,7 @@ namespace PLCSharp.VVMs.Vision
{
VisionsModel = this,
ImageDatas = ImageDatas,
+ GlobalModel = GlobalModel,
};
VisionFunctions.Add(newFunction);