资源下载链接
第一次使用海康威视的网络摄像头并进行二次开发,海康威视网络摄像头的二次开发的资料已经比较详细,网上关于这方面的介绍也不少。此处基于我的需求做了一些工作,特此记录。
目标:
1、实时预览
2、抓图
3、录制视频
4、监听告警信号
摄像头型号如下:
准备工作:
官网下载对应的版本的SDK:/cn/download_61.html
在下载的文件中有对应的开发指南、库文件、头文件、Demo示例等
代码分析:
//消息异常回调函数void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser){//qDebug("g_ExceptionCallBack: lUserID = %d, dwType = 0x%x\r\n",lUserID, dwType);switch(dwType){case EXCEPTION_RECONNECT: //预览时重连break;case PREVIEW_RECONNECTSUCCESS:case EXCEPTION_RELOGIN:case RESUME_EXCHANGE: break;default:break;}}
LONG lPort = -1; //全局的播放库port号void CALLBACK g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize, DWORD dwUser){switch (dwDataType){case NET_DVR_SYSHEAD: //系统头if (lPort >= 0){break; //该通道取流之前已经获取到句柄,后续接口不需要再调用}if (!PlayM4_GetPort(&lPort)) //获取播放库未使用的通道号{break;}//m_iPort = lPort; //第一次回调的是系统头,将获取的播放库port号赋值给全局port,下次回调数据时即使用此port号播放if (dwBufSize > 0){if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME)) //设置实时流播放模式{break;}if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, 1024*1024)) //打开流接口{break;}/* if (!PlayM4_Play(lPort, m_hWnd)) //播放开始{break;}*/}break;case NET_DVR_STREAMDATA: //码流数据if (dwBufSize > 0 && lPort != -1){if (!PlayM4_InputData(lPort, pBuffer, dwBufSize)){break;}}break;default: //其他数据if (dwBufSize > 0 && lPort != -1){if (!PlayM4_InputData(lPort, pBuffer, dwBufSize)){break;}}break;}}
//监听告警void CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser){((camerawidget*)pUser)->msgCallback(lCommand, pAlarmer, pAlarmInfo, dwBufLen);return;}void camerawidget::msgCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen){QString text = QString("msgCallback lCommand = %1").arg(lCommand);NET_VCA_RULE_ALARM struVcaRuleALramInfo;memcpy(&struVcaRuleALramInfo, pAlarmInfo, sizeof(NET_VCA_RULE_ALARM));switch (lCommand){case COMM_ALARM_RULE:{ switch (struVcaRuleALramInfo.struRuleInfo.wEventTypeEx){case ENUM_VCA_EVENT_TRAVERSE_PLANE:{ QString text = QString("越界");break;}case ENUM_VCA_EVENT_ENTER_AREA:{ QString text = QString("进入");break;} case ENUM_VCA_EVENT_LOITER:{ QString text = QString("徘徊");break;}case ENUM_VCA_EVENT_INTRUSION:{ QString text = QString("入侵");break;}case ENUM_VCA_EVENT_EXIT_AREA:{ QString text = QString("离开");break;}default:break; }break;} default:break; }return;}
//初始化SDKbool camerawidget::initSDK(){bool isok = NET_DVR_Init();if(isok == false){QMessageBox::warning(NULL,"error","NET_DVR_Init error;error number is "+QString::number(NET_DVR_GetLastError()));return isok;}//设置连接时间与重连时间NET_DVR_SetConnectTime(2000, 1);NET_DVR_SetReconnect(10000, true);return isok;}//登录摄像机void camerawidget::loginCamera(QString ip , int port , QString userName , QString password){//设置异常消息回调函数NET_DVR_SetExceptionCallBack_V30(0, NULL,g_ExceptionCallBack, NULL);NET_DVR_DEVICEINFO_V30 DeviceInfoTmp;userID = NET_DVR_Login_V30(ip.toLatin1().data() ,port, userName.toLatin1().data(), password.toLatin1().data() ,&DeviceInfoTmp);//设置报警回调函数BOOL isSuccess = NET_DVR_SetDVRMessageCallBack_V30(MessageCallback, this);if (isSuccess){//"NET_DVR_SetDVRMessageCallBack_V30 success"}//启用布防NET_DVR_SetupAlarmChan_V30(userID);if(m_cameraIndex >= RSPS_MAX_CAMERA_COUNT){return;}if(userID == RSPS_INVALID_CAMERA_USER_ID){QString text = tr("Login camera fail , IP = %1").arg(ip);}}
//开始预览void camerawidget::startPreview(){if(userID == RSPS_INVALID_CAMERA_USER_ID){return;}//预览NET_DVR_PREVIEWINFO struPlayInfo;struPlayInfo={0};struPlayInfo.hPlayWnd = (HWND)ui->label->winId(); //需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空struPlayInfo.lChannel = 1;//预览通道号struPlayInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推struPlayInfo.dwLinkMode = 0;//0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTPstruPlayInfo.bBlocked = 1;//0- 非阻塞取流,1- 阻塞取流IRealPlayHandle = NET_DVR_RealPlay_V40(userID,&struPlayInfo,NULL,NULL);if(IRealPlayHandle <0){QString text = tr("NET_DVR_RealPlay_V40 error, error number = %1").arg(NET_DVR_GetLastError());NET_DVR_Logout(userID);userID=-1;IRealPlayHandle = -1;return;}//注册回调函数if (!NET_DVR_SetRealDataCallBack(IRealPlayHandle, g_RealDataCallBack_V30 , 0)){QString text = tr("NET_DVR_SetRealDataCallBack error, error number = %1").arg(NET_DVR_GetLastError());}}//开始抓图void camerawidget::onClickedCapture(){if(userID!=-1){NET_DVR_JPEGPARA IpJpegPara2;IpJpegPara2.wPicQuality=0;IpJpegPara2.wPicSize=0xff;// 检查目录是否存在,若不存在则新建QString dirStr = RSPS_CAMERA_PIC_DIR;QDir dir;if (!dir.exists(dirStr)){dir.mkpath(dirStr);}QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyy-MM-dd_hh-mm-ss_zzz");QString fileName = dirStr + "\\";fileName += "M" + current_date;fileName += ".jpg";if(NET_DVR_CaptureJPEGPicture(userID,1,&IpJpegPara2, fileName.toLatin1().data())==false){QMessageBox::warning(NULL,"error","NET_DVR_CaptureJPEGPicture error;error number "+QString::number(NET_DVR_GetLastError()));return;}}}//开始录像void camerawidget::onClickedStartRecording(){// 检查目录是否存在,若不存在则新建QString dirStr = RSPS_CAMERA_FILE_DIR;QDir dir;if (!dir.exists(dirStr)){dir.mkpath(dirStr);}//设置当前时间为文件名QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss-zzz");QString sFileName = dirStr + "\\";sFileName += "M" + currentTime;sFileName += ".mp4";if(!NET_DVR_SaveRealData(IRealPlayHandle , sFileName.toLatin1().data())){QMessageBox::warning(NULL , "警告" , "启动录像失败"+QString::number(NET_DVR_GetLastError()));}}
资源下载链接
欢迎大家指教,希望大家喜欢
路漫漫其修远兮,同志仍须努力
如果觉得《基于QT的海康威视网络摄像头二次开发》对你有帮助,请点赞、收藏,并留下你的观点哦!