当前位置: 首页 > news >正文

基于C#的Baumer相机二次开发教程

文章目录

    • 环境配置
    • 接口层级
    • 启动相机
    • 读取相机数据

环境配置

  1. 新建项目
  2. 右键【依赖项】-> 选择【bin】文件夹中的【bgapi2_genicam_dotnet.dll】文件
  3. 当dll文件被添加到【依赖项\程序集】中之后,右键->【属性】,将其【复制本地】设为【True】

此动态链接库自身也有很多依赖项,为了省事儿,可以将其同文件夹下的内容,全部复制粘贴到debug或release文件夹。

接口层级

Baumer相机提供了多个不同的system,每个system下分为多个API,每个API又对应多个设备。因此,在二次开发时,需要先遍历所有的system,再遍历system下的api,最后遍历api下的device,才能获取相应的设备。

一般来说,每个system对应一个.cti格式的文件,例如我所用的SDK中,探测到了【bgapi2_usb.cti_2.15.2】和【bgapi2_gige.cti_2.15.2】这两个system。通过下面的代码,可以获取这些System

using BGAPI2;public string getSystemList()
{string txtInfo = "";SystemList sysList;BGAPI2.System mSystem = null;try{sysList = SystemList.Instance;sysList.Refresh();txtInfo += $"5.1.2 Detected systems: {sysList.Count}\n";}catch (BGAPI2.Exceptions.IException ex){txtInfo += $"ErrorType: {ex.GetType()}\n " +$"ErrorException {ex.GetErrorDescription()}\n " +$"in function: {ex.GetFunctionName()}";return txtInfo;}foreach (KeyValuePair<string, BGAPI2.System> sys in sysList){txtInfo += $"the {sys.Key} will be opened!\n";mSystem = sys.Value;mSystem.Open();txtInfo += //$"\nSystem ID: {mSystem.Id}\n" +$"System Name: {mSystem.FileName}\n" +$"System Vendor: {mSystem.Vendor}\n" +$"System Version: {mSystem.Version}\n" +$"System Model: {mSystem.Model}\n";mSystem.Close();}return txtInfo;
}

其返回内容表示,探测到两个system,列表如下

System Namebgapi2_usb.ctibgapi2_gige.cti
System VendorBaumerBaumer
System Version2.15.22.15.2
System Modelbgapi2_usbbgapi2_gige

USB和GigE分别是Baumer GAPI支持的接口类型,分别使用USB和网口进行通信。

启动相机

相机的启动流程为

  1. 打开system
  2. 打开api
  3. 打开设备
  4. 打开设备的数据流
  5. 打开数据流对应的buffer

每个system中,提供了不同的API,每个API又适配不同类型的设备,所以想获得设备信息,需要先便利system,再遍历相应的API,最后对设备进行遍历,代码如下。


BGAPI2.System workSystem;
BGAPI2.Device workDevice;
BGAPI2.Interface workApi;
BGAPI2.DataStream workDS;
BGAPI2.BufferList workBL;public void startSystem()
{SystemList sysList;BGAPI2.System mSystem;sysList = SystemList.Instance;sysList.Refresh();foreach (KeyValuePair<string, BGAPI2.System> sys in sysList){if (sys.Key.Contains("gige")){workSystem = sys.Value;workSystem.Open();break;}}
}public void startAPI()
{string txtInfo = "";InterfaceList iList = workSystem.Interfaces;iList.Refresh(1);foreach (KeyValuePair<string, BGAPI2.Interface> ifc_pair in iList){workApi = ifc_pair.Value;workApi.Open();}
}public void startDevice()
{BGAPI2.DeviceList devLst = workApi.Devices;devLst.Refresh(1);foreach (KeyValuePair<string, BGAPI2.Device> dev_pair in devLst){workDevice = dev_pair.Value;workDevice.Open();break;}
}public void startDataStream()
{BGAPI2.DataStreamList dsList = workDevice.DataStreams;dsList.Refresh();foreach (KeyValuePair<string, BGAPI2.DataStream> ds_pair in dsList){workDS = ds_pair.Value;workDS.Open();break;}
}public void startBuffer()
{workBL = workDS.BufferList;BGAPI2.Buffer bf;long iDevicePayloadsize = (long)workDevice.RemoteNodeList["PayloadSize"].Value;IntPtr mUserBuffer = new IntPtr(0);ulong uPayloadSize = workDS.IsDefinedPayloadSize ? workDS.PayloadSize : (ulong)iDevicePayloadsize;List<IntPtr> LUserBuffer = new List<IntPtr>();for (int i = 0; i < 4; i++){mUserBuffer = Marshal.AllocHGlobal((int)uPayloadSize);LUserBuffer.Add(mUserBuffer);IntPtr pUserObj = new IntPtr(0); // NULL pointer, not usedbf = new BGAPI2.Buffer(mUserBuffer, uPayloadSize, pUserObj);workBL.Add(bf); // 1MB buffer size}foreach (KeyValuePair<string, BGAPI2.Buffer> buf_pair in workBL){buf_pair.Value.QueueBuffer();}workDS.StartAcquisition();  //开始采集数据流workDevice.RemoteNodeList["AcquisitionStart"].Execute(); //开始采集数据
}

读取相机数据

在开启数据采集之后,接下来就是把数据装载到buffer中,如果想要读取这些buffer数据,可将其进一步存储为BGAPI2中的Image格式,示例如下

BGAPI2.Image imgWork = null;
public string fillBuffer()
{string info = "";ImageProcessor imgPro = new ImageProcessor(); //创建图像处理器对象BGAPI2.Buffer bfFilled = null;bfFilled = workDS.GetFilledBuffer(1000);if (bfFilled == null)info += "Error: Buffer Timeout after 1000 ms\n";else{info += $"图像{bfFilled.FrameID}的内存地址为{(ulong)bfFilled.MemPtr}";imgWork = imgPro.CreateImage((uint)bfFilled.Width,(uint)bfFilled.Height,(string)bfFilled.PixelFormat,bfFilled.MemPtr,(ulong)bfFilled.MemSize); //创建图像对象bfFilled.QueueBuffer(); //将缓冲区重新放回队列中,等待下一次使用}return info;
}

相关文章:

  • 主流防火墙策略绕过漏洞的修复方案与加固实践
  • 宽度优先遍历(bfs)(2)——fllodfill算法
  • QLoRA (Quantized Low-Rank Adaptation)浅析
  • 火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
  • 0_1排序与搜索
  • 【unitrix】 3.2 位取反运算(not.rs)
  • 【音视频】PJSIP库——示例简介、C++类说明
  • 【 感知集群】大规模分布式基础设施的AI赋能蓝图
  • Spring AOP @Before (前置通知): 在目标方法执行前做什么?
  • ChatGPT上瘾,大脑萎缩47%!?
  • Windows本地部署wordpress
  • 矩阵置零C++
  • Mac电脑-Office 2024 长期支持版(Excel、Word、PPT)
  • 《Go语言圣经》map
  • F接口基础.go
  • 基于Spring Boot的计算机考研交流系统的设计与实现
  • 微信小程序canvas实现抽奖动画
  • Arduino Nano 33 BLE Sense Rev 2开发板使用指南之【环境搭建 / 点灯】
  • 视频续播功能实现 - 断点续看从前端到 Spring Boot 后端
  • IDE深度集成+实时反馈:企业级软件测试方案Parasoft如何重塑汽车巨头的测试流程
  • 合肥网站优化步骤/武汉网站seo服务
  • 怎么做网页链接图片/seo排名优化哪家好
  • python兼职网站开发/互联网推广怎么找客户
  • 长春宣传片拍摄/厦门seo网络优化公司
  • 室内设计和网站建设哪个前景好/站长之家ppt素材
  • 教育企业重庆网站建设/百度视频免费高清影视