C#中打开摄像头(Halcon)
1、halcon中打开摄像头
通过“助手”菜单打开“Image Acquisition”,如图所示,选择“DirectShow”表示普通USB摄像头。
通过“连接、拍照、视频”可以检测是否已经打开。
可以看到图像窗口中已经有视频可以显示,如图
可以选择代码生成,选择插入代码,可以看到在程序编辑区,代码已经插入。
这里是为了在C#中打开摄像头,下面选择代码导出:“文件---->导出”。
打开image_acp.cs代码如下:
// // File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0 // 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 = null; // Initialize local and output iconic variables 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, "rgb", -1, "false", "default", "[0] USB2.0 PC CAMERA", 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(); } #endif } #if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN) public class HDevelopExportApp { static void Main(string[] args) { new HDevelopExport(); } } #endif
2、C#中打开摄像头
(1)新建一个C#---WINFORM程序,在窗体上添加Halcon的HWindowControl控件(参照博客: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空间 using System.Threading; namespace 打开摄像头 { public partial class Form1 : Form { //halcon导出的变量 HObject ho_Image = null; HTuple hv_AcqHandle = null; public Form1() { InitializeComponent(); } //定义一个线程 Thread dispig; private void button1_Click(object sender, EventArgs e) {//图像采集 dispig = new Thread(getin); dispig.Start(); } private void button2_Click(object sender, EventArgs e) {//停止 dispig.Abort(); HOperatorSet.CloseFramegrabber(hv_AcqHandle); } void getin() { HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] USB2.0 PC CAMERA", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); while(true) { HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow); ho_Image.Dispose(); } } } }
(3)效果如下:
如果视频正在采集中,直接关闭窗体,程序会报错,其实是由于线程正在运行的问题(参照之前的博客:C#----多线程)。为窗体添加一个“FromClosing”事件即可,代码如下:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (dispig != null) { dispig.Abort(); //结束这个线程 } }
到此,程序完全可以正常运行。
其全部代码如下:
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空间 using System.Threading; namespace 打开摄像头 { public partial class Form1 : Form { //halcon导出的变量 HObject ho_Image = null; HTuple hv_AcqHandle = null; public Form1() { InitializeComponent(); } //定义一个线程 Thread dispig; private void button1_Click(object sender, EventArgs e) {//图像采集 dispig = new Thread(getin); dispig.Start(); } private void button2_Click(object sender, EventArgs e) {//停止 dispig.Abort(); HOperatorSet.CloseFramegrabber(hv_AcqHandle); } void getin() { HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] USB2.0 PC CAMERA", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); while(true) { HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow); ho_Image.Dispose(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (dispig != null) { dispig.Abort(); //结束这个线程 } } } }