【Facial】Receiving Face Detection Alarm In Arming Mode

サンプルプログラム

//
// Device Network SDK (Facial)
// Alarm or Event Settings
// Configure Facial Detection Alarm
// Sample Code of Receiving Face Detection Alarm in Arming Mode
//
#include <stdio.h>
#include <iostream>
#include “Windows.h”
#include “HCNetSDK.h”

using namespace std;

//Macro definition of parsed time
#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_ALARM_FACE_DETECTION: //Facial detection alarm information
        {
            NET_DVR_FACE_DETECTION struFaceDetectionAlarm = {0};
            memcpy(&struFaceDetectionAlarm, pAlarmInfo, sizeof(NET_DVR_FACE_DETECTION));
            NET_DVR_TIME struAbsTime = {0};
            struAbsTime.dwYear = GET_YEAR(struFaceDetectionAlarm.dwAbsTime);
            struAbsTime.dwMonth = GET_MONTH(struFaceDetectionAlarm.dwAbsTime);
            struAbsTime.dwDay = GET_DAY(struFaceDetectionAlarm.dwAbsTime);
            struAbsTime.dwHour = GET_HOUR(struFaceDetectionAlarm.dwAbsTime);
            struAbsTime.dwMinute = GET_MINUTE(struFaceDetectionAlarm.dwAbsTime);

struAbsTime.dwSecond = GET_SECOND(struFaceDetectionAlarm.dwAbsTime);

            //Save captured scene picture
            if (struFaceDetectionAlarm.dwBackgroundPicLen > 0 && struFaceDetectionAlarm.pBackgroundPicpBuffer != NULL)
            {
                char cFilename[256] = {0};
                HANDLE hFile;
                DWORD dwReturn;
                char chTime[128];
                sprintf(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(cFilename, “FaceDetectionBackPic[%s][%s].jpg”,struFaceDetectionAlarm.struDevInfo.struDevIP.sIpV4, chTime);
                hFile = CreateFile(cFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
                if (hFile == INVALID_HANDLE_VALUE)
                {
                    break;
                }
                WriteFile(hFile, struFaceDetectionAlarm.pBackgroundPicpBuffer, struFaceDetectionAlarm.dwBackgroundPicLen, &dwReturn, NULL);
                CloseHandle(hFile);
                hFile = INVALID_HANDLE_VALUE;
}
                printf(“Face Detection Alarm[0x%x]: Abs[%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d] Dev[ip:%s,port:%d,ivmsChan:%d] \n”,\
                lCommand, struAbsTime.dwYear, struAbsTime.dwMonth, struAbsTime.dwDay, struAbsTime.dwHour, \
                struAbsTime.dwMinute, struAbsTime.dwSecond, struFaceDetectionAlarm.struDevInfo.struDevIP.sIpV4, \
                struFaceDetectionAlarm.struDevInfo.wPort, struFaceDetectionAlarm.struDevInfo.byIvmsChannel);
        }
        break;
        default:
        printf(“Other alarm, alarm type: 0x%x\n”, lCommand);
        break;
}
    return TRUE;

}

void main() {
    //—————————————
    // Initialize
    NET_DVR_Init();
    //Set connection time and reconnection time
    NET_DVR_SetConnectTime(2000, 1);

NET_DVR_SetReconnect(10000, true);

    //—————————————
    // Log in to device

LONG lUserID;

    //Login parameter, including device address, user name, and password.
    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
    struLoginInfo.bUseAsynLogin = 0; //Synchronous login mode
    strcpy(struLoginInfo.sDeviceAddress, “192.0.0.64”); //Device IP address
    struLoginInfo.wPort = 8000; //Device service port
    strcpy(struLoginInfo.sUserName, “admin”); //Device user name

strcpy(struLoginInfo.sPassword, “abcd1234”); //Device password

    //Device information, output parameters
    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;

}

    //Set alarm callback function

NET_DVR_SetDVRMessageCallBack_V31(MessageCallback, NULL);

    //Enable arming
    LONG lHandle;
    NET_DVR_SETUPALARM_PARAM  struAlarmParam={0};
    struAlarmParam.dwSize=sizeof(struAlarmParam);
    struAlarmParam.byFaceAlarmDetection = 1; //Facial detection alarm, upload the alarm information with the type of COMM_ALARM_FACE_DETECTION

//The other arming parameters are not supported.

    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);

During waiting process, if the device continues to upload alarm information, you can receive and handle the alarm in the callback function.

    //Disconnect the uploading channel
    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;

}

    //Log out
    NET_DVR_Logout(lUserID);
    //Release resources
    NET_DVR_Cleanup();
    return;
}

関連記事