Halcon+VisualStudio使用线程实现大恒水星相机实时图像采集

时间:2024-05-18 19:39:52

实时采集图像,可将采集的图像保存到本地文件夹

具体C#代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using HalconDotNet;
using System.IO;


namespace Demo
{
    public partial class 图像采集 : Form
    {
        private Thread  ThreadObject;  //线程
        private bool    ThreadStop = false;//判断线程是否关闭
        private HTuple  WindowID;
        public 图像采集()
        {
            InitializeComponent();
            //线程对象实例化
            ThreadObject = new Thread(new ThreadStart(ThreadFunction));
            CreateHalconWindow();//创建halcon显示窗口
        }

        public void CreateHalconWindow()
        {
            HTuple FatherWindow = this.DisplayVideo_pictureBox.Handle;

            //设置窗口的背景颜色
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, this.DisplayVideo_pictureBox.Width, this.DisplayVideo_pictureBox.Height, FatherWindow, "visible", "", out WindowID);
        }

                //线程回调函数
        public void ThreadFunction()
        {
            HObject ho_Image = null;
            HTuple  hv_AcqHandle = null;
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.OpenFramegrabber("GenICamTL", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "MER-131-210U3M(KG0170060082)", 0, -1, out hv_AcqHandle);
            //300万像素:1280*1024
            int ImageWidth = 1280;
            int ImageHeight = 1024;//
            ThreadStop = false;
            while (!ThreadStop)
            {
                //ho_Image.Dispose();
                //HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);


                HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                ho_Image.Dispose();
                HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                //通过改变图像的缩放来适应图像在窗口的正常显示
                HOperatorSet.SetPart(WindowID, 0, 0, ImageHeight, ImageWidth);
                //在窗口上显示图像
                HOperatorSet.DispObj(ho_Image, WindowID);
                if (this.SaveImage_checkBox.Checked)
                {
                    string FileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒fff毫秒");
                    HOperatorSet.WriteImage(ho_Image, "bmp", 0, Directory.GetCurrentDirectory() + "/image/" + FileName + ".bmp");
                }
            }
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            ho_Image.Dispose();
        }
        private void DisplayImage_button_Click(object sender, EventArgs e)
        {
            if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
            {
                ThreadObject.Start();
            }
            if ((ThreadObject.ThreadState == System.Threading.ThreadState.Stopped) || (ThreadObject.ThreadState == System.Threading.ThreadState.Aborted))
            {
                ThreadObject = new Thread(new ThreadStart(ThreadFunction));
                ThreadObject.Start();
            }
        }
        private void StopPlay_button_Click(object sender, EventArgs e)
        {
            ThreadStop = true;
        }

}

}

 控制UI界面:
        
    Halcon+VisualStudio2015使用线程实现大恒水星相机实时图像采集