编写作业如图所示,本文采用的是读取本地二维码,由于没有相机所以只能够选择读取本地二维码。
当然需要利用Cog2DSymbolTool1工具对二维码进行训练。
C#脚本代码中添加变量:
1 private Cog2DSymbolTool Cog2DSymbolToolObject;
2 private string DecodedString;
C#脚本构造函数GroupRun中添加如下代码:
1 Cog2DSymbolToolObject = (Cog2DSymbolTool) toolGroup.Tools["Cog2DSymbolTool1"];
2 DecodedString = Cog2DSymbolToolObject.Result.DecodedString;
3
4 MessageBox.Show("二维码解码信息:" + DecodedString); //用于弹出对话框
C#脚本构造函数ModifyLastRunRecord添加代码如下:
1 public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
2 {
3 CogGraphicLabel ResultLabel = new CogGraphicLabel();
4 ResultLabel.SetXYText(300 / 2, 300 / 2, DecodedString);
5 ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
6 toolGroup.AddGraphicToRunRecord(ResultLabel,lastRecord,"Image Source.OutputImage","script");
7 }
完成代码如下:
1 using System;
2 using System.Windows.Forms;
3 using Cognex.VisionPro;
4 using Cognex.VisionPro3D;
5 using Cognex.VisionPro.ToolGroup;
6 using Cognex.VisionPro.TwoDSymbol;
7
8 public class UserScript : CogToolGroupBaseScript
9 {
10 private Cog2DSymbolTool Cog2DSymbolToolObject;
11 private string DecodedString;
12
13 // The GroupRun function is called when the tool group is run. The default
14 // implementation provided here is equivalent to the normal behavior of the
15 // tool group. Modifying this function will allow you to change the behavior
16 // when the tool group is run.
17 public override bool GroupRun(ref string message, ref CogToolResultConstants result)
18 {
19 // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
20 // #if DEBUG
21 // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
22 // #endif
23
24 // Run each tool in the tool group using the RunTool function
25 for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++)
26 toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result);
27
28 Cog2DSymbolToolObject = (Cog2DSymbolTool) toolGroup.Tools["Cog2DSymbolTool1"];
29 DecodedString = Cog2DSymbolToolObject.Result.DecodedString;
30
31 MessageBox.Show("二维码解码信息:" + DecodedString);
32
33 // Returning False indicates we ran the tools in script, and they should not be
34 // run by VisionPro
35 return false;
36 }
37
38 #region "When the Current Run Record is Created"
39 public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
40 {
41 }
42 #endregion
43
44 #region "When the Last Run Record is Created"
45 // Allows you to add or modify the contents of the last run record when it is
46 // created. For example, you might add custom graphics to the run record here.
47 public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
48 {
49 CogGraphicLabel ResultLabel = new CogGraphicLabel();
50 ResultLabel.SetXYText(300 / 2, 300 / 2, DecodedString);
51 ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
52 toolGroup.AddGraphicToRunRecord(ResultLabel,lastRecord,"Image Source.OutputImage","script");
53 }
54 #endregion
55
56 #region "When the Script is Initialized"
57 // Perform any initialization required by your script here
58 public override void Initialize(CogToolGroup host)
59 {
60 // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
61 base.Initialize(host);
62 }
63 #endregion
64
65 }
分类:
VisionPro
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)