標籤:lin 方法 star dev def space thread net abi
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的長寬比與採集的映像長寬比保持一致即可:
大功告成!
Halcon學習筆記(2) VS2010 + Halcon12 C#連結相機