第一步:做好halcon图像采集算法并将其导出为C#代码。
打开VS2019选择Windows窗体应用程序并设置命名工程名称与保存路径。如下图:
可以看到一个类似于白板的窗体(适当拖动窗体大小),接着添加应用,如下如
单击引用在右击添加引用,引用的路径为halcon安装目录下的bin->dotnet35下的halcondotnet.dll(动态链接库),若不知道在哪里,可回到桌面右击halcon图标查看文件所在位置,然后回到VS215单击浏览按钮做如下图操作:
做好添加引用,点击工具箱去添加显示halcon窗体的控件,如下图:
常规选项在工具箱的最后面,右击常规单击选择项,弹出如下图的窗口:
单击浏览选择上一步路径下的halcondotnet.dll(动态链接库),确定后若出现如下图的内容说明添加成功,然后点击确认即可。
现在常规下面出现以下两个选项,如下图:
这里单击第二个且按住放到白板上,如下图:
这样关于联合halcon的界面内容就做完了,接下来就是布置界面,如下图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using HalconDotNet;
namespace DefectDetection.View
{
public partial class Form1 : RibbonForm
{
public Form1()
{
InitializeComponent();
InitSkinGallery();
InitGrid();
}
void InitSkinGallery()
{
SkinHelper.InitSkinGallery(rgbiSkins, true);
}
BindingList gridDataList = new BindingList();
void InitGrid()
{
gridDataList.Add(new Person("John", "Smith"));
gridDataList.Add(new Person("Gabriel", "Smith"));
gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
//gridControl.DataSource = gridDataList;
}
//定义变量
HObject ho_Image = null;
// Local control variables
HTuple hv_AcqHandle = new HTuple(), hv_WindowHandle = new HTuple();
// Initialize local and output iconic variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
private void openCameraBtn_Click(object sender, EventArgs e)
{
//打开相机
try
{
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "gray",
-1, "false", "default", "[1] Microsoft? LifeCam HD-3000", 0, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.CloseWindow(HDevWindowStack.Pop());
}
HOperatorSet.SetWindowAttr("background_color", "black");
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
this.openCameraBtn.Enabled = false;
this.collectionBtn.Enabled = true;
this.closeCameraBtn.Enabled = false;
}
catch { }
}
private void collectionBtn_Click(object sender, EventArgs e)
{
this.openCameraBtn.Enabled = false;
this.collectionBtn.Enabled = false;
this.closeCameraBtn.Enabled = true;
timer1.Enabled = true;
}
private void closeCameraBtn_Click(object sender, EventArgs e)
{
//关闭相机
this.openCameraBtn.Enabled = true;
this.collectionBtn.Enabled = false;
this.closeCameraBtn.Enabled = false;
HOperatorSet.CloseFramegrabber(hv_AcqHandle); //关闭相机
timer1.Enabled = false; //停止定时器
}
private void timer1_Tick(object sender, EventArgs e)
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
// hv_Width.Dispose(); hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.openCameraBtn.Enabled = true;
this.collectionBtn.Enabled = false;
this.closeCameraBtn.Enabled = false;
}
}
}