方法1:直接调用halconnet.dll。然后在C#内直接调用函数;
优点:只在C#里面操作,
缺点:调试不直观
方法2:halcon用HDevelop写函数,然后导出C#代码,C#调用导出的方法,更方便;
缺点:需要halcon更新后再导出。容易造成更新不同步。
优点:调试直观方便
方法3:HDevEngine
转载:https://www.51halcon.com/thread-203-1-1.html
需要引用
hdevenginedotnet.dll
halcondotnet.dll
halcon除了可以导出常见的C#文件外,也可以不用导出,通过HDevEngine直接在c#下调用脚本。
//**********
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using HalconDotNet;
namespace ExecExtProc
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ExecExtProcForm : System.Windows.Forms.Form
{
private HalconDotNet.HWindowControl WindowControl;
internal System.Windows.Forms.Button LoadBtn;
internal System.Windows.Forms.Button ExecuteBtn;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
// HDevEngine
// instance of the engine
private HDevEngine MyEngine = new HDevEngine();
// implementation of the display operators
// private HDevOpMultiWindowImpl MyHDevOperatorImpl;
// procedure call
private HDevProcedureCall ProcCall;
// HALCON window
private HWindow Window;
public ExecExtProcForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.WindowControl = new HalconDotNet.HWindowControl();
this.LoadBtn = new System.Windows.Forms.Button();
this.ExecuteBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// WindowControl
//
this.WindowControl.BackColor = System.Drawing.Color.Black;
this.WindowControl.BorderColor = System.Drawing.Color.Black;
this.WindowControl.ImagePart = new System.Drawing.Rectangle(0, 0, 768, 576);
this.WindowControl.Location = new System.Drawing.Point(19, 9);
this.WindowControl.Name = "WindowControl";
this.WindowControl.Size = new System.Drawing.Size(461, 310);
this.WindowControl.TabIndex = 1;
this.WindowControl.WindowSize = new System.Drawing.Size(461, 310);
this.WindowControl.HInitWindow += new HalconDotNet.HInitWindowEventHandler(this.WindowControl_HInitWindow);
//
// LoadBtn
//
this.LoadBtn.Location = new System.Drawing.Point(509, 9);
this.LoadBtn.Name = "LoadBtn";
this.LoadBtn.Size = new System.Drawing.Size(144, 43);
this.LoadBtn.TabIndex = 3;
this.LoadBtn.Text = "Load Procedure";
this.LoadBtn.Click += new System.EventHandler(this.LoadBtn_Click);
//
// ExecuteBtn
//
this.ExecuteBtn.Location = new System.Drawing.Point(509, 69);
this.ExecuteBtn.Name = "ExecuteBtn";
this.ExecuteBtn.Size = new System.Drawing.Size(144, 43);
this.ExecuteBtn.TabIndex = 6;
this.ExecuteBtn.Text = "Execute Procedure";
this.ExecuteBtn.Click += new System.EventHandler(this.ExecuteBtn_Click);
//
// ExecExtProcForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(680, 329);
this.Controls.Add(this.ExecuteBtn);
this.Controls.Add(this.LoadBtn);
this.Controls.Add(this.WindowControl);
this.Name = "ExecExtProcForm";
this.Text = "Execute External HDevelop Procedures via HDevEngine";
this.Load += new System.EventHandler(this.ExecExtProcForm_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ExecExtProcForm());
}
private void ExecExtProcForm_Load(object sender, System.EventArgs e)
{
// path of external procedures
string halconExamples = HSystem.GetSystem("example_dir");
string ProcedurePath = halconExamples + @"\hdevengine\procedures";
if (!HalconAPI.isWindows)
{
// Unix-based systems (Mono)
ProcedurePath = ProcedurePath.Replace('\\', '/');
}
MyEngine.SetProcedurePath(ProcedurePath);
// disable Execute button
ExecuteBtn.Enabled = false;
}
private void WindowControl_HInitWindow(object sender, System.EventArgs e)
{
Window = WindowControl.HalconWindow;
// initialize display
Window.SetDraw("margin");
Window.SetLineWidth(4);
// handler for display operators
// to use handler, uncomment the following lines
// MyHDevOperatorImpl = new HDevOpMultiWindowImpl(Window);
// MyEngine.SetHDevOperators(MyHDevOperatorImpl);
}
private void LoadBtn_Click(object sender, System.EventArgs e)
{
try
{
HDevProcedure Procedure = new HDevProcedure("detect_fin");
ProcCall = new HDevProcedureCall(Procedure);
}
catch (HDevEngineException Ex)
{
MessageBox.Show(Ex.Message, "HDevEngine Exception");
return;
}
// enable Execute button
LoadBtn.Enabled = false;
ExecuteBtn.Enabled = true;
}
private void ExecuteBtn_Click(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
HFramegrabber Framegrabber = new HFramegrabber();
// read images and process them
try
{
Framegrabber.OpenFramegrabber("File", 1, 1, 0, 0, 0, 0, "default",
-1, "default", -1, "default", "fin.seq", "default", -1, -1);
HImage Image = new HImage();
HRegion FinRegion;
HTuple FinArea;
for (int i=0; i<=2; i++)
{
Image.GrabImage(Framegrabber);
Image.DispObj(Window);
// execute procedure
ProcCall.SetInputIconicParamObject("Image", Image);
ProcCall.Execute();
// get output parameters from procedure call
FinRegion = ProcCall.GetOutputIconicParamRegion("FinRegion");
FinArea = ProcCall.GetOutputCtrlParamTuple("FinArea");
// display results
Image.DispObj(Window);
Window.SetColor("red");
Window.DispObj(FinRegion);
Window.SetColor("white");
Window.SetTposition(150, 20);
Window.WriteString("FinArea: " + FinArea.D);
HSystem.WaitSeconds(2);
FinRegion.Dispose();
Image.Dispose();
}
}
catch (HOperatorException Ex)
{
MessageBox.Show(Ex.Message, "HALCON Exception");
}
catch (HDevEngineException Ex)
{
MessageBox.Show(Ex.Message, "HDevEngine Exception");
}
Framegrabber.Dispose();
this.Cursor = Cursors.Default;
}
}
}
//**********