呼叫海康威視sdk獲取車牌號

環境: win10(10.0.16299.0) vs2017
sdk版本:CH-HCNetSDK_Win32_V5.3.2.15_build20171122

https://download.csdn.net/download/cngwj/10307129(工程已包含sdk下載後可直接執行)

1、首先確認攝像頭是否支援車輛檢測

2、 啟用事件抓圖

3、配置sdk

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

    

4、執行效果圖

/************************************************************************
* Copyright(c) 2018
*
* File:		報警佈防_0.3.c
* Brief:	呼叫海康威視sdk獲取車牌號
* Version: 	0.3
* Author: 	一念無明
* Email:	[email protected]
* 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;
}