halcon做好图像采集,在VS2015中的WinForm界面上显示,未来做运动控制、串口通信等都是需要halcon联合WinForm界面的,这里就先从基础的开始,话不多说,直奔主题。
第一步:做好halcon图像采集算法并将其导出为C#代码。
打开VS2015选择Windows窗体应用程序并设置命名工程名称与保存路径。如下图:
可以看到一个类似于白板的窗体(适当拖动窗体大小),接着添加应用,如下如
单击引用在右击添加引用,引用的路径为halcon安装目录下的bin->dotnet35下的halcondotnet.dll(动态链接库),若不知道在哪里,可回到桌面右击halcon图标查看文件所在位置,然后回到VS215单击浏览按钮做如下图操作:
出现下图内容就说明添加成功了。
做好添加引用,点击工具箱去添加显示halcon窗体的控件,如下图:
常规选项在工具箱的最后面,右击常规单击选择项,弹出如下图的窗口:
单击浏览选择上一步路径下的halcondotnet.dll(动态链接库),确定后若出现如下图的内容说明添加成功,然后点击确认即可。
现在常规下面出现以下两个选项,如下图:
这里单击第二个且按住放到白板上,如下图:
这样关于联合halcon的界面内容就做完了,接下来就是布置界面,如下图:
强调:定时器的响应周期设置为40ms。
第二步:从halcon中导出C#
首先是图像的实时采集代码(借助图像采集助手),代码如下:
* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'gray', -1, 'false', 'default', '[1] ', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
dev_close_window () //手动添加
dev_open_window (0, 0, 512, 512, 'black', WindowHandle) //手动添加
while (true)
grab_image_async (Image, AcqHandle, -1)
dev_display (Image)
get_image_size (Image, Width, Height) //手动添加,获取图像的尺寸大小
dev_set_part (0, 0, Height, Width) //手动添加,放大到填充整个窗口
* Image Acquisition 01: Do something
endwhile
close_framegrabber (AcqHandle)
接着点击导出图标弹出导出窗口,如下图:
选择导出路径与导出形式,这里导出的形式为C#,。
第三步:halcon联合C#编写。
//
// File generated by HDevelop for HALCON/.NET (C#) Version 18.11.1.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
//
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
//
// Source files with different encoding should not be mixed in one project.
//
using HalconDotNet;
public partial class HDevelopExport
{
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public HDevelopExport()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
if (HalconAPI.isWindows)
HOperatorSet.SetSystem("use_window_thread","true");
action();
}
#endif
#if !NO_EXPORT_MAIN
// Main procedure
private void action()
{
// Local iconic variables
HObject ho_Image=null;
// Local control variables
HTuple hv_AcqHandle = new HTuple(), hv_WindowHandle = new HTuple();
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "gray",
-1, "false", "default", "[1] ", 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,512,512,0,"visible","",out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
while ((int)(1) != 0)
{
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);
}
//Image Acquisition 01: Do something
}
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
hv_AcqHandle.Dispose();
hv_WindowHandle.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
}
#endif
}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
static void Main(string[] args)
{
new HDevelopExport();
}
}
#endif
//定义变量
HObject ho_Image = null;
HTuple hv_AcqHandle = new HTuple(), hv_WindowHandle = new HTuple();
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
//打开相机按钮的方法
private void button1_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[1] ", 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, 512, 512, 0, "visible", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
}
//图像采集按钮的方法
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
6.关闭相机与停止定时器的程序放在关闭相机按钮方法里面,程序如下:
//关闭相机按钮的方法
private void button3_Click(object sender, EventArgs e)
{
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);
}
}
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
hWindowControl1即为窗体的名称,可在属性里查找。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace _666
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义变量
HObject ho_Image = null;
HTuple hv_AcqHandle = new HTuple(), hv_WindowHandle = new HTuple();
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
//打开相机按钮的方法
private void button1_Click(object sender, EventArgs e)
{
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "gray",
-1, "false", "default", "[1] ", 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);
}
//图像采集按钮的方法
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
//关闭相机按钮的方法
private void button3_Click(object sender, EventArgs e)
{
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);
}
}
}
}