halcon+WinForm打开摄像头

注意引用HalconDotNet.dll,项目属性平台一致(X64)https://www.cnblogs.com/xixixing/p/10634903.html

using HalconDotNet;//引用halcon
using System.Threading;//引用线程

namespace MyDemo
{
    public partial class Form1 : Form
    {
        HObject ho_Image = null;//原始图像
        HTuple hv_AcqHandle = null;//帧采集句柄
        public Form1()
        {
            InitializeComponent();//初始化
        }

        Thread thread1;//线程,控制采集的开、闭
        private void button1_Click(object sender, EventArgs e)
        {
            thread1 = new Thread(getin);//线程调用采集方法
            thread1.Start();//启动线程       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            thread1.Abort();//关闭线程
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);//关闭帧采集器
        }
        void getin()//采集方法
        {
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "gray",
        -1, "false", "default", "[0] ", 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 (thread1 != null)
            {
                thread1.Abort();//结束线程
            }
        }
    }
}

 

posted @ 2019-04-04 21:48  夕西行  阅读(1802)  评论(0编辑  收藏  举报