#include "hikvisionsdk.h"
#include <windows.h>
#include <QDebug>
#include <QThread>
HikvisionSDK::HikvisionSDK()
{
/*初始化对象↓*/
nRet = MV_OK;
handle = NULL;
g_nPayloadSize = 0;
stImageInfo = {};
isGain = true;
x=0,y=0,w=500,h=500;
/*↑初始化对象*/
}
HikvisionSDK::~HikvisionSDK()
{
}
void HikvisionSDK::EnumDevice()
{
memset(&stDeviceList,0,sizeof(MV_CC_DEVICE_INFO_LIST));//开辟空间
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE,&stDeviceList);//枚举设备并返回信息进行判断
if(nRet != MV_OK)
{
QString text = QString("枚举设备失败!nRet [0x%1]").arg(nRet);
//QMessageBox::warning(this,"警告",text);
return;
}
if(stDeviceList.nDeviceNum > 0)
{
for (unsigned int i = 0;i < stDeviceList.nDeviceNum;i++)
{
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if(pDeviceInfo == NULL)
{
continue;
}
wchar_t* pUserName = NULL;
if(pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((stDeviceList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((stDeviceList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((stDeviceList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (stDeviceList.pDeviceInfo[i]->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
if(std::strcmp("",(LPCSTR)(pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName)) != 0)
{
DWORD dwLenUserName = MultiByteToWideChar(CP_ACP,0,(LPCSTR)(pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName),-1,NULL,0);
pUserName = new wchar_t[dwLenUserName];
MultiByteToWideChar(CP_ACP,0,(LPCSTR)(pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName),-1,pUserName,dwLenUserName);
}
else
{
DWORD dwLenUserName = MultiByteToWideChar(CP_ACP,0,(LPCSTR)(pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName),-1,NULL,0);
pUserName = new wchar_t[dwLenUserName];
MultiByteToWideChar(CP_ACP,0,(LPCSTR)(pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName),-1,pUserName,dwLenUserName);
}
QString text = QString("[%1]Gige:%2(%3.%4.%5.%6)").arg(i).arg(pUserName).arg(nIp1).arg(nIp2).arg(nIp3).arg(nIp4);
qDebug()<<text;
}else if(pDeviceInfo->nTLayerType == MV_USB_DEVICE)
{
printf("UserDefinedName: %s\n", pDeviceInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
printf("Serial Number: %s\n", pDeviceInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
printf("Device Number: %d\n\n", pDeviceInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
}
else
{
//QMessageBox::warning(this,"警告","未知设备");
QString text = QString("未知设备[%1]").arg(i);
qDebug()<<text;
}
if(pUserName)
{
delete [] pUserName;
pUserName = NULL;
}
}
}
if(stDeviceList.nDeviceNum == 0)
{
qDebug()<< "警告未发现设备";
emit signal_sendInfoToShow("未发现设备,请查看通信线是否连接,如确认连接请等待重连");
emit signal_sendConnectState(false);
return;
}
}
void HikvisionSDK::ConnectDevice()
{
//创建设备句柄
unsigned int nIndex = 0;
nRet = MV_CC_CreateHandle(&handle,stDeviceList.pDeviceInfo[nIndex]);
if(nRet != MV_OK)
{
QString text = QString("创建设备失败!nRet [%1]").arg(nRet);
qDebug()<< text;
return;
}
//设置IP
// nRet = MV_GIGE_ForceIpEx(handle,1921681100);
//打开设备
nRet = MV_CC_OpenDevice(handle);
if(nRet != MV_OK)
{
QString text = QString("打开设备失败!nRet [%1]").arg(nRet);
qDebug()<< text;
return;
}
// 检测网络最近封装尺寸(仅支持 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)
{
QString text = QString("设置封装尺寸失败!nRet [%1]").arg(nRet);
qDebug()<< text;
}
}
else
{
QString text = QString("获取封装尺寸失败!nRet [%1]").arg(nPacketSize);
qDebug()<< text;
}
}
SetTriggerMode();
GrabImage();
}
void HikvisionSDK::SetTriggerMode()
{
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
QString text = QString("设置触发模式失败!nRet [%1]").arg(nRet);
qDebug()<< text;
return;
}
}
void HikvisionSDK::GrabImage()
{
// qDebug()<<"1子线程"<<QThread::currentThreadId();
//获取playload尺寸
MVCC_INTVALUE stParam;
memset(&stParam, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
if (MV_OK != nRet)
{
QString text = QString("获取playlord失败!nRet [%1]").arg(nRet);
qDebug()<< text;
return;
}
//获取尺寸后+2个分配大小,否则报错分配空间不足
g_nPayloadSize = stParam.nCurValue+2;
qDebug()<<g_nPayloadSize;
//开始取流
nRet = MV_CC_StartGrabbing(handle);
if(nRet != MV_OK)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
QString text = QString("取流失败!nRet [0x%1]").arg(nRet);
qDebug()<<text;
return;
}
qDebug()<<"1开始取流";
MV_FRAME_OUT_INFO_EX stImageInfo = {};
memset(&stImageInfo,0,sizeof (MV_FRAME_OUT_INFO_EX));
pData = (unsigned char*)malloc(sizeof (unsigned char)* (g_nPayloadSize));
if (pData == NULL)
{
qDebug()<< "分配内容失败!";
return;
}
// 获取一张图片get one frame from camera with timeout=1000ms
nRet = MV_CC_GetOneFrameTimeout(handle, pData, g_nPayloadSize, &stImageInfo, 3000);
if (nRet == MV_OK)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum);
}
else
{
QString text = QString("无图像数据!nRet [%1]").arg(nRet);
qDebug()<< text;
free(pData);
pData = NULL;
return;
}
// qDebug()<<"取流完成";
emit signal_sendInfoToShow("检测模组连接成功");
emit signal_sendConnectState(true);//发送相机连接成功信号
emit signal_sendStartSDKWork();//发送图片采集准备完成信号
}
void HikvisionSDK::stopCamera()
{
MV_CC_StopGrabbing(handle);
qDebug()<<"关闭取流";
}
void HikvisionSDK::closeCamera()
{
if (NULL == handle)
{
return;
}
MV_CC_CloseDevice(handle);
int tempValue = MV_CC_DestroyHandle(handle);
handle = NULL;
if(tempValue!=0)
{
return;
}else
{
return;
}
qDebug()<<"关闭相机";
}
int HikvisionSDK::RGB2BGR(unsigned char *pRgbData, unsigned int nWidth, unsigned int nHeight)
{
if ( NULL == pRgbData )
{
return MV
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
HIK_muliCamera.zip (18个子文件)
HIK_muliCamera
hikvisionsdk.h 2KB
mainform.h 706B
Includes
MvISPErrorDefine.h 5KB
MvCameraControl.h 136KB
PixelType.h 16KB
MvErrorDefine.h 7KB
MvObsoleteInterfaces.h 108KB
MvCameraControl.lib 56KB
CameraParams.h 85KB
MvSdkExport.h 243KB
ObsoleteCamParams.h 23KB
picturedeal.h 610B
main.cpp 179B
picturedeal.cpp 2KB
mainform.ui 2KB
mainform.cpp 2KB
hikvisionsdk.cpp 11KB
HIK_muliCamera.pro 1KB
共 18 条
- 1
weixin_53157219
- 粉丝: 4
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0