C#与VisionPro联合开发——链接相机

联合开发

Visual Studio 2022中引入visionpro的控件
在这里插入图片描述

下面的示例是在winform中实现了相机的链接、拍照功能、读取本地图片功能、保存图片功能、显示相机的实时画面功能,和设置相机的曝光值。

在这里插入图片描述

引入命名空间

using Cognex.VisionPro;
//图像操作的命名空间
using Cognex.VisionPro.ImageFile;

声明接口变量

//ICogAcqFifo 控制采集的fifo接口
public ICogAcqFifo m_Acqfifo = null;
//控制framegrabber的接口
public ICogFramGrabber m_FrameGrabber = null;

封装相机的加载方法,在窗体加载中调用

//封装相机加载的方法,在窗体加载中调用
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("出错了");
   }
 }

拍照

 //拍照
 private void btnTriggger_Click(object sender, EventArgs e) {
   
   //手动设置
   if (m_Acqfifo != null) {
   
     m_Acqfifo.StartAcquire();
   }
 }

读取图片

//读取图片
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();
}

保存图片

//保存图片
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("曝光值设置成功");
} 

解决关闭窗口时报错(给窗口绑定FormClosing事件)

//解决关闭窗口时报错
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
   
  //获取所有的链接设备
  CogFrameGrabbers cogFrameGrabbers = new CogFrameGrabbers();
  foreach (ICogFrameGrabber item in cogFrameGrabbers) {
   
    item.Disconnect(false);
  }
}

所有代码展示

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);
      }
    }
  }
}

相关推荐

  1. c的编译执行

    2024-02-23 09:24:04       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 09:24:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 09:24:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 09:24:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 09:24:04       18 阅读

热门阅读

  1. 中级.NET开发工程师面试经历

    2024-02-23 09:24:04       28 阅读
  2. MySQL

    2024-02-23 09:24:04       23 阅读
  3. 【npm install报错,如何解决记录】讲解

    2024-02-23 09:24:04       32 阅读
  4. 汽车会撞死人,应不应该限制汽车?

    2024-02-23 09:24:04       29 阅读
  5. lua 拓展math库,增加四舍五入函数 math.round

    2024-02-23 09:24:04       28 阅读
  6. Docker的优势及实际应用

    2024-02-23 09:24:04       23 阅读