C#(VS)调用HALCON12

时间:2024-03-19 08:36:27

1、使用HDevelop编写代码:

	
dev_close_window ()
read_image (Image, 'E:/图库/abc.jpg')

rgb1_to_gray(Image,GrayImage)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)

C#(VS2010)调用HALCON12

2、导出HALCON代码

选择HDevelop菜单栏中的“文件---->导出”,导出类型设为:C#-HALCON/.NET;选择导出到F盘中导出的文件为:sa.cs。

C#(VS2010)调用HALCON12

其导出的sa.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, ho_GrayImage;


    // Local control variables 


    HTuple hv_Width = null, hv_Height = null;
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    HOperatorSet.GenEmptyObj(out ho_GrayImage);


    //dev_close_window(...);
    ho_Image.Dispose();
    HOperatorSet.ReadImage(out ho_Image, "E:/图库/abc.jpg");
    ho_GrayImage.Dispose();
    HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
    HOperatorSet.GetImageSize(ho_GrayImage, out hv_Width, out hv_Height);
    //dev_open_window(...);
    HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
    ho_Image.Dispose();
    ho_GrayImage.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、c#调用HALCON导出的程序

(1)新建一个C#应用窗体程序

(2)添加halcondotnet.dll文件到引用中,并将halconwindow添加至工具箱;
引用-添加引用-(找到halcon中的halcondotnet.dll进行添加)

其目录为:D:\Program Files\MVTec\HALCON-12.0\bin\dotnet35

C#(VS2010)调用HALCON12

(3)在工具箱中添加halconWindow控件

打开工具箱,在空白处右键-选择项-浏览,选择下边文件
C#(VS2010)调用HALCON12C#(VS2010)调用HALCON12
(4)在Form1窗口添加一个button和halconWindow控件(工具箱中找halcon图标).
C#(VS2010)调用HALCON12
(5)在Form1.cs中代码如下:
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; 	//引入Halcon空间

namespace HALCON2
{
    /*以下复制于sa.cs*/
    public partial class HDevelopExport
    {
        public HTuple hv_ExpDefaultWinHandle;

        // Main procedure 
        private void action()
        {



            // Local iconic variables 

            HObject ho_Image, ho_GrayImage;

            // Local control variables 

            HTuple hv_Width = null, hv_Height = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);

            //dev_close_window(...);
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, "E:/图库/abc.jpg");
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
            HOperatorSet.GetImageSize(ho_GrayImage, out hv_Width, out hv_Height);
            //dev_open_window(...);
            HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
            ho_Image.Dispose();
            ho_GrayImage.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();
        }

    }
    /*以上复制于sa.cs*/

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HDevelopExport HD = new HDevelopExport();
            HD.RunHalcon(hWindowControl1.HalconWindow);
        }
    }
}
运行结果:


另一种方法:

(1)将sa.cs导入C#工程:

C#(VS2010)调用HALCON12

(2)Form1.cs的代码修改为:

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; 	//引入Halcon空间
namespace HALCON2
{
   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HDevelopExport HD = new HDevelopExport();
            HD.RunHalcon(hWindowControl1.HalconWindow);
        }
    }
}
其运行结果为: