Bootstrap

vs联合halcon——采集图像(实时采集与单次采集)

摘要
在对vs进行环境配置好以后,就可以开始与halcon联合进行实战。本篇就对图像的采集进行总结。通过构建采集相机GrabImage类的三个方法实现图像的采集:

open() 打开相机
grabimage() 单次采集图像
close() 关闭相机,释放内存
最后用IrisSkin4实现WinForm窗体换皮肤。
在这里插入图片描述
在这里插入图片描述

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using HalconDotNet;
namespace 采集图像

{

public class GrabImage

{

HTuple hv_AcqHandle; //打开相机窗口的引用句柄

HObject ho_Image;//定义图片变量

HTuple imageWidth, imageHeight;//定义宽高
public void open()

{

try

{
HOperatorSet.GenEmptyObj(out ho_Image);// 初始化本地图像空间的变量

//打开本地相机

HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",

-1, "false", "default", "[0] Integrated Camera", 0, -1, out hv_AcqHandle);

//开始采集图像

HOperatorSet.GrabImageStart(hv_AcqHandle, -1);

}

catch (Exception)

{
System.Windows.Forms.MessageBox.Show("打开相机失败!");//报错

}
}

//构造单次采集图像方法

public void grabimage(HWindowControl HW)

{
//清空窗体halconwindows

HW.HalconWindow.ClearWindow();

//采集图像

HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);

//转成黑白图像

HOperatorSet.Rgb1ToGray(ho_Image, out ho_Image);

//图片自适应窗口

HOperatorSet.GetImageSize(ho_Image, out imageWidth, out imageHeight);

HOperatorSet.SetPart(HW.HalconWindow, 0, 0, imageHeight - 1, imageWidth - 1);

//显示图像

HOperatorSet.DispObj(ho_Image, HW.HalconWindow);

//以当前日期保存图像到D盘下

//HOperatorSet.WriteImage(ho_Image, "png", 0, "D:\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));

ho_Image.Dispose();
}

//关闭相机,释放内存

public void close()

{

ho_Image.Dispose();

HOperatorSet.CloseFramegrabber(hv_AcqHandle);
}

}

}

在这里插入图片描述
三,编写主窗体Form1程序

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;
namespace 采集图像

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();
}

GrabImage grabimg = new GrabImage();//实例化GrabImage类
private void Form1_Load(object sender, EventArgs e)

{

skinEngine1.SkinFile = System.Environment.CurrentDirectory+ "\\Skins\\Calmness.ssk";

skinEngine1.DisableTag = 8888;//设置不自动换肤。tag的值8888(默认9999)

grabimg.open();//调用open方法

}
private void button1_Click(object sender, EventArgs e)

{

grabimg.grabimage(hWindowControl1);

}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

grabimg.close();

}
private void timer1_Tick(object sender, EventArgs e)

{

grabimg.grabimage(hWindowControl2);
}

//实时采集

private void button2_Click(object sender, EventArgs e)

{

timer1.Start();

}

//停止采集

private void button3_Click(object sender, EventArgs e)

{

timer1.Stop();

}

}

}

在这里插入图片描述

;