最近在做一个项目,涉及到工业相机,需要对其进行二次开发。相机方面选择了海康威视,网上关于海康威视工业相机SDK的开发资料很少,官方文档里面虽然写的是支持C++开发的,但其实是C。自己也摸索了一段时间,请教大佬,终于有了些收获,记录下来,方面以后查阅。
工业相机我买的是海康威视的,具体的参数如下:
工业相机资料:https://c109334.hi1718.com/news/201831317343619.html
镜头资料:https://c109334.hi1718.com/news/2018319114719513.html
VS2019+Opencv4.4+QT5.13.2+海康威视SDKMVS_SDK_V3_2_0
①VS工程配置Opencv(参考如下博客)
Opencv4.4文件:
链接:https://pan.baidu.com/s/1EpOfbkrzkpLyX14w7vINUw
提取码:7qxl
【opencv4.3.0教程】01之opencv介绍与配置
https://blog.csdn.net/shuiyixin/article/details/105998661
②VS中安装Qt(配置步骤参考下面两个博客)
Qt相关文件:
链接:https://pan.baidu.com/s/1r0gISpfQFuowwA7q2BL3og
提取码:gll1
https://blog.csdn.net/visual_eagle/article/details/106236109
Windows安装qt与VS2019添加QT工具过程
https://blog.csdn.net/ljb9854/article/details/102600931
③安装海康威视提供的MVS客户端操作,我是安装了然后直接就能打开了,若不能打开就设置一下电脑与相机的IP,MVS_SDK_V3_2_0_VC90_Runtime_190626文件是用来更新原来电脑上的MVS版本。
MVS文件:
链接:https://pan.baidu.com/s/1RanOEDx-HQ4zJgBpSobAqg
提取码:d3cy
参考博客:https://blog.csdn.net/u014779536/article/details/106577273
一开始在网上搜索相关海康威视SDK二次开发的时候,发现有很多的资料,然后就对着开始操作,自己琢磨了好久激活相机修改密码等等,发现都无济于事。后来一问客服才知道,那些是安防相机的相关资料,不是工业相机。
嗯,,,心态炸裂。。。
关于安防相机和工业相机(网络设备SDK和工业相机SDK)的区别,可以自行百度,这里提个醒防止走入误区。
安防相机买回来是需要激活重新设置密码的,工业相机买回来直接用。
这里贴出关于安防相机SDK二次开发的资料,给有需要的人做个参考。
关于安防相机的SDK相关资料下载:https://www.hikvision.com/cn/
这些SDK和工业相机的完全不一样,都不用,网上大部分的都是海康安防相机的SDK开发资料,什么实时预览&抓图&云台控制等等。
关于安防相机的SDK开发资料:
https://blog.csdn.net/o_ha_yo_yepeng/article/details/79537452
https://blog.csdn.net/o_ha_yo_yepeng/article/details/79825648
https://www.jianshu.com/p/1368c8ea24ff
https://blog.csdn.net/qq_15029743/article/details/79733960
https://www.bilibili.com/video/BV1ui4y1t72x?t=1326
买了相机之后,问下客服要一下MVS软件,安装之后,关于软件的使用说明和IP设置说明参考如下文件,关于如何查看相机IP等等,
千兆网口工业面阵相机用户手册V3.0.1
海康威视相机IP设置说明
链接:https://pan.baidu.com/s/11FD3GIhz5E1O5-vJ2u-_tA
提取码:n8oa
海康威视工业相机官网,里面有关于工业相机的介绍及客户端MVS软件等等。
https://www.hikrobotics.com/
①《海康威视工业相机SDK二次开发》这篇是MFC的,且官方提供的界面功能软件都是基于MFC的,配置好环境可以直接打开使用。
https://blog.csdn.net/weixin_47364204/article/details/108537518
②《C++调用海康相机SDK采集图像(C++\QT Creator\OpenCV\线程\接口)》
这篇是基于QTCreater的,没有在VS环境中,源码我下载下来了,给有需要的人。
链接:https://pan.baidu.com/s/1FsJg1hDCBdoJ_8hye6j1rw
提取码:z94k
https://blog.csdn.net/biggestcherry/article/details/106154321
PS:不管运行海康威视的哪个例程,都需要先配置下环境
在VS项目中配置opencv和mvs的动态链接库等(用到Opencv的话)
①在VS中配置Opencv
②在VS中配置MVS
功能:通过ip地址连接相机
该示例程序说明了如何通过ip地址连接网口相机。通过输入需要连接的相机ip(Camera Ip)和相机对应的网卡ip(Export Ip)来连接相机,输入的格式为xx.xx.xx.xx。
修改的地方:
一开始官方例程中的scanf会报错说这个函数不安全,这不是错误但在高版本VS下会认为这是个错误,解决方法有二:在项目设置中增加一行预处理定义可以自行百度。或者选择修改为scanf_s。
我选择改成scanf_s,且后面还要加个参数20,表示输入的最大字符数大小,ip地址加上三个点是15个字符,这里填个20。
参考:https://baike.baidu.com/item/scanf_s/443572?fr=aladdin
修改后的源码如下:
#include
#include
#include
#include
#include "MvCameraControl.h"
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// ch:等待按键输入 | en:Wait for key press
void WaitForKeyPress(void)
{
while (!_kbhit())
{
Sleep(10);
}
_getch();
}
static unsigned int __stdcall WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
unsigned char* pData = (unsigned char*)malloc(sizeof(unsigned char) * (g_nPayloadSize));
unsigned int nDataSize = g_nPayloadSize;
while (1)
{
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
printf("No data[0x%x]\n", nRet);
break;
}
if (g_bExit)
{
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
MV_CC_DEVICE_INFO stDevInfo = { 0 };
MV_GIGE_DEVICE_INFO stGigEDev = { 0 };
// ch:需要连接的相机ip(根据实际填充) | en:The camera IP that needs to be connected (based on actual padding)
printf("Please input Current Camera Ip : ");
char nCurrentIp[128];
scanf_s("%s", &nCurrentIp, 20);
// ch:相机对应的网卡ip(根据实际填充) | en:The pc IP that needs to be connected (based on actual padding)
printf("Please input Net Export Ip : ");
char nNetExport[128];
scanf_s("%s", &nNetExport, 20);
unsigned int nIp1, nIp2, nIp3, nIp4, nIp;
sscanf_s(nCurrentIp, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nCurrentIp = nIp;
sscanf_s(nNetExport, "%d.%d.%d.%d", &nIp1, &nIp2, &nIp3, &nIp4);
nIp = (nIp1 << 24) | (nIp2 << 16) | (nIp3 << 8) | nIp4;
stGigEDev.nNetExport = nIp;
stDevInfo.nTLayerType = MV_GIGE_DEVICE;// ch:仅支持GigE相机 | en:Only support GigE camera
stDevInfo.SpecialInfo.stGigEInfo = stGigEDev;
do
{
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, &stDevInfo);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet[0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDevInfo.nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
if (nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
// ch:设置触发模式为off | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParam.nCurValue;
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
unsigned int nThreadID = 0;
void* hThreadHandle = (void*)_beginthreadex(NULL, 0, WorkThread, handle, 0, &nThreadID);
if (NULL == hThreadHandle)
{
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
g_bExit = true;
Sleep(1000);
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
MV_CC_DestroyHandle(handle);
handle = NULL;
}
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}
运行效果:
将工业相机接上电源和连接电脑,通过打开MVS客户端软件,知道你电脑网口的IP地址和工业相机的IP地址:
表示相机正在采集中。。。
功能:图像采集并显示
该示例程序演示如何取图并显示取到的每一帧图像。
修改的地方:
一开始官方例程中会报错"const char"类型的实参与"LPCWSTR"类型的形参不兼容和不能将"const char"类型的值分配到"LPCWSTR"类型的实体,解决办法如下:
项目——项目属性——常规——项目默认值——字符集,把字符集设为未设置,确定。
源码如下:
#include
#include
#include
#include "windows.h"
#include "MvCameraControl.h"
HWND g_hwnd = NULL;
bool g_bExit = false;
unsigned int g_nPayloadSize = 0;
// ch:等待按键输入 | en:Wait for key press
void WaitForKeyPress(void)
{
while (!_kbhit())
{
Sleep(10);
}
_getch();
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
printf("Not support.\n");
}
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
g_hwnd = NULL;
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
static unsigned int __stdcall CreateRenderWindow(void* pUser)
{
HINSTANCE hInstance = ::GetModuleHandle(NULL); //获取应用程序的模块句柄
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW; //窗口的风格
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); //图标风格
wc.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //背景色
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); //鼠标风格
wc.lpfnWndProc = WndProc; //自定义消息处理函数
wc.lpszMenuName = NULL;
wc.lpszClassName = "RenderWindow"; //该窗口类的名称
if (!RegisterClassEx(&wc))
{
return 0;
}
DWORD style = WS_OVERLAPPEDWINDOW;
DWORD styleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
RECT rect = { 0, 0, 640, 480 };
AdjustWindowRectEx(&rect, style, false, styleEx);
HWND hWnd = CreateWindowEx(styleEx, "RenderWindow", "Display", style, 0, 0,
rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
return 0;
}
::UpdateWindow(hWnd);
::ShowWindow(hWnd, SW_SHOW);
g_hwnd = hWnd;
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
static unsigned int __stdcall WorkThread(void* pUser)
{
int nRet = MV_OK;
MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };
MV_DISPLAY_FRAME_INFO stDisplayInfo = { 0 };
unsigned char* pData = (unsigned char*)malloc(sizeof(unsigned char) * (g_nPayloadSize));
if (pData == NULL)
{
return 0;
}
unsigned int nDataSize = g_nPayloadSize;
while (1)
{
nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
if (g_hwnd)
{
stDisplayInfo.hWnd = g_hwnd;
stDisplayInfo.pData = pData;
stDisplayInfo.nDataLen = stImageInfo.nFrameLen;
stDisplayInfo.nWidth = stImageInfo.nWidth;
stDisplayInfo.nHeight = stImageInfo.nHeight;
stDisplayInfo.enPixelType = stImageInfo.enPixelType;
MV_CC_DisplayOneFrame(pUser, &stDisplayInfo);
}
}
else
{
printf("No data[0x%x]\n", nRet);
}
if (g_bExit)
{
break;
}
}
free(pData);
return 0;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// ch:枚举设备 | en:Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList = { 0 };
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
if (MV_OK != nRet)
{
printf("Enum Devices fail! nRet [0x%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Input camera index:");
unsigned int nIndex = 0;
scanf_s("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Input error!\n");
break;
}
// ch:选择设备并创建句柄 | en:Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | en:Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
if (nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize);
}
}
// ch:设置触发模式为off | en:Set trigger mode as off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParam = { 0 };
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParam.nCurValue;
unsigned int nThreadID = 0;
void* hCreateWindow = (void*)_beginthreadex(NULL, 0, CreateRenderWindow, handle, 0, &nThreadID);
if (NULL == hCreateWindow)
{
break;
}
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
nThreadID = 0;
void* hThreadHandle = (void*)_beginthreadex(NULL, 0, WorkThread, handle, 0, &nThreadID);
if (NULL == hThreadHandle)
{
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
g_bExit = true;
WaitForSingleObject(hThreadHandle, INFINITE);
CloseHandle(hThreadHandle);
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("ClosDevice fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
break;
}
} while (0);
if (nRet != MV_OK)
{
if (handle != NULL)
{
MV_CC_DestroyHandle(handle);
handle = NULL;
}
}
printf("Press a key to exit.\n");
WaitForKeyPress();
return 0;
}
D:\MVS\MVS\Development\Documentations 里面有各种语言及相关的SDK开发指南,如果是C++,也看《工业相机SDK开发指南 C》,里面关于工业相机的各种操作如取流、图像采集、图像处理等等都有详细的API调用,需要什么,就去这个文档中去查阅。
D:\MVS\MVS\Development\Samples\VC\VS 里面是VS项目的开发示例,前面六个是已经写好的MFC界面程序,具体的操控看示例程序说明VS2008.pdf,SimpleSamples中的是一个个小示例,在《工业相机SDK开发指南 C》里面有讲解。