VisionPro 编写二维码识别Quickbuild工程

时间:2024-03-03 18:16:30

编写作业如图所示,本文采用的是读取本地二维码,由于没有相机所以只能够选择读取本地二维码。

当然需要利用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 }