Qt控制海康NVR设备预览、回放等功能

调用海康SDK开发硬盘录像机回放和实时预览的功能,能够实现实时预览、回放控制等
头文件

/*
* @Title: 海康威视NVR
* @Description: 海康威视NVR设备预览、回放
* @author: hfjw
* @date: 2024-08-22
* @version: V1.1
*/
#pragma once
#include "HCNetSDK.h"
#include <QObject>
#include <QThread>
#include <qtimer.h>

typedef struct HKTime
{
	//开始时间
	int start_year;
	int start_month;
	int start_day;
	int start_hour;
	int start_minute;
	int start_second;
	//结束时间
	int end_year;
	int end_month;
	int end_day;
	int end_hour;
	int end_minute;
	int end_second;
};

class HKControler : public QObject
{
	Q_OBJECT
public:
	HKControler(HWND window);
	bool NVR_Init();
	bool NVR_Login(QString NVR_IP,quint16 NVR_Port,QString NVR_User,QString NVR_Password);

	bool NVR_StartLive();
	void NVR_StopLive();

	void NVR_FindFile();

	void NVR_ReviewByTime();
	void NVR_StopReviewBytime();

	void NVR_PauseReview();
	void NVR_RestartReview();

	void NVR_SetReviewTime(NET_DVR_TIME tempTime);

	void NVR_DragStart(NET_DVR_TIME_EX temp);
	void NVR_DragStop(NET_DVR_TIME_EX temp);

	void NVR_PlayFast();
	void NVR_PlaySlow();

	static void CALLBACK fPlayDataCallBack_V40(LONG lPlayHandle, DWORD dwDataType, BYTE* pBuffer, DWORD dwBufSize, void* pUser);
	
	HKTime NVR_GetNVRTime();

	void NVR_SetNVRStartDateTime(int year,int month,int day,int hour,int minute,int second);
	void NVR_SetNVRStopDateTime(int year, int month, int day, int hour, int minute, int second);

	void NVR_SetNVRStartTime(int hour, int minute, int second);
	void NVR_SetNVRStopTime(int hour, int minute, int second);
	
	int NVR_GetNVRReviewPassTime();

signals:
	void signal_hkCallback();
private:
	LONG lUserID;
	LONG lRealPlayHandle;
	int hPlayback;
	NET_DVR_PREVIEWINFO struPlayInfo;
	NET_DVR_USER_LOGIN_INFO struLoginInfo;
	NET_DVR_DEVICEINFO_V40 struDeviceInfoV40;
	HWND displayWindow;
	int xunHuanCode;
	bool isCallBackGo;
	QTimer* timerHK;
	bool hknvr_startReview;

	int hknvr_passTime;
	HKTime hknvr_time;
public slots:
	void TimerSendNVR();
};

源文件

#include "HKControler.h"
#include <qmessagebox.h>
#include <qdebug.h>
#include <qtimer.h>

HKControler* pointerHKControler;

HKControler::HKControler(HWND window)
{
	displayWindow = window;
	memset(&hknvr_time, 0, sizeof(hknvr_time));
	pointerHKControler = this;
	hknvr_passTime = 0;
	isCallBackGo = false;
	xunHuanCode = 0;
	hknvr_startReview = false;

	timerHK = new QTimer(this);
	timerHK->setTimerType(Qt::CoarseTimer);
	timerHK->start(40);
	connect(timerHK, &QTimer::timeout, this, &HKControler::TimerSendNVR);
}

bool HKControler::NVR_Init()
{
	bool initOK = false;
	// 初始化
	initOK = NET_DVR_Init();
	if (initOK)
	{
		//设置连接时间与重连时间
		NET_DVR_SetConnectTime(2000, 1);
		NET_DVR_SetReconnect(10000, true);
		
		return true;
	}
	else
	{
		return false;
	}	
}

bool HKControler::NVR_Login(QString NVR_IP, quint16 NVR_Port, QString NVR_User, QString NVR_Password)
{
	//设备登录参数
	memset(&struLoginInfo, 0x00, sizeof(struLoginInfo));
	memset(&struDeviceInfoV40, 0x00, sizeof(struDeviceInfoV40));
	struLoginInfo.bUseAsynLogin = 0;						//同步登录方式
	strcpy_s(struLoginInfo.sDeviceAddress, NVR_IP.toUtf8().constData());	//设备IP地址
	struLoginInfo.wPort = NVR_Port;								//设备服务端口
	strcpy_s(struLoginInfo.sUserName, NVR_User.toUtf8().constData());				//设备登录用户名
	strcpy_s(struLoginInfo.sPassword, NVR_Password.toUtf8().constData());			//设备登录密码


	lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);

	//int StartChanNum = struDeviceInfoV40.struDeviceV30.byStartDChan;//起始数字通道

	if (lUserID < 0)
	{
		QMessageBox msg;
		QString temps;
		temps = QString("%1%2").arg("Login failed, error code: \n").arg(NET_DVR_GetLastError());
		msg.setText(temps);
		msg.exec();
		NET_DVR_Cleanup();
		return false;
	}
	else
	{
		return true;
	}
	
}

