时间:2023-03-09 12:09:57 | 栏目:C代码 | 点击:次
下面是我根据海康官方文档代码,放到VS 2022 版本中调试通过后的代码:
#include <stdio.h> #include <iostream> #include "Windows.h" #include "HCNetSDK.h" using namespace std; //时间解析宏定义 #define GET_YEAR(_time_) (((_time_)>>26) + 2000) #define GET_MONTH(_time_) (((_time_)>>22) & 15) #define GET_DAY(_time_) (((_time_)>>17) & 31) #define GET_HOUR(_time_) (((_time_)>>12) & 31) #define GET_MINUTE(_time_) (((_time_)>>6) & 63) #define GET_SECOND(_time_) (((_time_)>>0) & 63) BOOL CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser) { switch (lCommand) { case COMM_FIREDETECTION_ALARM: //火点检测报警 { printf("fire192.168.1.31 \n"); NET_DVR_FIREDETECTION_ALARM struFireDetection = { 0 }; memcpy(&struFireDetection, pAlarmInfo, sizeof(NET_DVR_FIREDETECTION_ALARM)); printf("火点检测报警: RelativeTime:%d, AbsTime:%d, PTZ{PanPos:%d, TiltPos:%d, ZoomPos:%d}, \ PicDataLen:%d, DevInfo{DevIP:%s, Port:%d, Channel:%d, IvmsChannel:%d}, \ FireMaxTemperature:%d, TargetDistance:%d, fireRectInfo{fX:%f,fY:%f,fWidth%f,fHeight%f}, \ fireMaxTemperaturePoint{fX:%f,fY:%f}\n", struFireDetection.dwRelativeTime, \ struFireDetection.dwAbsTime, struFireDetection.wPanPos, struFireDetection.wTiltPos, \ struFireDetection.wZoomPos, struFireDetection.dwPicDataLen, \ struFireDetection.struDevInfo.struDevIP.sIpV4, struFireDetection.struDevInfo.wPort, \ struFireDetection.struDevInfo.byChannel, struFireDetection.struDevInfo.byIvmsChannel, \ struFireDetection.wFireMaxTemperature, struFireDetection.wTargetDistance, \ struFireDetection.struRect.fX, struFireDetection.struRect.fY, struFireDetection.struRect.fWidth, \ struFireDetection.struRect.fHeight, struFireDetection.struPoint.fX, struFireDetection.struPoint.fY); NET_DVR_TIME struAbsTime = { 0 }; struAbsTime.dwYear = GET_YEAR(struFireDetection.dwAbsTime); struAbsTime.dwMonth = GET_MONTH(struFireDetection.dwAbsTime); struAbsTime.dwDay = GET_DAY(struFireDetection.dwAbsTime); struAbsTime.dwHour = GET_HOUR(struFireDetection.dwAbsTime); struAbsTime.dwMinute = GET_MINUTE(struFireDetection.dwAbsTime); struAbsTime.dwSecond = GET_SECOND(struFireDetection.dwAbsTime); //保存报警抓拍图片 if (struFireDetection.dwPicDataLen > 0 && struFireDetection.pBuffer != NULL) { char cFilename[256] = { 0 }; HANDLE hFile; DWORD dwReturn; char chTime[128]; sprintf_s(chTime, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d", struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, struAbsTime.dwMinute, struAbsTime.dwSecond); sprintf_s(cFilename, "FireDetectionPic[%s][%s].jpg", struFireDetection.struDevInfo.struDevIP.sIpV4, chTime); LPCWSTR tmp; // begin added by zhangchao tmp = L"FireDetectionPic31.jpg"; // end added by zhangchao //printf("%s", tmp); hFile = CreateFile(tmp, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { break; } WriteFile(hFile, struFireDetection.pBuffer, struFireDetection.dwPicDataLen, &dwReturn, NULL); CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } } break; default: printf("other192.168.1.31 \n"); printf("其他报警,报警信息类型: %d\n", lCommand); break; } return TRUE; } void RunCam(const char* ip) { //--------------------------------------- // 初始化 NET_DVR_Init(); //设置连接时间与重连时间 NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); //--------------------------------------- // 注册设备 LONG lUserID; //登录参数,包括设备地址、登录用户、密码等 NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 }; struLoginInfo.bUseAsynLogin = 0; //同步登录方式 strcpy_s(struLoginInfo.sDeviceAddress, ip); struLoginInfo.wPort = 8000; //设备服务端口 strcpy_s(struLoginInfo.sUserName, "your_username"); //设备登录用户名 strcpy_s(struLoginInfo.sPassword, "your_password"); //设备登录密码 //设备信息, 输出参数 NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 }; lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40); if (lUserID < 0) { printf("Login failed, error code: %d\n", NET_DVR_GetLastError()); NET_DVR_Cleanup(); return; } //设置报警回调函数 NET_DVR_SetDVRMessageCallBack_V31(MessageCallback, NULL); //启用布防 LONG lHandle; NET_DVR_SETUPALARM_PARAM struAlarmParam = { 0 }; struAlarmParam.dwSize = sizeof(struAlarmParam); //火点检测不需要设置其他报警布防参数,不支持 lHandle = NET_DVR_SetupAlarmChan_V41(lUserID, &struAlarmParam); if (lHandle < 0) { printf("NET_DVR_SetupAlarmChan_V41 error, %d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return; } Sleep(50000); //等待过程中,如果设备上传报警信息,在报警回调函数里面接收和处理报警信息 //撤销布防上传通道 if (!NET_DVR_CloseAlarmChan_V30(lHandle)) { printf("NET_DVR_CloseAlarmChan_V30 error, %d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return; } //注销用户 NET_DVR_Logout(lUserID); //释放SDK资源 NET_DVR_Cleanup(); } void main() { RunCam("192.168.1.31"); return; }
第一步:打开窗口顶部 【项目】菜单,选中 【<某某项目>属性】。
第二步:在打开的对话框中,左侧菜单选择 【C/C++】=> 【常规】,选中右侧附加包含目录中,点击右侧出现的向下箭头,点击编辑,在打开的对话框中如下填写:
其中 D:\ws\vc\emptycam\hkheader 文件夹放着海康的头文件,分别是: DataType.h、DecodeCardSdk.h、HCNetSDK.h、plaympeg4.h。
第三步:左侧菜单选择 【链接器】=> 【常规】。右侧选中附加库目录,点击右侧小三角,点击编辑,打开对话框。
文件夹 D:\ws\vc\emptycam\hkdll 是放海康dll文件的地方。文件如下图所示:
第四步:左侧菜单选择 【链接器】=> 【输入】。右侧选中【附加依赖项】点击右侧小三角,点击编辑,打开对话框。内容按照图片里的文字进行填写。