調用海康威視sdk獲取車牌號

環境: win10(10.0.16299.0)+ vs2017
sdk版本:CH-HCNetSDK_Win32_V5.3.2.15_build20171122
https://download.csdn.net/download/cngwj/10307129(工程已包含sdk下載後可直接運行)

一、首先確認攝像頭是否支持車輛檢測html


二、 啓用事件抓圖
ios


三、配置sdkapp

        下載sdk http://www.hikvision.com/cn/download_61.html函數

    


四、運行效果圖
ui



/************************************************************************
* Copyright(c) 2018
*
* File:		報警佈防_0.3.c
* Brief:	調用海康威視sdk獲取車牌號
* Version: 	0.3
* Author: 	一念無明
* Email:	zhunmeng@live.com
* Date:    	2018.02.26
* History:
2018.2.26 	調用海康威視sdk獲取車牌號
2018.3.7 	獲取設備系統時間
2018.3.12   保存車牌號到csv文件
2018.3.14	對識別到的車牌號進行白名單比對

************************************************************************/
//-------------------------------------------------------------------------
//頭文件

#include <stdio.h>
#include <iostream>
#include <fstream> 
#include <string>
#include <streambuf> 
#include "Windows.h"
#include "HCNetSDK.h"
#include <time.h>
#include <conio.h>//getch()函數用

using namespace std;


//參數聲明
int iNum = 0; 	//圖片名稱序號
LONG IUserID;	//攝像機設備
LONG IHandle = -1;//報警佈防/撤防;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;	//設備信息


char sDVRIP[20];	//抓拍攝像機設備IP地址
short wDVRPort = 8000;	//設備端口號
char sUserName[20];	//登陸的用戶名
char sPassword[20];	//用戶密碼
string carNum;//車牌號							
string LineByLine;//逐行讀取文件 


				  //---------------------------------------------------------------------------------
				  //函數聲明
void Init();//初始化
void Demo_SDK_Version(); //獲取sdk版本
void Connect();//設置鏈接事件與重連時間
void Htime();//獲取海康威視設備時間
bool Login(char *sDVRIP, short wDVRPort, char *sUserName, char *sPassword);//註冊攝像機設備
void CALLBACK MSesGCallback(LONG ICommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void *pUser);//報警回調函數
void SetMessageCallBack();//設置報警回調函數
void Whitelist();//白名單比對
void Blacklist();//黑名單比對
void SetupAlarm();//報警佈防
void CloseAlarm();//報警撤防
void OnExit(void);//退出
				  //---------------------------------------------------------------------------------------------------
				  //函數定義
				  //初始化
void Init()
{
	//獲取系統時間
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	cout << sys.wYear << "-" << sys.wMonth << "-" << sys.wDay << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << endl;

	cout << "IP:";
	cin >> sDVRIP; cout << endl;
	cout << "用戶名:";
	cin >> sUserName; cout << endl;
	cout << "密碼:";
	char c;
	for (int i = 0; (c = _getch()) != '\r'; i++) {
		sPassword[i] = c;
		putchar('*');
	}
	cout << endl;

	NET_DVR_Init();//初始化
	Demo_SDK_Version();//獲取 SDK  的版本號和 build  信息	
}

//設置鏈接事件與重連時間
void Connect()
{
	NET_DVR_SetConnectTime(2000, 1);
	NET_DVR_SetReconnect(10000, true);
}
//獲取海康威視設備時間
void Htime() {
	bool iRet;
	DWORD dwReturnLen;
	NET_DVR_TIME struParams = { 0 };

	iRet = NET_DVR_GetDVRConfig(IUserID, NET_DVR_GET_TIMECFG, 1, \
		&struParams, sizeof(NET_DVR_TIME), &dwReturnLen);
	if (!iRet)
	{
		printf("NET_DVR_GetDVRConfig NET_DVR_GET_TIMECFG  error.\n");
		NET_DVR_Logout(IUserID);
		NET_DVR_Cleanup();
	}



	printf("%d年%d月%d日%d:%d:%d\n", struParams.dwYear, struParams.dwMonth, struParams.dwDay, struParams.dwHour, struParams.dwMinute, struParams.dwSecond);
}

//註冊攝像機設備
bool Login(char *sDVRIP, short wDVRPort, char *sUserName, char *sPassword)
{
	IUserID = NET_DVR_Login_V30(sDVRIP, wDVRPort, sUserName, sPassword, &struDeviceInfo);

	if (IUserID < 0)
	{
		std::cout << "Login Failed! Error number:" << NET_DVR_GetLastError() << std::endl;
		NET_DVR_Cleanup();
		return false;
	}
	else
	{
		std::cout << "Login Successfully!" << std::endl;
		return true;
	}

}