bool HKControler::NVR_StartLive()
{
	memset(&struPlayInfo, 0, sizeof(struPlayInfo));

	struPlayInfo.hPlayWnd = displayWindow;				//需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空
	struPlayInfo.lChannel = 33;					//预览通道号
	struPlayInfo.dwStreamType = 0;				//0-主码流,1-子码流,2-码流 3,3-码流 4,以此类推
	struPlayInfo.dwLinkMode = 0;				//0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTP
	struPlayInfo.bBlocked = 0;					//0- 非阻塞取流,1- 阻塞取流
	
	lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, nullptr, nullptr);
	if (lRealPlayHandle < 0)
	{
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return false;
	}
	else
	{
		return true;
	}
	
}

void HKControler::NVR_StopLive()
{
	//NET_NVR_STOP
	if (!NET_DVR_StopRealPlay(lRealPlayHandle))
	{
		qDebug() << "failed stop live";
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}

}

void HKControler::NVR_FindFile()
{
	  //查询条件
    NET_DVR_FILECOND_V50 struFileCond = { 0 };
    struFileCond.dwFileType = 0xFF;
    struFileCond.struStreamID.dwSize = sizeof(NET_DVR_STREAM_INFO);
    struFileCond.struStreamID.dwChannel = 33;
    struFileCond.byIsLocked = 0xFF;
    struFileCond.byNeedCard = 0;
    struFileCond.struStartTime.wYear = 2000;
    struFileCond.struStartTime.byMonth = 1;
    struFileCond.struStartTime.byDay = 1;
    struFileCond.struStartTime.byHour = 0;
    struFileCond.struStartTime.byMinute = 0;
    struFileCond.struStartTime.bySecond = 0;
    struFileCond.struStopTime.wYear = 2050;
    struFileCond.struStopTime.byMonth = 1;
    struFileCond.struStopTime.byDay = 1;
    struFileCond.struStopTime.byHour = 23;
    struFileCond.struStopTime.byMinute = 59;
    struFileCond.struStopTime.bySecond = 59;

    //查找录像文件
    int lFindHandle = NET_DVR_FindFile_V50(lUserID, &struFileCond);
    if (lFindHandle < 0)
    {
        return;
    }
    //逐个获取查询文件结果
    NET_DVR_FINDDATA_V50 struFileData;
	QVector<QString> fileName;
	QVector<QString> fileName_Time;
    int iFileNum = 0;
    while (true)
    {
        int result = NET_DVR_FindNextFile_V50(lFindHandle, &struFileData);
        if (result == NET_DVR_ISFINDING)
        {
            continue;
        }
        else if (result == NET_DVR_FILE_SUCCESS)
        {
            iFileNum++;
			QString temps,temps1;
			temps = QString("%1%2 %3%4 %5%6").arg("find file no:").arg(iFileNum).arg("find file name:").arg(struFileData.sFileName);
			temps1 = QString::fromLocal8Bit("%1年%2月%3日 %4时%5分%6秒").arg(struFileData.struStartTime.wYear).arg(struFileData.struStartTime.byMonth).arg(struFileData.struStartTime.byDay)
				.arg(struFileData.struStartTime.byHour).arg(struFileData.struStartTime.byMinute).arg(struFileData.struStartTime.bySecond);
			fileName.push_back(struFileData.sFileName);
			fileName_Time.push_back(temps1);
            continue;
        }
        else if (result == NET_DVR_FILE_NOFIND || result == NET_DVR_NOMOREFILE)
        {
            break;
        }
        else
        {
            break;
        }
    }

	if (lFindHandle >= 0)
	{
		NET_DVR_FindClose_V30(lFindHandle);
	}


}
//根据时间回放
void HKControler::NVR_ReviewByTime()
{
	isCallBackGo = true;
	NET_DVR_VOD_PARA_V50 struVodPara = { 0 };
	struVodPara.dwSize = sizeof(struVodPara);
	struVodPara.struIDInfo.dwSize = sizeof(NET_DVR_STREAM_INFO);
	struVodPara.struIDInfo.dwChannel = 33;
	struVodPara.hWnd = displayWindow;
	struVodPara.byDisplayBufNum = 30;
	struVodPara.byOptimalStreamType = 1;
	//开始时间
	struVodPara.struBeginTime.wYear		= hknvr_time.start_year;
	struVodPara.struBeginTime.byMonth	= hknvr_time.start_month;
	struVodPara.struBeginTime.byDay		= hknvr_time.start_day;
	struVodPara.struBeginTime.byHour	= hknvr_time.start_hour;
	struVodPara.struBeginTime.byMinute	= hknvr_time.start_minute;
	struVodPara.struBeginTime.bySecond	= hknvr_time.start_second;
	//结束时间
	struVodPara.struEndTime.wYear		= hknvr_time.end_year;
	struVodPara.struEndTime.byMonth		= hknvr_time.end_month;
	struVodPara.struEndTime.byDay		= hknvr_time.end_day;
	struVodPara.struEndTime.byHour		= hknvr_time.end_hour;
	struVodPara.struEndTime.byMinute	= hknvr_time.end_minute;
	struVodPara.struEndTime.bySecond	= hknvr_time.end_second;
	//按时间回放
	hPlayback = NET_DVR_PlayBackByTime_V50(lUserID, &struVodPara);
	if (hPlayback < 0)
	{
		QMessageBox msg;
		QString temps;
		temps = QString("%1%2").arg(QString::fromLocal8Bit("回放错误,错误信息:\n")).arg(NET_DVR_GetLastError());
		msg.setText(temps);
		msg.exec();

		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
	//开始
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART, NULL, 0, NULL, NULL))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
	hknvr_startReview = true;
}

