1.打开Halcon, 使用图像采集助手获取相机实时图像:
1.1 获取实时图像:
1.2 插入采集实时图像的Halcon代码,并导出:
Image_acq.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=null; // Local control variables HTuple hv_AcqHandle = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); //Image Acquisition 01: Code generated by Image Acquisition 01 HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "e0508b2e4c36_DahuaTechnology_A5201MG50", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); //Image Acquisition 01: Do something } HOperatorSet.CloseFramegrabber(hv_AcqHandle); 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(); } }
2. 在c#项目中画好对应的按钮:
对采集/停止按钮 执行方法进行编程(注意头部添加using HalconDotNet; 用到线程还要添加:using System.Threading;):
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; using System.Threading; namespace demo4 { public partial class Form1 : Form { HObject ho_Image = null; HTuple hv_AcqHandle = null; public Form1() { InitializeComponent(); } Thread dispig; //采集 private void button1_Click(object sender, EventArgs e) { dispig = new Thread(showFrame); dispig.Start(); } //停止 private void button2_Click(object sender, EventArgs e) { dispig.Abort(); HOperatorSet.CloseFramegrabber(hv_AcqHandle); } void showFrame() { // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); //Image Acquisition 01: Code generated by Image Acquisition 01 HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "e0508b2e4c36_DahuaTechnology_A5201MG50", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); //Image Acquisition 01: Do something HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow); } HOperatorSet.CloseFramegrabber(hv_AcqHandle); ho_Image.Dispose(); } } }
在Debug文件下添加“halcon.dll”文件,把目标平台改成“Any CPU”之后执行,结果还是出错:
该错误是因为Debug目录下没有Halcon加载相机的相关dll导致,为了方便,我们将“HALCON-12.0\bin\x64-win64”目录下的所有dll拷贝到Debug文件下,然后再次执行采集按钮:
按停止后图像不采集。
大功告成!
后续:
上面图像并没有完全显示,而是受到随意拉动HWindowControl空间后缩放的影响,我们注意HWindowControl的如下两个属性(红圈):
其中两个值已经是我改过来的,因为我使用的相机是1920*1200的。 ImagePart表示采集相机图像的尺寸,Size属性表示空间HWindowControl的尺寸,把Size的长宽比与采集的图像长宽比保持一致即可:
大功告成!
时间: 2024-11-07 17:41:08