1.首先配置好VS2010:
添加目錄“HALCON-12.0\bin\dotnet35\halcondotnet.dll”
添加后工具欄中多出HWindowControl工具:
2.寫好Halcon程序並導出:
導出:
其中halcon_test.cs代碼如下:
// // File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0 // // This file is intended to be used with the HDevelopTemplate or // HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c# using System; using HalconDotNet; public partial class HDevelopExport { public HTuple hv_ExpDefaultWinHandle; // Main procedure private void action() { // Local iconic variables HObject ho_Image; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); //dev_close_window(...); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "C:/Users/Administrator/Desktop/車牌.jpg"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //dev_open_window(...); HOperatorSet.DispImage(ho_Image, hv_ExpDefaultWinHandle); ho_Image.Dispose(); } public void InitHalcon() { // Default settings used in HDevelop HOperatorSet.SetSystem("width", 512); HOperatorSet.SetSystem("height", 512); } public void RunHalcon(HTuple Window) { hv_ExpDefaultWinHandle = Window; action(); } }
3.在VS項目中:
3.1把導出的cs文件復制到program.cs文件中:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using HalconDotNet; //引用HalconDotNet namespace demo3 { public partial class HDevelopExport { public HTuple hv_ExpDefaultWinHandle; // Main procedure private void action() { // Local iconic variables HObject ho_Image; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); //dev_close_window(...); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "C:/Users/Administrator/Desktop/車牌.jpg"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //dev_open_window(...); HOperatorSet.DispImage(ho_Image, hv_ExpDefaultWinHandle); ho_Image.Dispose(); } public void InitHalcon() { // Default settings used in HDevelop HOperatorSet.SetSystem("width", 512); HOperatorSet.SetSystem("height", 512); } public void RunHalcon(HTuple Window) { hv_ExpDefaultWinHandle = Window; action(); } } static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
3.2在Form1.cs[設計]中畫好HWindowControl和按鈕:
雙擊"start"按鈕並編寫相應的程序(不要忘記在頭部添加 using HalconDotNet;):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using HalconDotNet; namespace demo3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { HDevelopExport HD = new HDevelopExport(); HD.RunHalcon(hWindowControl1.HalconWindow); } } }
4.運行發現出錯:
在Program.cs文件中:
4.1添加“HALCON-12.0\bin\x64-win64”下的halcon.dll到項目Debug文件夾下再次運行,發現出現如下錯:
4.2 更新 “生成配置”,將目標平台改成:“Any CPU”並運行:
5.大功告成!