☰
(三)MVS SDK采集图像C#
2026/10/14 8:25:26 网站建设 项目流程

一、SDK基本结构

二、调用流程

SDK所占用的资源。由于该垃圾回收机制存在滞后性,建议主动释放资源,以保证资源及时被释放。可通过如下两种方式主动释放资源

(1)方式1:调用.NET的 Dispose()

try { // ch:创建设备 | en:Create device device = DeviceFactory.CreateDevice(deviceInfo); } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (device != null) { // ch:释放设备资源 | en:Dispose resource device.Dispose(); } }

(2)方式2:使用using命令释放资源

// ch:创建设备 | en:Create device using (IDevice device = DeviceFactory.CreateDevice(deviceInfo)) { // ch:打开设备 | en:Open device device.Open(); // ch:关闭设备 | en:Close device device.Close(); }

三、具体使用方式示例

(1)刷新设备列表,枚举相机

变量:设备枚举类型和设备列表,枚举类型包括GigEDevice,USBDevice,可以自行添加

//设备类型枚举 readonly DeviceTLayerType enumTLayerType = DeviceTLayerType.MvGigEDevice | DeviceTLayerType.MvUsbDevice | DeviceTLayerType.MvGenTLGigEDevice | DeviceTLayerType.MvGenTLCXPDevice | DeviceTLayerType.MvGenTLCameraLinkDevice | DeviceTLayerType.MvGenTLXoFDevice; public List<IDeviceInfo> deviceInfoList = new List<IDeviceInfo>();

刷新相机

public int RefreshDeviceList() { // ch:创建设备列表 | en:Create Device List int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out deviceInfoList); if (nRet != MvError.MV_OK) { //枚举失败返回 return nRet; } return MvError.MV_OK; }

获取相机数量

public int DeviceNum() { return deviceInfoList.Count; }

(2)操作设备

枚举到相机列表后,具体操作那个相机,以设备句柄作为依据,结构体为IDevice

public IDevice device = null;

如打开和关闭相机

public int OpenDevice() { try { // ch:打开设备 | en:Open device device = DeviceFactory.CreateDevice(deviceInfoList[index]); } catch (Exception ex) { return CREATEFAILED; } nRet = device.Open(); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public void CloseDevice() { // ch:关闭设备 | en:Close Device if (device != null) { device.Close(); device.Dispose(); } }

(3)参数设置

设置参数要在打开相机之后,开始取流之前。参数是通过属性树字符串命令设置的。

属性树的命令参数在点击对应触发源时下方黄色可以看到,也可以通过手册查询。分为节点名,属性类型,共有六种属性类型。Float型,枚举类型,Bool型,整形,String 类型和命令型,并分别有读取和写入两种方法。

如获取浮点型参数的C#函数:

public int GetFloatParam(in string key, out float valu) { IFloatValue floatValue; nRet = device.Parameters.GetFloatValue(key, out floatValue); valu = floatValue.CurValue; if (nRet == MvError.MV_OK) { return nRet; } return nRet; }

执行软触发,相机复位等属于命令型,如执行一次软触发,执行软触发需要相机开启触发模式并选择触发源为软触发。C#函数:

public int SoftTriggerOnce() { // ch:触发命令 | en:Trigger command nRet = device.Parameters.SetCommandValue("TriggerSoftware"); if (nRet != MvError.MV_OK) { return nRet; } return MvError.MV_OK; }

以下为设置全部参数的C#函数:

public int GetFloatParam(in string key, out float valu) { IFloatValue floatValue; nRet = device.Parameters.GetFloatValue(key, out floatValue); valu = floatValue.CurValue; if (nRet == MvError.MV_OK) { return nRet; } return nRet; } public int GetEnumParam(in string key, out string valu) { IEnumValue enumValue; nRet = device.Parameters.GetEnumValue(key, out enumValue); valu = enumValue.CurEnumEntry.Symbolic; if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int GetBoolParam(in string key, out bool valu) { nRet = device.Parameters.GetBoolValue(key, out bool value); valu = value; if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int GetIntParam(in string key, out long valu) { IIntValue value; nRet = device.Parameters.GetIntValue(key, out value); valu = value.CurValue; if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int GetStringParam(in string key, out string valu) { IStringValue value; nRet = device.Parameters.GetStringValue(key, out value); valu = value.CurValue; if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetEnumParam(in string key, uint valu) { nRet = device.Parameters.SetEnumValue(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetEnumParamByString(in string key, string valu) { nRet = device.Parameters.SetEnumValueByString(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetIntParam(in string key, long valu) { nRet = device.Parameters.SetIntValue(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetFloatParam(in string key, float valu) { nRet = device.Parameters.SetFloatValue(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetBoolParam(in string key, bool valu) { nRet = device.Parameters.SetBoolValue(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetStringParam(in string key, string valu) { nRet = device.Parameters.SetStringValue(key, valu); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int SetCommandParam(in string key) { nRet = device.Parameters.SetCommandValue(key); if (nRet != MvError.MV_OK) { return nRet; } return nRet; }

(4)开始取流和结束取流

public int StartGrabbing() { // ch:开始采集 | en:Start Grabbing nRet = device.StreamGrabber.StartGrabbing(); if (nRet != MvError.MV_OK) { return nRet; } return nRet; } public int StopGrabbing() { // ch:停止采集 | en:Stop Grabbing nRet = device.StreamGrabber.StopGrabbing(); if (nRet != MvError.MV_OK) { return nRet; } return nRet; }

(5)获取一张图像,以超时退出得模式

public int GetOneFrame() { nRet = device.StreamGrabber.GetImageBuffer(1000, out frameOut); if (MvError.MV_OK != nRet) { return nRet; } buff = frameOut; return MvError.MV_OK; }

(6)开始和停止录像

public int StartRecordVideo(string path, float frameRate) { IIntValue intValue; IEnumValue enumValue; uint width; uint height; MvGvspPixelType pixelType; nRet = device.Parameters.GetIntValue("Width", out intValue); if (nRet != MvError.MV_OK) { return nRet; } width = (uint)intValue.CurValue; nRet = device.Parameters.GetIntValue("Height", out intValue); if (nRet != MvError.MV_OK) { return nRet; } height = (uint)intValue.CurValue; nRet = device.Parameters.GetEnumValue("PixelFormat", out enumValue); if (nRet != MvError.MV_OK) { // ShowErrorMsg("Get PixelFormat failed!", result); return nRet; } pixelType = (MvGvspPixelType)enumValue.CurEnumEntry.Value; // ch:开始录像 | en:Start record RecordParam recordParam; recordParam.Width = width; recordParam.Height = height; recordParam.PixelType = pixelType; recordParam.FrameRate = frameRate; recordParam.BitRate = 1000; recordParam.FormatType = VideoFormatType.AVI; nRet = device.VideoRecorder.StartRecord(path, recordParam); if (nRet != MvError.MV_OK) { return nRet; } isRecord = true; return nRet; } public int StopRecordVideo() { nRet = device.VideoRecorder.StopRecord(); if (nRet != MvError.MV_OK) { return nRet; } return nRet; }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询