编写作业如图所示,本文采用的是读取本地二维码,由于没有相机所以只能够选择读取本地二维码。
当然需要利用Cog2DSymbolTool1工具对二维码进行训练。
C#脚本代码中添加变量:
private Cog2DSymbolTool Cog2DSymbolToolObject;
private string DecodedString;
C#脚本构造函数GroupRun中添加如下代码:
Cog2DSymbolToolObject = (Cog2DSymbolTool) toolGroup.Tools["Cog2DSymbolTool1"];
DecodedString = Cog2DSymbolToolObject.Result.DecodedString;
MessageBox.Show("二维码解码信息:" + DecodedString); //用于弹出对话框
C#脚本构造函数ModifyLastRunRecord添加代码如下:
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
CogGraphicLabel ResultLabel = new CogGraphicLabel();
ResultLabel.SetXYText(300 / 2, 300 / 2, DecodedString);
ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
toolGroup.AddGraphicToRunRecord(ResultLabel,lastRecord,"Image Source.OutputImage","script");
}
完成代码如下:
using System;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ToolGroup;
using Cognex.VisionPro.TwoDSymbol;
public class UserScript : CogToolGroupBaseScript
{
private Cog2DSymbolTool Cog2DSymbolToolObject;
private string DecodedString;
// The GroupRun function is called when the tool group is run. The default
// implementation provided here is equivalent to the normal behavior of the
// tool group. Modifying this function will allow you to change the behavior
// when the tool group is run.
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// To let the execution stop in this script when a debugger is attached, uncomment the following lines.
// #if DEBUG
// if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
// #endif
// Run each tool in the tool group using the RunTool function
for (Int32 toolIdx = 0; toolIdx < toolGroup.Tools.Count; toolIdx++)
toolGroup.RunTool(toolGroup.Tools[toolIdx], ref message, ref result);
Cog2DSymbolToolObject = (Cog2DSymbolTool) toolGroup.Tools["Cog2DSymbolTool1"];
DecodedString = Cog2DSymbolToolObject.Result.DecodedString;
MessageBox.Show("二维码解码信息:" + DecodedString);
// Returning False indicates we ran the tools in script, and they should not be
// run by VisionPro
return false;
}
#region "When the Current Run Record is Created"
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region "When the Last Run Record is Created"
// Allows you to add or modify the contents of the last run record when it is
// created. For example, you might add custom graphics to the run record here.
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
CogGraphicLabel ResultLabel = new CogGraphicLabel();
ResultLabel.SetXYText(300 / 2, 300 / 2, DecodedString);
ResultLabel.Color = Cognex.VisionPro.CogColorConstants.Blue;
toolGroup.AddGraphicToRunRecord(ResultLabel,lastRecord,"Image Source.OutputImage","script");
}
#endregion
#region "When the Script is Initialized"
// Perform any initialization required by your script here
public override void Initialize(CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
}
#endregion
}
参考: