Set callback functions for getting the video data.
BOOL NET_DVR_SetDVRMessageCallBack_V50( int iIndex, MSGCallBack fMessageCallBack, void *pUser );
[IN] Callback function index No., which ranges from 0 to 15.
[IN] Callback function, see details in MSGCallBack.
[IN] User data.
Return TRUE for success, and return FALSE for failure.
If FALSE returned, call NET_DVR_GetLastError to get the error code.
This API supports setting multiple callback functions for different channels (up to 16 channels are supported) at same time, and the configured callback functions are distinguished by the index No.
All alarm/event information will be returned in each configured callback function, and you can distinguish the devices via the pAlarmInfo in the callback function (MSGCallBack).
#include <stdio.h> #include <iostream> #include "Windows.h" #include "HCNetSDK.h" using namespace std; int iNum=0; void CALLBACK MessageCallbackNo1(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser) { int i=0; char filename[100]; FILE *fSnapPic=NULL; FILE *fSnapPicPlate=NULL; //This sample code is for reference only. Actually, it is not recommended to process the data and save file in the callback function directly. //You'd better process the data in the message response funcion via message mode (PostMessage). switch(lCommand) { case COMM_ALARM: { NET_DVR_ALARMINFO struAlarmInfo; memcpy(&struAlarmInfo, pAlarmInfo, sizeof(NET_DVR_ALARMINFO)); switch (struAlarmInfo.dwAlarmType) { case 3: //Motion detection alarm for (i=0; i<16; i++) //#define MAX_CHANNUM 16 //The maximum number of channels { if (struAlarmInfo.dwChannel[i] == 1) { printf("Channel Number with Motion Detection Alarm %d\n", i+1); } } break; default: break; } break; } case COMM_UPLOAD_PLATE_RESULT: { NET_DVR_PLATE_RESULT struPlateResult={0}; memcpy(&struPlateResult, pAlarmInfo, sizeof(struPlateResult)); printf("License Plate Number: %s\n", struPlateResult.struPlateInfo.sLicense);//License plate number switch(struPlateResult.struPlateInfo.byColor)//License plate color { case VCA_BLUE_PLATE: printf("Vehicle Color: Blue\n"); break; case VCA_YELLOW_PLATE: printf("Vehicle Color: Yellow\n"); break; case VCA_WHITE_PLATE: printf("Vehicle Color: White\n"); break; case VCA_BLACK_PLATE: printf("Vehicle Color: Black\n"); break; default: break; } //Scene picture if (struPlateResult.dwPicLen != 0 && struPlateResult.byResultType == 1 ) { sprintf(filename,"testpic_%d.jpg",iNum); fSnapPic=fopen(filename,"wb"); fwrite(struPlateResult.pBuffer1,struPlateResult.dwPicLen,1,fSnapPic); iNum++; fclose(fSnapPic); } //License plate picture if (struPlateResult.dwPicPlateLen != 0 && struPlateResult.byResultType == 1) { sprintf(filename,"testPicPlate_%d.jpg",iNum); fSnapPicPlate=fopen(filename,"wb"); fwrite(struPlateResult.pBuffer1,struPlateResult.dwPicLen,1,fSnapPicPlate); iNum++; fclose(fSnapPicPlate); } //Processing other data... break; } case COMM_ITS_PLATE_RESULT: { NET_ITS_PLATE_RESULT struITSPlateResult={0}; memcpy(&struITSPlateResult, pAlarmInfo, sizeof(struITSPlateResult)); for (i=0;i<struITSPlateResult.dwPicNum;i++) { printf("License Plate Number: %s\n", struITSPlateResult.struPlateInfo.sLicense);//License plate number switch(struITSPlateResult.struPlateInfo.byColor)//License plate color { case VCA_BLUE_PLATE: printf("Vehicle Color: Blue\n"); break; case VCA_YELLOW_PLATE: printf("Vehicle Color: Yellow\n"); break; case VCA_WHITE_PLATE: printf("Vehicle Color: White\n"); break; case VCA_BLACK_PLATE: printf("Vehicle Color: Black\n"); break; default: break; } //Save scene picture if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0)&&(struITSPlateResult.struPicInfo[i].byType== 1)||(struITSPlateResult.struPicInfo[i].byType == 2)) { sprintf(filename,"testITSpic%d_%d.jpg",iNum,i); fSnapPic=fopen(filename,"wb"); fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen,1,fSnapPic); iNum++; fclose(fSnapPic); } //License plate thumbnails if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0)&&(struITSPlateResult.struPicInfo[i].byType == 0)) { sprintf(filename,"testPicPlate%d_%d.jpg",iNum,i); fSnapPicPlate=fopen(filename,"wb"); fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen, 1, \ fSnapPicPlate); iNum++; fclose(fSnapPicPlate); } //Processing other data... } break; } default: break; } } void CALLBACK MessageCallbackNo2(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser) { int i=0; char filename[100]; FILE *fSnapPic=NULL; FILE *fSnapPicPlate=NULL; //This sample code is for reference only. Actually, it is not recommended to process the data and save file in the callback function directly. //You'd better process the data in the message response funcion via message mode (PostMessage). switch(lCommand) { case COMM_ALARM: { NET_DVR_ALARMINFO struAlarmInfo; memcpy(&struAlarmInfo, pAlarmInfo, sizeof(NET_DVR_ALARMINFO)); switch (struAlarmInfo.dwAlarmType) { case 3: //Motion detection alarm for (i=0; i<16; i++) //#define MAX_CHANNUM 16 //The maximum number of channel { if (struAlarmInfo.dwChannel[i] == 1) { printf("Channel No. with Motion Detection Alarm %d\n", i+1); } } break; default: break; } break; } case COMM_UPLOAD_PLATE_RESULT: { NET_DVR_PLATE_RESULT struPlateResult={0}; memcpy(&struPlateResult, pAlarmInfo, sizeof(struPlateResult)); printf("License Plate Number: %s\n", struPlateResult.struPlateInfo.sLicense);//License plate number switch(struPlateResult.struPlateInfo.byColor)//License plate color { case VCA_BLUE_PLATE: printf("Vehicle Color: Blue\n"); break; case VCA_YELLOW_PLATE: printf("Vehicle Color: Yellow\n"); break; case VCA_WHITE_PLATE: printf("Vehicle color: White\n"); break; case VCA_BLACK_PLATE: printf("Vehicle Color: Black\n"); break; default: break; } //Scene picture if (struPlateResult.dwPicLen != 0 && struPlateResult.byResultType == 1 ) { sprintf(filename,"testpic_%d.jpg",iNum); fSnapPic=fopen(filename,"wb"); fwrite(struPlateResult.pBuffer1,struPlateResult.dwPicLen,1,fSnapPic); iNum++; fclose(fSnapPic); } //License plate picture if (struPlateResult.dwPicPlateLen != 0 && struPlateResult.byResultType == 1) { sprintf(filename,"testPicPlate_%d.jpg",iNum); fSnapPicPlate=fopen(filename,"wb"); fwrite(struPlateResult.pBuffer1,struPlateResult.dwPicLen,1,fSnapPicPlate); iNum++; fclose(fSnapPicPlate); } //Processing other data... break; } case COMM_ITS_PLATE_RESULT: { NET_ITS_PLATE_RESULT struITSPlateResult={0}; memcpy(&struITSPlateResult, pAlarmInfo, sizeof(struITSPlateResult)); for (i=0;i<struITSPlateResult.dwPicNum;i++) { printf("License Plate Number: %s\n", struITSPlateResult.struPlateInfo.sLicense);//License plate number switch(struITSPlateResult.struPlateInfo.byColor)//License plate color { case VCA_BLUE_PLATE: printf("Vehicle Color: Blue\n"); break; case VCA_YELLOW_PLATE: printf("Vehicle Color: Yellow\n"); break; case VCA_WHITE_PLATE: printf("Vehicle Color: White\n"); break; case VCA_BLACK_PLATE: printf("Vehicle Color: Black\n"); break; default: break; } //Save scene picture if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0)&&(struITSPlateResult.struPicInfo[i].byType== 1)||(struITSPlateResult.struPicInfo[i].byType == 2)) { sprintf(filename,"testITSpic%d_%d.jpg",iNum,i); fSnapPic=fopen(filename,"wb"); fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen,1,fSnapPic); iNum++; fclose(fSnapPic); } //License plate thumbnails if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0)&&(struITSPlateResult.struPicInfo[i].byType == 0)) { sprintf(filename,"testPicPlate%d_%d.jpg",iNum,i); fSnapPicPlate=fopen(filename,"wb"); fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen, 1, \ fSnapPicPlate); iNum++; fclose(fSnapPicPlate); } //Processing other data... } break; } default: break; } } void main() { //--------------------------------------- //Initialize NET_DVR_Init(); //Set the connection time and reconnection time NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); //--------------------------------------- //Log in to device LONG lUserID; NET_DVR_DEVICEINFO_V30 struDeviceInfo; lUserID = NET_DVR_Login_V30("172.0.0.100", 8000, "admin", "12345", &struDeviceInfo); if (lUserID < 0) { printf("Login error, %d\n", NET_DVR_GetLastError()); NET_DVR_Cleanup(); return; } //Set alarm callback function NET_DVR_SetDVRMessageCallBack_V50(0, MessageCallbackNo1, NULL); NET_DVR_SetDVRMessageCallBack_V50(1, MessageCallbackNo2, NULL); //Enable arming NET_DVR_SETUPALARM_PARAM struSetupParam={0}; struSetupParam.dwSize=sizeof(NET_DVR_SETUPALARM_PARAM); //Alarm information type to upload: 0-History Alarm (NET_DVR_PLATE_RESULT), 1-Real-Time Alarm (NET_ITS_PLATE_RESULT) struSetupParam.byAlarmInfoType=1; //Arming Level: Level-2 arming (for traffic device) struSetupParam.byLevel=1; LONG lHandle = NET_DVR_SetupAlarmChan_V41(lUserID,&struSetupParam); if (lHandle < 0) { printf("NET_DVR_SetupAlarmChan_V41 error, %d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return; } Sleep(20000); //Disarm 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; } //User logout NET_DVR_Logout(lUserID); //Release SDK resource NET_DVR_Cleanup(); return; }