#include<cstdlib>
#include<cstring>
#include<iostream>
#include<Windows.h>
#include<HCNetSDK.h>
#include <plaympeg4.h>
#include <opencv2\opencv.hpp>
#include <time.h>
#include <conio.h>
using namespace std;
using namespace cv;
LONG nPort = -1;
volatile int gbHandling = 3;
void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
if (gbHandling)
{
gbHandling--;
return;
}
long lFrameType = pFrameInfo->nType;
if (lFrameType == T_YV12)
{
Mat pImg(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3);
Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, pBuf);
cvtColor(src, pImg, CV_YUV2BGR_YV12);
waitKey(1);
}
gbHandling = 3;
}
void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
switch (dwDataType)
{
case NET_DVR_SYSHEAD:
if (!PlayM4_GetPort(&nPort))
{
break;
}
if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME))
{
break;
}
if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 10 * 1024 * 1024))
{
break;
}
if (!PlayM4_Play(nPort, NULL))
{
break;
}
if (!PlayM4_SetDecCallBack(nPort, DecCBFun))
{
break;
}
}
break;
case NET_DVR_STREAMDATA:
if (dwBufSize > 0 && nPort != -1)
{
if (!PlayM4_InputData(nPort, pBuffer, dwBufSize))
{
cout << "error" << PlayM4_GetLastError(nPort) << endl;
break;
}
}
break;
default:
if (dwBufSize > 0 && nPort != -1)
{
if (!PlayM4_InputData(nPort, pBuffer, dwBufSize))
{
break;
}
}
break;
}
}
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;
}
}
DWORD HexToDecMa(DWORD wHex)
{
return (wHex / 4096) * 1000 + ((wHex % 4096) / 256) * 100 + ((wHex % 256) / 16) * 10 + (wHex % 16);
}
void OnLButtonUp(LONG lRealPlayHandle)
{
NET_DVR_PTZControl(lRealPlayHandle, PAN_RIGHT, 1);
NET_DVR_PTZControl(lRealPlayHandle, PAN_LEFT, 1);
NET_DVR_PTZControl(lRealPlayHandle, TILT_UP, 1);
NET_DVR_PTZControl(lRealPlayHandle, TILT_DOWN, 1);
}
void main()
{
NET_DVR_Init();
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30("192.168.6.100", 8000, "admin", "qwer1234", &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
LONG lRealPlayHandle;
cvNamedWindow("Mywindow", 0);
HWND h = (HWND)cvGetWindowHandle("Mywindow");
if (h == 0)
{
cout << "窗口创建失败" << endl;
}
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = h;
struPlayInfo.lChannel = 1;
struPlayInfo.dwStreamType = 0;
struPlayInfo.dwLinkMode = 0;
lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, fRealDataCallBack, NULL);
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V40 error\n");
printf("%d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
NET_DVR_PTZPOS m_ptzPosCurrent;
DWORD dwtmp;
while (1)
{
OnLButtonUp(lRealPlayHandle);
if (GetKeyState(VK_LEFT) < 0)
NET_DVR_PTZControl(lRealPlayHandle, PAN_LEFT, 0);
if (GetKeyState(VK_UP) < 0)
NET_DVR_PTZControl(lRealPlayHandle, TILT_UP, 0);
if (GetKeyState(VK_DOWN) < 0)
NET_DVR_PTZControl(lRealPlayHandle, TILT_DOWN, 0);
if (GetKeyState(VK_RIGHT) < 0)
NET_DVR_PTZControl(lRealPlayHandle, PAN_RIGHT, 0);
bool a = NET_DVR_GetDVRConfig(0, NET_DVR_GET_PTZPOS, 0, &m_ptzPosCurrent, sizeof(NET_DVR_PTZPOS), &dwtmp);
int m_iPara1 = HexToDecMa(m_ptzPosCurrent.wPanPos);
int m_iPara2 = HexToDecMa(m_ptzPosCurrent.wTiltPos);
int m_iPara3 = HexToDecMa(m_ptzPosCurrent.wZoomPos);
cout << "P" << m_iPara1 / 10 + 1 << endl;
cout << "T" << m_iPara2 / 10 + 1 << endl;
cout << "Z" << m_iPara3 / 10 << endl;
waitKey(1);
}
waitKey();
Sleep(-1);
NET_DVR_StopRealPlay(lRealPlayHandle);
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}