//Demo_SDK_Version()海康威視sdk版本獲取函數
void Demo_SDK_Version()
{
	unsigned int uiVersion = NET_DVR_GetSDKBuildVersion();

	char strTemp[1024] = { 0 };
	sprintf_s(strTemp, "HCNetSDK V%d.%d.%d.%d\n", \
		(0xff000000 & uiVersion) >> 24, \
		(0x00ff0000 & uiVersion) >> 16, \
		(0x0000ff00 & uiVersion) >> 8, \
		(0x000000ff & uiVersion));
	printf(strTemp);
}

//定義異常消息回調函數
void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
	char tempbuf[256] = { 0 };
	switch (dwType)
	{
	case EXCEPTION_RECONNECT:    //預覽時重連  
		printf("----------reconnect--------%d\n", time(NULL));
		break;
	default:
		break;
	}
}


//報警回調函數
void CALLBACK MSesGCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser)
{

	ofstream oFile;//定義文件輸出流
	oFile.open("車牌號.csv", ofstream::app);    //打開要輸出的文件 	
											 //獲取系統時間
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	cout << sys.wYear << "-" << sys.wMonth << "-" << sys.wDay << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << endl;

	int i = 0;
	char filename[100];
	FILE *fSnapPic = NULL;
	FILE *fSnapPicPlate = NULL;
	//如下代碼僅供參考,實際應用中不建議在該回調函數中直接處理數據保存文件,
	//例如能夠使用消息的方式(PostMessage)在消息響應函數裏進行處理。
	switch (lCommand) {
		//交通抓拍結果(老報警消息)
	case COMM_UPLOAD_PLATE_RESULT: {
		NET_DVR_PLATE_RESULT struPlateResult = { 0 };
		memcpy(&struPlateResult, pAlarmInfo, sizeof(struPlateResult));
		printf("車牌號: %s\n", struPlateResult.struPlateInfo.sLicense);//車牌號		
																	// oFile << struPlateResult.struPlateInfo.sLicense << endl;//保存車牌號到csv文件		
		oFile << struPlateResult.struPlateInfo.sLicense << "," << sys.wYear << "-" << sys.wMonth << "-" << sys.wDay << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << endl;//保存車牌號到csv文件	
																																															//場景圖
		if (struPlateResult.dwPicLen != 0 && struPlateResult.byResultType == 1)
		{
			sprintf(filename, "./pic/%s.jpg", struPlateResult.struPlateInfo.sLicense);
			fSnapPic = fopen(filename, "wb");
			fwrite(struPlateResult.pBuffer1, struPlateResult.dwPicLen, 1, fSnapPic);
			iNum++;
			fclose(fSnapPic);
		}
		//車牌圖
		if (struPlateResult.dwPicPlateLen != 0 && struPlateResult.byResultType == 1)
		{
			sprintf(filename, "./pic/1/%s.jpg", struPlateResult.struPlateInfo.sLicense);
			fSnapPicPlate = fopen(filename, "wb");
			fwrite(struPlateResult.pBuffer1, struPlateResult.dwPicLen, 1, fSnapPicPlate);
			iNum++;
			fclose(fSnapPicPlate);
		}
		//其餘信息處理......
		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("車牌號: %s\n", struITSPlateResult.struPlateInfo.sLicense);//車牌號
			carNum = struITSPlateResult.struPlateInfo.sLicense;
			oFile << carNum << "," << sys.wYear << "-" << sys.wMonth << "-" << sys.wDay << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << endl;//保存車牌號到csv文件	
			if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0) && (struITSPlateResult.struPicInfo[i].byType == 1) || (struITSPlateResult.struPicInfo[i].byType == 2))
			{
				sprintf(filename, "./pic/%s_%d.jpg", struITSPlateResult.struPlateInfo.sLicense, i);
				fSnapPic = fopen(filename, "wb");
				fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen, 1, fSnapPic);
				iNum++;
				fclose(fSnapPic);
			}
			//車牌小圖片
			if ((struITSPlateResult.struPicInfo[i].dwDataLen != 0) && (struITSPlateResult.struPicInfo[i].byType == 0))
			{
				sprintf(filename, "./pic/1/%s_%d.jpg", struITSPlateResult.struPlateInfo.sLicense, i);
				fSnapPicPlate = fopen(filename, "wb");
				fwrite(struITSPlateResult.struPicInfo[i].pBuffer, struITSPlateResult.struPicInfo[i].dwDataLen, 1, \
					fSnapPicPlate);
				iNum++;
				fclose(fSnapPicPlate);
			}
			//其餘信息處理......
		}
		Whitelist();//白名單比對
		Blacklist();//黑名單比對
		break;
	}
	default: {
		std::cout << lCommand << endl;
		break;
	}
	}

	oFile.close();//關閉文件
	return;
}
//設置報警回調函數
void SetMessageCallBack()
{
	NET_DVR_SetDVRMessageCallBack_V30(MSesGCallback, NULL);
}
//報警佈防
void SetupAlarm()
{
	//啓動佈防
	NET_DVR_SETUPALARM_PARAM struSetupParam = { 0 };
	struSetupParam.dwSize = sizeof(NET_DVR_SETUPALARM_PARAM);


	struSetupParam.byAlarmInfoType = 1;//上傳報警信息類型:0-老報警信息(NET_DVR_PLATE_RESULT), 1-新報警信息(NET_ITS_PLATE_RESULT)
	struSetupParam.byLevel = 1;//佈防優先級:0- 一等級(高),1- 二等級(中),2- 三等級(低)
							   //bySupport 按位表示,值:0 - 上傳,1 - 不上傳;  bit0 - 表示二級佈防是否上傳圖片;


	IHandle = NET_DVR_SetupAlarmChan_V41(IUserID, &struSetupParam);//創建報警上傳通道,獲取報警等信息。
	if (IHandle < 0)
	{
		std::cout << "NET_DVR_SetupAlarmChan_V41 Failed! Error number:" << NET_DVR_GetLastError() << std::endl;
		NET_DVR_Logout(IUserID);
		NET_DVR_Cleanup();
		return;
	}
	std::cout << "\n" << endl;

}
//報警撤防
void CloseAlarm()
{
	//撤銷佈防上傳通道
	if (!NET_DVR_CloseAlarmChan_V30(IHandle))//佈防句柄IHandle
	{
		std::cout << "NET_DVR_CloseAlarmChan_V30 Failed! Error number:" << NET_DVR_GetLastError() << std::endl;
		NET_DVR_Logout(IUserID);
		NET_DVR_Cleanup();
		return;
	}
	IHandle = -1;//佈防句柄;
}
//退出
void OnExit(void)
{
	std::cout << "Begin exit..." << std::endl;

	//報警撤防
	CloseAlarm();

	//釋放相機
	NET_DVR_Logout(IUserID);//註銷用戶
	NET_DVR_Cleanup();//釋放SDK資源	
}
//白名單比對
void Whitelist() {

	ifstream iFile;
	iFile.open("白名單.csv", ios::in);
	if (!iFile.is_open())
		std::cout << "找不到文件";

	while (getline(iFile, LineByLine))
	{
		if (LineByLine.empty()) {
			continue;
		}
		else {
			size_t found = LineByLine.find(carNum.substr(4, 8));//carNum.substr(4, 8) 截取車牌號「藍新NF8202」爲NF8202
			if (found != std::string::npos) {
				std::cout << "白名單:" << LineByLine << '\n';
			}

		}
	}
	iFile.close();//關閉文件
}

//黑名單比對
void Blacklist() {

	ifstream iFile;
	iFile.open("黑名單.csv", ios::in);
	if (!iFile.is_open())
		std::cout << "找不到文件";

	while (getline(iFile, LineByLine))
	{
		if (LineByLine.empty()) {
			continue;
		}
		else {
			size_t found = LineByLine.find(carNum.substr(4, 8));//carNum.substr(4, 8) 截取車牌號「藍新NF8202」爲NF8202
			if (found != std::string::npos) {
				std::cout << "黑名單:" << LineByLine << '\n';
			}

		}
	}
	iFile.close();//關閉文件
}

int main()
{
	Init();//初始化sdk
	Connect();//設置鏈接事件與重連時間			  	
	Login(sDVRIP, wDVRPort, sUserName, sPassword);	//註冊設備
	Htime(); //獲取海康威視設備時間
	SetupAlarm();//佈防
	SetMessageCallBack();	//註冊報警回調函數 

	for (;;) {
		SetMessageCallBack();	//報警回調函數 				
		Sleep(500);

	}
	Sleep(-1);
	atexit(OnExit);//退出
	return 0;

}
相關文章
相關標籤/搜索