173VS2022调试通过海康温度报警SDK的C++代码

Posted zhangchao19890805

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了173VS2022调试通过海康温度报警SDK的C++代码相关的知识,希望对你有一定的参考价值。

具体的开发环境配置方法可以参考 【160】VS2022调试通过海康摄像头烟火识别SDK

操作系统: Windows

需要先在海康平台设置好报警温度,然后就可以编写代码监听警报。此功能可以用于火灾报警的场景中,推荐和火点检测配合使用。

#include <stdio.h>
#include <iostream>
#include <time.h>
#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)

char* gip;

BOOL CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void* pUser)

    cout << "lCommand=" << lCommand << endl;
    switch (lCommand)
    
    case COMM_THERMOMETRY_ALARM:
    
        cout << "case COMM_THERMOMETRY_ALARM" << endl;
        NET_DVR_THERMOMETRY_ALARM structTherometry = 0;
        memcpy(&structTherometry, pAlarmInfo, sizeof(NET_DVR_THERMOMETRY_ALARM));

        // 时间字符串
        struct tm t;   //tm结构指针
        time_t now;  //声明time_t类型变量
        time(&now);      //获取系统日期和时间
        localtime_s(&t, &now);   //获取当地日期和时间
        cout << "year:" << t.tm_year + 1900 << " month:" << t.tm_mon + 1 << " day:" << t.tm_mday
            << " hour:" << t.tm_hour << " minute:" << t.tm_min << " second:" << t.tm_sec;
        char chTime[128];
        sprintf_s(chTime, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
            t.tm_hour, t.tm_min, t.tm_sec);

        //保存报警抓拍图片
        if (structTherometry.dwThermalPicLen > 0 && structTherometry.pThermalPicBuff != NULL) 
            char cFilename[256] =  0 ;
            HANDLE hFile;
            DWORD dwReturn;
            sprintf_s(cFilename, "D:\\\\your_black_white_pic\\\\FireAlarm[%s][%s][%f][%d].jpg", gip, chTime, structTherometry.fCurrTemperature, structTherometry.byThermometryUnit);
            hFile = CreateFileA(cFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
            if (hFile == INVALID_HANDLE_VALUE)
            
                cout << "INVALID_HANDLE_VALUE " << INVALID_HANDLE_VALUE << endl;
                break;
            
            WriteFile(hFile, structTherometry.pThermalPicBuff, structTherometry.dwThermalPicLen, &dwReturn, NULL);
            CloseHandle(hFile);
            hFile = INVALID_HANDLE_VALUE;
            cout << "TherometryAlarm" << endl;
        
        // 保存可见光图片
        if (structTherometry.dwPicLen > 0 && structTherometry.pPicBuff != NULL) 
            char cFilename2[256] =  0 ;
            HANDLE hFile;
            DWORD dwReturn;
            sprintf_s(cFilename2, "D:\\\\yourVisibleLight\\\\FireAlarm[%s][%s][%f][%d].jpg", gip, chTime, structTherometry.fCurrTemperature, structTherometry.byThermometryUnit);
            hFile = CreateFileA(cFilename2, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
            if (hFile == INVALID_HANDLE_VALUE)
            
                cout << "INVALID_HANDLE_VALUE " << INVALID_HANDLE_VALUE << endl;
                break;
            
            WriteFile(hFile, structTherometry.pPicBuff, structTherometry.dwPicLen, &dwReturn, NULL);
            CloseHandle(hFile);
            hFile = INVALID_HANDLE_VALUE;
        
    
    break;
    default:
        printf("其他报警,报警信息类型: %d\\n", lCommand);
        break;
    

    return TRUE;


int main(int argc, char* argv[]) 
    cout << "Zhang Chao's program starts." << endl;
    if (argc < 2) 
        cout << "NoIP" << endl;
        return 0;
    
    char* ip = argv[1];
    gip = ip;
    cout << ip << endl;

    //---------------------------------------
    // 初始化
    NET_DVR_Init();
    char arr[] = "E:\\\\yourLog\\\\";
    char* logFileDir = arr;
    NET_DVR_SetLogToFile(3, logFileDir, FALSE);
    //设置连接时间与重连时间
    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); //设备IP地址
    struLoginInfo.wPort = xxx; //设备服务端口
    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 0;
    

    //设置报警回调函数
    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 0;
    

    Sleep(120000); //等待过程中,如果设备上传报警信息,在报警回调函数里面接收和处理报警信息

    //撤销布防上传通道
    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 0;
    

    //注销用户
    NET_DVR_Logout(lUserID);
    //释放SDK资源
    NET_DVR_Cleanup();
    return 0;



以上是关于173VS2022调试通过海康温度报警SDK的C++代码的主要内容,如果未能解决你的问题,请参考以下文章

163VS2022调试通过海康明眸门禁报警事件中的报警布防C++示例代码

163VS2022调试通过海康明眸门禁报警事件中的报警布防C++示例代码

166VS2022调试通过海康人脸抓拍SDK的C++代码

160VS2022调试通过海康摄像头烟火识别SDK

174Java解析文件名中的方括号表达式

174Java解析文件名中的方括号表达式