联合开发
在Visual Studio 2022
中引入visionpro的控件
下面的示例是在winform中实现了相机的链接、拍照功能、读取本地图片功能、保存图片功能、显示相机的实时画面功能,和设置相机的曝光值。
引入命名空间
csharp
using Cognex.VisionPro;
//图像操作的命名空间
using Cognex.VisionPro.ImageFile;
声明接口变量
csharp
//ICogAcqFifo 控制采集的fifo接口
public ICogAcqFifo m_Acqfifo = null;
//控制framegrabber的接口
public ICogFramGrabber m_FrameGrabber = null;
封装相机的加载方法,在窗体加载中调用
csharp
//封装相机加载的方法,在窗体加载中调用
private void Form1_Load(object sender, EventArgs e) {
//获取已经连接的相机
CogFrameGrabbers frameGrabbers = new CogFrameGrabbers();
//判断是否有相机,链接的相机有可能不止一个
if (frameGrabbers.Count < 1) {
MessageBox.Show("未连接相机");
return;
} else {
MessageBox.Show("当前链接相机台数为:" + frameGrabbers.Count);
}
//如果有多台相机,则需要遍历
foreach (ICogFrameGrabber frameGrabber in frameGrabbers) {
try {
m_FrameGrabber = frameGrabber;
//创建图像采集接口,相机模式设置成黑白图(也可以获取本地的vpp文件)
m_Acqfifo = m_FrameGrabber.CreateAcqFifo("Generic GigEVision (Mono)", CogAcqFifoPixelFormatConstants.Format8Grey, 0, true);
//设置曝光
m_Acqfifo.OwnedExposureParams.Exposure = 80;
//配置采集图像事件,因为图像是持续采集的所以需要 +=
m_Acqfifo.Complete += m_Acqfifo_Comlete;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
//封装生成图像的事件,让采集图像自动跳转到该事件
private void m_Acqfifo_Comlete(object sender, CogCompleteEventArgs e) {
int numReadyVal, numPendingVal;
bool busyVal;
try {
//黑白图像
CogImage8Grey image = new CogImage8Grey();
CogAcqInfo info = new CogAcqInfo();
//获取采集的信息
m_Acqfifo.GetFifoState(out numPendingVal, out numReadyVal, out busyVal);
//判断采集的信息是否存在,如果存在则不为0
if (numReadyVal > 0) {
//把图像设置成从相机获取到的最新图
image = m_Acqfifo.CompleteAcquireEx(info) as CogImage8Grey;
cogRecordDisplay1.Image = null;
cogRecordDisplay1.Image = image;
cogRecordDisplay1.Fit();
}
} catch {
MessageBox.Show("出错了");
}
}
拍照
csharp
//拍照
private void btnTriggger_Click(object sender, EventArgs e) {
//手动设置
if (m_Acqfifo != null) {
m_Acqfifo.StartAcquire();
}
}
读取图片
csharp
//读取图片
private void button2_Click(object sender, EventArgs e) {
string fileName = @"C:\Users\郭贝贝\Pictures\Pict.jpg";
CogImageFileTool mIFTool = new CogImageFileTool();
mIFTool.Operator.Open(path, CogImageFileModeConstants.Read);
mIFTool.Run();
cogRecordDisplay1.Image = mIFTool.OutputImage;
cogRecordDisplay1.Fit();
}
保存图片
csharp
//保存图片
private void button3_Click(object sender, EventArgs e) {
try {
cogRecordDisplay1.Image.ToBitmap().Save(@"images\" + textBox1.Text + ".png");
MessageBox.Show("保存成功");
} catch (Exception ex) {
MessageBox.Show("保存失败:" + ex);
}
}
实时画面
csharp
//实时画面
private void button4_Click(object sender, EventArgs e) {
CogAcqFifoTool fifo = (CogAcqFifoTool)CogSerializer.LoadObjectFromFile(Directory.GetCurrentDirectory() + "\\vpp\\abc.vpp");
if (this.button4.Text.Equals("实时画面")) {
//打开实时显示
cogRecordDisplay1.StartLiveDisplay(fifo.Operator, false);
this.button4.Text = "关闭实时画面";
} else {
cogRecordDisplay1.StopLiveDisplay();
this.button4.Text = "实时画面";
}
}
设置曝光
csharp
//设置曝光
private void button5_Click(object sender, EventArgs e) {
m_Acqfifo.OwnedExposureParams.Exposure = Convert.ToInt32(textBox2.Text);
MessageBox.Show("曝光值设置成功");
}
解决关闭窗口时报错(给窗口绑定FormClosing事件)
csharp
//解决关闭窗口时报错
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
//获取所有的链接设备
CogFrameGrabbers cogFrameGrabbers = new CogFrameGrabbers();
foreach (ICogFrameGrabber item in cogFrameGrabbers) {
item.Disconnect(false);
}
}
所有代码展示
csharp
using System;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
//图像操作的命名空间
using Cognex.VisionPro.ImageFile;
namespace FrameGrabber {
public partial class Form1 : Form {
//ICogAcqFifo 控制采集的fifo接口
public ICogAcqFifo m_Acqfifo = null;
//ICogFrameGrabber 图像帧(图像采集)
public ICogFrameGrabber m_FrameGrabber = null;//控制framegrabber的接口
public Form1() {
InitializeComponent();
}
//封装相机加载的方法,在窗体加载中调用
private void Form1_Load(object sender, EventArgs e) {
//封装相机加载的方法,在窗体加载中调用
//获取已经连接的相机
CogFrameGrabbers frameGrabbers = new CogFrameGrabbers();
//判断是否有相机,链接的相机有可能不止一个
if (frameGrabbers.Count < 1) {
MessageBox.Show("未连接相机");
return;
} else {
MessageBox.Show("当前链接相机台数为:" + frameGrabbers.Count);
}
//如果有多台相机,则需要遍历
foreach (ICogFrameGrabber frameGrabber in frameGrabbers) {
try {
m_FrameGrabber = frameGrabber;
//创建图像采集接口,相机模式设置成黑白图
m_Acqfifo = m_FrameGrabber.CreateAcqFifo("Generic GigEVision (Mono)", CogAcqFifoPixelFormatConstants.Format8Grey, 0, true);
//设置曝光
m_Acqfifo.OwnedExposureParams.Exposure = 80;
//配置采集图像事件,因为图像是持续采集的所以需要 +=
m_Acqfifo.Complete += m_Acqfifo_Comlete;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
//封装生成图像的事件,让采集图像自动跳转到该事件
private void m_Acqfifo_Comlete(object sender, CogCompleteEventArgs e) {
int numReadyVal, numPendingVal;
bool busyVal;
try {
//黑白图像
CogImage8Grey image = new CogImage8Grey();
CogAcqInfo info = new CogAcqInfo();
//获取采集的信息
m_Acqfifo.GetFifoState(out numPendingVal, out numReadyVal, out busyVal);
//判断采集的信息是否存在,如果存在则不为0
if (numReadyVal > 0) {
//把图像设置成从相机获取到的最新图
image = m_Acqfifo.CompleteAcquireEx(info) as CogImage8Grey;
cogRecordDisplay1.Image = null;
cogRecordDisplay1.Image = image;
cogRecordDisplay1.Fit();
}
} catch {
MessageBox.Show("出错了");
}
}
//拍照
private void btnTriggger_Click(object sender, EventArgs e) {
//手动设置
if (m_Acqfifo != null) {
m_Acqfifo.StartAcquire();
}
}
//读取图片
private void button2_Click(object sender, EventArgs e) {
string path = "C:\\Users\\郭贝贝\\Pictures\\Saved Pictures\\230803-1579619283a6c1.jpg";
CogImageFileTool mIFTool = new CogImageFileTool();
mIFTool.Operator.Open(path, CogImageFileModeConstants.Read);
mIFTool.Run();
cogRecordDisplay1.Image = mIFTool.OutputImage;
cogRecordDisplay1.Fit();
}
//保存图片
private void button3_Click(object sender, EventArgs e) {
try {
cogRecordDisplay1.Image.ToBitmap().Save(@"images\" + textBox1.Text + ".png");
MessageBox.Show("保存成功");
} catch (Exception ex) {
MessageBox.Show("保存失败:" + ex);
}
}
//实时画面
private void button4_Click(object sender, EventArgs e) {
CogAcqFifoTool fifo = (CogAcqFifoTool)CogSerializer.LoadObjectFromFile(Directory.GetCurrentDirectory() + "\\vpp\\abc.vpp");
if (this.button4.Text.Equals("实时画面")) {
//打开实时显示
cogRecordDisplay1.StartLiveDisplay(fifo.Operator, false);
this.button4.Text = "关闭实时画面";
} else {
cogRecordDisplay1.StopLiveDisplay();
this.button4.Text = "实时画面";
}
}
//设置相机曝光值
private void button5_Click(object sender, EventArgs e) {
m_Acqfifo.OwnedExposureParams.Exposure = Convert.ToInt32(textBox2.Text);
MessageBox.Show("曝光值设置成功");
}
//解决关闭窗口时报错
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
//获取所有的链接设备
CogFrameGrabbers cogFrameGrabbers = new CogFrameGrabbers();
foreach (ICogFrameGrabber item in cogFrameGrabbers) {
item.Disconnect(false);
}
}
}
}