void HKControler::NVR_StopReviewBytime()
{
	//结束
	isCallBackGo = false;
	hknvr_startReview = false;
	if (!NET_DVR_StopPlayBack(hPlayback))
	{
		printf("failed to stop file [%d]\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
}
//停止回放
void HKControler::NVR_PauseReview()
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYPAUSE, NULL, 0, NULL, NULL))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
}
void HKControler::NVR_RestartReview()
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYRESTART, NULL, 0, NULL, NULL))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
		NET_DVR_Logout(lUserID);
		NET_DVR_Cleanup();
		return;
	}
}
void HKControler::NVR_SetReviewTime(NET_DVR_TIME tempTime)
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSETTIME, &tempTime, sizeof(tempTime), NULL, 0))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
		qDebug() << "error";
	}
}
void HKControler::NVR_DragStart(NET_DVR_TIME_EX temp)
{
	qDebug() << temp.byHour << temp.byMinute << temp.bySecond;
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_VOD_DRAG_ING, &temp, sizeof(temp), NULL, 0))
	{
		qDebug() << "error";
	}
}

void HKControler::NVR_DragStop(NET_DVR_TIME_EX temp)
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_VOD_DRAG_END, &temp, sizeof(temp), NULL, 0))
	{
		qDebug() << "error";
	}
}
void HKControler::NVR_PlayFast()
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYFAST, NULL, 0, NULL, NULL))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
	
		return;
	}
}
void HKControler::NVR_PlaySlow()
{
	if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSLOW, NULL, 0, NULL, NULL))
	{
		printf("play back control failed [%d]\n", NET_DVR_GetLastError());
		
		return;
	}
}
//回调函数
void HKControler::fPlayDataCallBack_V40(LONG lPlayHandle, DWORD dwDataType, BYTE* pBuffer, DWORD dwBufSize, void* pUser)
{
	
}

HKTime HKControler::NVR_GetNVRTime()
{
	return hknvr_time;
}

void HKControler::NVR_SetNVRStartDateTime(int year, int month, int day, int hour, int minute, int second)
{
	hknvr_time.start_year = year;
	hknvr_time.start_month = month;
	hknvr_time.start_day = day;
	hknvr_time.start_hour = hour;
	hknvr_time.start_minute = minute;
	hknvr_time.start_second = second;
}

void HKControler::NVR_SetNVRStopDateTime(int year, int month, int day, int hour, int minute, int second)
{
	hknvr_time.end_year		= year;
	hknvr_time.end_month	= month;
	hknvr_time.end_day		= day;
	hknvr_time.end_hour		= hour;
	hknvr_time.end_minute	= minute;
	hknvr_time.end_second	= second;
}

void HKControler::NVR_SetNVRStartTime(int hour, int minute, int second)
{
	hknvr_time.start_hour = hour;
	hknvr_time.start_minute = minute;
	hknvr_time.start_second = second;
}

void HKControler::NVR_SetNVRStopTime(int hour, int minute, int second)
{
	hknvr_time.end_hour = hour;
	hknvr_time.end_minute = minute;
	hknvr_time.end_second = second;
}

int HKControler::NVR_GetNVRReviewPassTime()
{
	return hknvr_passTime;
}

void HKControler::TimerSendNVR()
{
	if (hknvr_startReview)
	{	
		DWORD frame;
		if (NET_DVR_PlayBackControl_V40(pointerHKControler->hPlayback, NET_DVR_PLAYGETTIME, NULL, 0, &frame, NULL))
		{
			pointerHKControler->hknvr_passTime = (int)frame;
		}
		emit pointerHKControler->signal_hkCallback();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值