前言
提示:这里可以添加本文要记录的大概内容:
按照之前的经验,单线程取图需要用timer实时抓,有时候会出现线程卡住的情况。另外同样程序使用单线好像比多线内存用的大,这个还得再看看。
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目条件
使用c#和halcon联合,利用多线程的方法,采集图像按比例放到开的hwindowcontrol窗中,checkbox按钮控制实时采集
二、使用步骤
1.引入
代码如下(示例):
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace 多线程抓取图像
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
HTuple hv_AcqHandle;
HObject ho_Image = null;
HTuple hv_Width, hv_Height;
bool fContiGrab;
Thread thContiGrab;
HTuple hv_WindowHandle;
private void chkcontinueGrab_CheckedChanged(object sender, EventArgs e)
{
if(chkcontinueGrab.CheckState==CheckState.Checked)
{
fContiGrab = true;
//开线程
thContiGrab = new Thread(new ThreadStart(ThreadContinueGrab));
thContiGrab.Start();
thContiGrab.IsBackground = true;
}
else
{
fContiGrab = false;
}
}
private void ThreadContinueGrab()
{
while(fContiGrab)
{
HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
ho_Image.Dispose();
}
}
private void FormMain_Load(object sender, EventArgs e)
{
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] USB2.0 VGA UVC WebCam", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Size.Width, hWindowControl1.Size.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
HDevWindowStack.Push(hv_WindowHandle);
}
}
}
总结
1.需要在方案中新增dll的halcondotnet文件
2.工具添加项也需要增加该dll
3.需要引用该文件
4.需要引用系统中的线程