VS(C#)调⽤海康⼯业相机SDK采集图像及基本功能设定VS(C#)调⽤海康⼯业相机SDK采集图像及基本功能设定
简介
由于⼯作关系,本⼈最近使⽤到了海康的⽹⼝及USB3.0接⼝的⼯业相机。现将相关内容进⾏整理记录。
开发环境
64位VS2013 +C#
Halcon12
海康MVS3.0.0
引⽤海康相机动态库(MvCameraControl.Net.dll)
在VS项⽬⽂件中添加添加引⽤,如下图
引⽤MVS安装⽬录下MVS\Development\DotNet\MvCameraControl.Net.dll这个⽂件。
创建相机类
⿏标右键单击⼯程项⽬–添加–类,选择“类”,输⼊类的名称,例如Hikvision,点击右下⾓的“添加”。
在项⽬中使⽤海康相机时,为便于程序编写,可引⼊如下的命名空间:
using ;
创建需要⽤到的全局变量
public MyCamera myCamera;//相机对象
private MyCamera.MV_CC_DEVICE_INFO_LIST deviceList;//设备列表
private MyCamera.MV_CC_DEVICE_INFO deviceInfo;//设备对象
private string seriesStr;//接收相机序列号
private MyCamera.MVCC_INTVALUE stParam;//⽤于接收特定的参数
//为读取、保存图像创建的数组
UInt32 m_nBufSizeForDriver = 4096 * 3000;
byte[] m_pBufForDriver = new byte[4096 * 3000];
UInt32 m_nBufSizeForSaveImage = 4096 * 3000 * 3 + 3000;
byte[] m_pBufForSaveImage = new byte[4096 * 3000 * 3 + 3000];
创建相关函数
1.创建构造函数
/
/在构造函数中实例化设备列表对象
public Hikvision()
{
deviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
}
2.创建改变相机IP的函数
//成功返回0失败返回-1
//调⽤函数时可以传⼊需要改变的⽬标IP,如过没有传⼊则将相机IP设置为其所连接的⽹卡地址+1或-1
public int changeIP(string IP = "")
{
try
{
//获取相机相关信息,例如相机所连接⽹卡的⽹址
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.SpecialInfo.stGigEInfo, 0);
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE _DEVICE_INFO));
IPAddress cameraIPAddress;
string tempStr = "";
if (IP.Trim().Equals("") || !(IPAddress.TryParse(IP, out cameraIPAddress)))
{
//当前⽹卡的IP地址
UInt32 nNetIp1 = (gigeInfo.nNetExport & 0xFF000000) >> 24;
UInt32 nNetIp2 = (gigeInfo.nNetExport & 0x00FF0000) >> 16;
UInt32 nNetIp3 = (gigeInfo.nNetExport & 0x0000FF00) >> 8;
UInt32 nNetIp4 = (gigeInfo.nNetExport & 0x000000FF);
//根据⽹卡IP设定相机IP,如果⽹卡ip第四位⼩于252,则相机ip第四位+1,否则相机IP第四位-1
UInt32 cameraIp1 = nNetIp1;
UInt32 cameraIp2 = nNetIp2;
UInt32 cameraIp3 = nNetIp3;
UInt32 cameraIp4 = nNetIp4;
if (nNetIp4 < 252)
{
cameraIp4++;
}
else
{
cameraIp4--;
}
tempStr = cameraIp1 + "." + cameraIp2 + "." + cameraIp3 + "." + cameraIp4;
}
else
{
tempStr = IP;
}
IPAddress.TryParse(tempStr, out cameraIPAddress);
long cameraIP = IPAddress.NetworkToHostOrder(cameraIPAddress.Address);
//设置相机掩码
uint maskIp1 = (gigeInfo.nCurrentSubNetMask & 0xFF000000) >> 24;
uint maskIp2 = (gigeInfo.nCurrentSubNetMask & 0x00FF0000) >> 16;
uint maskIp3 = (gigeInfo.nCurrentSubNetMask & 0x0000FF00) >> 8;
uint maskIp4 = (gigeInfo.nCurrentSubNetMask & 0x000000FF);
IPAddress subMaskAddress;
tempStr = maskIp1 + "." + maskIp2 + "." + maskIp3 + "." + maskIp4;
IPAddress.TryParse(tempStr, out subMaskAddress);
long maskIP = IPAddress.NetworkToHostOrder(subMaskAddress.Address);
//设置⽹关
uint gateIp1 = (gigeInfo.nDefultGateWay & 0xFF000000) >> 24;
uint gateIp2 = (gigeInfo.nDefultGateWay & 0x00FF0000) >> 16;
uint gateIp3 = (gigeInfo.nDefultGateWay & 0x0000FF00) >> 8;
uint gateIp4 = (gigeInfo.nDefultGateWay & 0x000000FF);
IPAddress gateAddress;
tempStr = gateIp1 + "." + gateIp2 + "." + gateIp3 + "." + gateIp4;
IPAddress.TryParse(tempStr, out gateAddress);
long gateIP = IPAddress.NetworkToHostOrder(gateAddress.Address);
int temp = myCamera.MV_GIGE_ForceIpEx_NET((UInt32)(cameraIP >> 32), (UInt32)(maskIP >> 32), (UInt32)(gateIP >> 32));//执⾏更改相机IP的命令
if (temp == 0)
//强制IP成功
return 0;
//强制IP失败
return -1;
}
catch
{
return -1;
}
}
3.创建相机连接函数
public int connectCamera(string id)//连接相机,返回-1为失败,0为成功
{
this.seriesStr = id;
string m_SerialNumber = "";//接收设备返回的序列号
int temp;//接收命令执⾏结果
myCamera = new MyCamera();
try
{
temp = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref deviceList);//更新设备列表
if (temp != 0)
{
//设备更新成功接收命令的返回值为0,返回值不为0则为异常
return -1;
}
//强制相机IP
for (int i = 0; i < deviceList.nDeviceNum; i++)
{
/*******该部分⽤于获取相机名称、序列号等,从⽽对指定的相机进⾏IP更改******/
//更改IP的函数中也有该部分,重叠部分程序可进⾏相应的简化,本⽂暂不做处理
deviceInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));//获取设备信息
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIG E_DEVICE_INFO));
/*****************************************************************/
m_SerialNumber = gigeInfo.chUserDefinedName;
if(deviceInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
//判断是否为⽹⼝相机
if (seriesStr.Equals(m_SerialNumber))
{
//如果相机⽤户名正确则修改IP
temp = myCamera.MV_CC_CreateDevice_NET(ref deviceInfo);//更改IP前需要创建设备对象
forceIP();
}
}
}
//更改IP后需要重新刷新设备列表,否则打开相机时会报错
temp = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref deviceList);//更新设备列表
for (int i = 0; i < deviceList.nDeviceNum; i++)
{
deviceInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(deviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));//获取设备
if (deviceInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_ GIGE_DEVICE_INFO));
//m_SerialNumber = gigeInfo.chSerialNumber;//获取序列号
m_SerialNumber = gigeInfo.chUserDefinedName;//获取⽤户名
}
else if (deviceInfo.nTLayerType == MyCamera.MV_USB_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(deviceInfo.SpecialInfo.stUsb3VInfo, 0);
MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.Ptr
ToStructure(buffer, typeof(MyCamera.MV_ USB3_DEVICE_INFO));
m_SerialNumber = usbInfo.chUserDefinedName;
}
if (seriesStr.Equals(m_SerialNumber))
if (seriesStr.Equals(m_SerialNumber))
{
temp = myCamera.MV_CC_CreateDevice_NET(ref deviceInfo);              if (MyCamera.MV_OK != temp)
{
//创建相机失败
return -1;
}
temp = myCamera.MV_CC_OpenDevice_NET();//
if (MyCamera.MV_OK != temp)
{
//打开相机失败
return -1;typeof array
}
return 0;
}
continue;
}
}
catch
{
return -1;
}
return 0;
}
4.创建开始采集、停⽌采集、关闭相机函数
public int startCamera()//相机开始采集,返回0为成功,-1为失败
{
int temp = myCamera.MV_CC_StartGrabbing_NET();
if (MyCamera.MV_OK != temp)
return -1;
return 0;
}
public int stopCamera()//停⽌相机采集,返回0为成功,-1为失败
{
int temp = myCamera.MV_CC_StopGrabbing_NET();
if (MyCamera.MV_OK != temp )
return -1;
return 0;
}
public int closeCamera()//关闭相机,返回0为成功,-1为失败
{
int temp = stopCamera();//停⽌相机采集
if (MyCamera.MV_OK != temp )
return -1;
temp = myCamera.MV_CC_CloseDevice_NET();
if (MyCamera.MV_OK != temp )
return -1;
temp = myCamera.MV_CC_DestroyDevice_NET();
if (MyCamera.MV_OK != temp )
return -1;
return 0;
}
5.创建发送软触发函数
//发送成功返回0,失败返回-1
public int softTrigger()
{
int temp = myCamera.MV_CC_SetCommandValue_NET("TriggerSoftware");    if (MyCamera.MV_OK != temp )
return -1;
return 0;
}
6.创建读取图像函数
函数返回Halcon图像库可以处理的Himage格式。
如何添加halcon图像库请⾃⾏百度,⽹上教程⾮常多。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。