当前位置:   article > 正文

C#与VisionPro联合开发——链接相机_c# 调用visionpro相机

c# 调用visionpro相机

联合开发

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

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

在这里插入图片描述

引入命名空间

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

声明接口变量

//ICogAcqFifo 控制采集的fifo接口
public ICogAcqFifo m_Acqfifo = null;
//控制framegrabber的接口
public ICogFramGrabber m_FrameGrabber = null;
  • 1
  • 2
  • 3
  • 4

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

//封装相机加载的方法,在窗体加载中调用
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("出错了");
   }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

拍照

 //拍照
 private void btnTriggger_Click(object sender, EventArgs e) {
   //手动设置
   if (m_Acqfifo != null) {
     m_Acqfifo.StartAcquire();
   }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

读取图片

//读取图片
private void button2_Click(object sender, EventArgs e) {
  //Directory.GetCurrentDirectory() 本地DeBug目录
  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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

保存图片

//保存图片
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);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

实时画面

//实时画面
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 = "实时画面";
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

设置曝光

//设置曝光
private void button5_Click(object sender, EventArgs e) {
	m_Acqfifo.OwnedExposureParams.Exposure = Convert.ToInt32(textBox2.Text);
  	MessageBox.Show("曝光值设置成功");
} 
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

所有代码展示

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/495456
推荐阅读
相关标签
  

闽ICP备14008679号