#pragma once
#include "HCCamera.h"
#include <time.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <opencv2\opencv.hpp>
#include <plaympeg4.h>
#include <queue>
#include <PoseDetcetion.h>
#if WINDOWS
#include <HCNetSDK.h>
using namespace std;
using namespace cv;
#define USECOLOR 1
int iPicNum = 0;//Set channel NO.
LONG nPort = -1;
HWND hWnd = NULL;
LONG lRealPlayHandle;
LONG lUserID;
void yv12toYUV(char *outYuv, char *inYv12, int width, int height, int widthStep)
{
int col, row;
unsigned int Y, U, V;
int tmp;
int idx;
for (row = 0; row < height; row++)
{
idx = row * widthStep;
int rowptr = row * width;
for (col = 0; col < width; col++)
{
tmp = (row / 2)*(width / 2) + (col / 2);
Y = (unsigned int)inYv12[row*width + col];
U = (unsigned int)inYv12[width*height + width * height / 4 + tmp];
V = (unsigned int)inYv12[width*height + tmp];
outYuv[idx + col * 3] = Y;
outYuv[idx + col * 3 + 1] = U;
outYuv[idx + col * 3 + 2] = V;
}
}
}
void CALLBACK HCCamera_DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
long lFrameType = pFrameInfo->nType;
if (lFrameType == T_YV12)
{
#if USECOLOR
//int start = clock();
IplImage* pImgYCrCb = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);//得到图像的Y分量
yv12toYUV(pImgYCrCb->imageData, pBuf, pFrameInfo->nWidth, pFrameInfo->nHeight, pImgYCrCb->widthStep);//得到全部RGB图像
IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);
cvCvtColor(pImgYCrCb, pImg, CV_YCrCb2RGB);
//int end = clock();
#else
IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 1);
memcpy(pImg->imageData, pBuf, pFrameInfo->nWidth*pFrameInfo->nHeight);
#endif
cv::Mat image = cv::cvarrToMatND(pImg);
cv::Mat re_image;
resize(image, re_image, Size(1400, 900), CV_INTER_LINEAR);
cv::cvtColor(re_image,re_image,COLOR_BGR2RGB);
if (PoseDetcetion::image_msg_queue.size() < 10) {
PoseDetcetion::lock = true;
PoseDetcetion::image_msg_queue.push(re_image);
PoseDetcetion::lock = false;
}
#if USECOLOR
cvReleaseImage(&pImgYCrCb);
cvReleaseImage(&pImg);
#else
cvReleaseImage(&pImg);
#endif
//此时是YV12格式的视频数据,保存在pBuf中,可以fwrite(pBuf,nSize,1,Videofile);
//fwrite(pBuf,nSize,1,fp);
}
/***************
else if (lFrameType ==T_AUDIO16)
{
//此时是音频数据,数据保存在pBuf中,可以fwrite(pBuf,nSize,1,Audiofile);
}
else
{
}
*******************/
}
///实时流回调
void CALLBACK HCCamera_fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
DWORD dRet;
PoseDetcetion *det = (PoseDetcetion*)pUser;
switch (dwDataType)
{
case NET_DVR_SYSHEAD: //系统头
if (!PlayM4_GetPort(&nPort)) //获取播放库未使用的通道号
{
break;
}
if (dwBufSize > 0)
{
if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//设置解码回调函数 只解码不显示
if (!PlayM4_SetDecCallBack(nPort, HCCamera_DecCBFun))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//设置解码回调函数 解码且显示
//if (!PlayM4_SetDecCallBackEx(nPort,DecCBFun,NULL,NULL))
//{
// dRet=PlayM4_GetLastError(nPort);
// break;
//}
//打开视频解码
if (!PlayM4_Play(nPort, hWnd))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//打开音频解码, 需要码流是复合流
if (!PlayM4_PlaySound(nPort))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
}
break;
case NET_DVR_STREAMDATA: //码流数据
if (dwBufSize > 0 && nPort != -1)
{
BOOL inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
while (!inData)
{
Sleep(10);
inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
//OutputDebugString(L"PlayM4_InputData failed \n");
}
}
break;
}
}
void CALLBACK HCCamera_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;
}
}
BOOL HCCamera_login(char* ipaddress, char* port, char* username, char* password) {
NET_DVR_Init();
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
int int32_port = stoi(port);
lUserID = NET_DVR_Login_V30(ipaddress, int32_port, username, password, &struDeviceInfo);
//userid = &lUserID;
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return FALSE;
}
NET_DVR_SetExceptionCallBack_V30(0, NULL, HCCamera_g_ExceptionCallBack, NULL);
NET_DVR_CLIENTINFO ClientInfo;
ClientInfo.lChannel = 1;
ClientInfo.hPlayWnd = NULL;
ClientInfo.lLinkMode = 0;
ClientInfo.sMultiCastIP = NULL;
lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &ClientInfo, HCCamera_fRealDataCallBack, NULL, TRUE);
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V30 failed! Error number: %d\n", NET_DVR_GetLastError());
return FALSE;
}
Sleep(-1);
if (!NET_DVR_StopRealPlay(lRealPlayHandle))
{
printf("NET_DVR_StopRealPlay error! Error number: %d\n", NET_DVR_GetLastError());
return FALSE;
}
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
}
BOOL HCCamera_logout() {
if (!NET_DVR_StopRealPlay(lRealPlayHandle))
{
return FALSE;
}
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return TRUE;
}
#endif // 0
Save Camera Document
猜你喜欢
转载自my.oschina.net/VenusV/blog/1790676
今日推荐
周排行