From 8015c5bbfe573dede08638ade353fd7cfc78e14b Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 14 Jul 2026 14:49:20 +0800 Subject: [PATCH] barcode --- PLCSharpWpf/PLCSharp.csproj | 3 +- PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs | 30 +- .../VVMs/Vision/VisionConfigViewModel.cs | 69 ++++ PLCSharpWpf/VVMs/Vision/VisionDataType.cs | 3 +- .../Algorithm/BarcodeDecode.xaml | 120 ++++++ .../Algorithm/BarcodeDecode.xaml.cs | 18 + .../Algorithm/BarcodeDecodeHandler.cs | 389 ++++++++++++++++++ .../Algorithm/WeChatDecode.xaml | 71 ++++ .../Algorithm/WeChatDecode.xaml.cs | 18 + .../Algorithm/WeChatDecodeHandler.cs | 145 +++++++ .../Vision/VisionFlowHandler/MatExtension.cs | 3 + PLCSharpWpf/VVMs/Vision/VisionFunction.cs | 173 +++++--- PLCSharpWpf/VVMs/Vision/VisionType.cs | 5 + 13 files changed, 979 insertions(+), 68 deletions(-) create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecode.xaml create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecode.xaml.cs create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecodeHandler.cs create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecode.xaml.cs create mode 100644 PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/WeChatDecodeHandler.cs diff --git a/PLCSharpWpf/PLCSharp.csproj b/PLCSharpWpf/PLCSharp.csproj index 8e1ce88..df79201 100644 --- a/PLCSharpWpf/PLCSharp.csproj +++ b/PLCSharpWpf/PLCSharp.csproj @@ -21,7 +21,7 @@ - + @@ -29,6 +29,7 @@ + diff --git a/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs b/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs index 4b93606..e9d1271 100644 --- a/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs +++ b/PLCSharpWpf/VVMs/Vision/LocalVariableItem.cs @@ -55,9 +55,9 @@ namespace PLCSharp.VVMs.Vision case "Pos": { var r = new Pos(); - if (value is Pos line) + if (value is Pos pos) { - r = line; + r = pos; } else { @@ -97,9 +97,9 @@ namespace PLCSharp.VVMs.Vision case "Rect": { var r = new Rect(); - if (value is Rect line) + if (value is Rect rect) { - r = line; + r = rect; } else { @@ -112,6 +112,22 @@ namespace PLCSharp.VVMs.Vision case "Double": SetProperty(ref _RawValue, value); break; + case "Barcode": + { + var r = new Barcode(); + if (value is Barcode barcode) + { + r = barcode; + } + else + { + r = JsonConvert.DeserializeObject(value.ToString()); + } + + SetProperty(ref _RawValue, r); + } + break; + } @@ -181,6 +197,12 @@ namespace PLCSharp.VVMs.Vision case "Double": return ((double)val).ToString("F4"); + case "Barcode": + if (val is Barcode b) + return $"位置:(X:{b.Box.X:F4}, Y:{b.Box.Y:F4}) 内容:({b.Info})"; + else + return val?.ToString() ?? ""; + default: return val?.ToString() ?? ""; } diff --git a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs index 84561c2..22abd5d 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionConfigViewModel.cs @@ -346,6 +346,17 @@ namespace PLCSharp.VVMs.Vision newFlow.DoubleParams["ImageY"] = 0; newFlow.StringParams["TransformMat"] = ""; break; + case VisionFlowType.ROI解码: + newFlow.IntParams["DecodeType"] = 0; + newFlow.BoolParams["EnableMirror"] = false; + newFlow.BoolParams["EnableUpscale"] = true; + newFlow.BoolParams["UsePureBarcode"] = false; + newFlow.StringParams["ResultVarName"] = "条码解码_Result"; + break; + case VisionFlowType.微信解码: + newFlow.BoolParams["EnableMirror"] = false; + newFlow.StringParams["ResultVarName"] = "微信解码_Result"; + break; default: break; } @@ -551,6 +562,12 @@ namespace PLCSharp.VVMs.Vision case VisionFlowType.坐标转换: ContentRegion = new CoordTransform(); break; + case VisionFlowType.ROI解码: + ContentRegion = new BarcodeDecode(); + break; + case VisionFlowType.微信解码: + ContentRegion = new WeChatDecode(); + break; } @@ -1568,6 +1585,58 @@ namespace PLCSharp.VVMs.Vision }); } + private AsyncDelegateCommand _DrawBarcodeROI; + /// + /// 绘制条码解码 ROI 区域 + /// + public AsyncDelegateCommand DrawBarcodeROI => + _DrawBarcodeROI ??= new AsyncDelegateCommand(ExecuteDrawBarcodeROIAsync); + + private async Task ExecuteDrawBarcodeROIAsync() + { + var window = System.Windows.Application.Current.Windows.OfType() + .FirstOrDefault(w => w.IsActive); + var imageEdit = WpfTool.FindVisualChild(window); + if (imageEdit?.ImageSource == null) + { + SendInfoDialog("请先获取图片!"); + return; + } + + try + { + var roi = await imageEdit.DrawROIAsync("条码解码"); + if (roi == null) return; + + if (SelectVisionFlow == null) return; + + SelectVisionFlow.DoubleParams["ROILeft"] = roi.Left; + SelectVisionFlow.DoubleParams["ROITop"] = roi.Top; + SelectVisionFlow.DoubleParams["ROIWidth"] = roi.Width; + SelectVisionFlow.DoubleParams["ROIHeight"] = roi.Height; + + // 画矩形边框 + var rect = new System.Windows.Shapes.Rectangle + { + Width = roi.Width, + Height = roi.Height, + Stroke = Brushes.Lime, + StrokeThickness = 2, + StrokeDashArray = new System.Windows.Media.DoubleCollection { 4, 2 }, + Tag = "条码解码", + }; + Canvas.SetLeft(rect, roi.Left); + Canvas.SetTop(rect, roi.Top); + imageEdit.Draw(rect); + + SendInfoDialog("条码解码 ROI 已保存"); + } + catch (Exception ex) + { + SendInfoDialog($"ROI 绘制失败:{ex.Message}"); + } + } + /// /// 在 ImageEdit 上画矩形边框 + 中心十字 /// diff --git a/PLCSharpWpf/VVMs/Vision/VisionDataType.cs b/PLCSharpWpf/VVMs/Vision/VisionDataType.cs index 8854d6b..e7676e3 100644 --- a/PLCSharpWpf/VVMs/Vision/VisionDataType.cs +++ b/PLCSharpWpf/VVMs/Vision/VisionDataType.cs @@ -5,6 +5,7 @@ public record struct Circle(Pos Center, double Radius); public record struct Line(Pos From,Pos To); public record struct Rect(Pos Center, double Width,double Height); + public record struct Barcode(Pos Box, string Info); -} +} \ No newline at end of file diff --git a/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecode.xaml b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecode.xaml new file mode 100644 index 0000000..7ddad42 --- /dev/null +++ b/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Algorithm/BarcodeDecode.xaml @@ -0,0 +1,120 @@ + + + + + + +