(三)MVS SDK采集图像C#

📅 2026/7/10 21:53:33
(三)MVS SDK采集图像C#
一、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刷新设备列表枚举相机变量:设备枚举类型和设备列表枚举类型包括GigEDeviceUSBDevice可以自行添加//设备类型枚举 readonly DeviceTLayerType enumTLayerType DeviceTLayerType.MvGigEDevice | DeviceTLayerType.MvUsbDevice | DeviceTLayerType.MvGenTLGigEDevice | DeviceTLayerType.MvGenTLCXPDevice | DeviceTLayerType.MvGenTLCameraLinkDevice | DeviceTLayerType.MvGenTLXoFDevice; public ListIDeviceInfo deviceInfoList new ListIDeviceInfo();刷新相机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操作设备枚举到相机列表后具体操作那个相机以设备句柄作为依据结构体为IDevicepublic 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; }