大华相机YUV数据通过opencv转换成rgb, 叠加字符显示

#include 
#include 
#include 
#include 
#include 

#define PLAYPORT		0
#define IMAGE_HEIGHT	        720
#define IMAGE_WIDTH		1280
#define OSD_MAX_ROW		10
#define OSD_MAX_STR		80

HANDLE mutex;

typedef struct OSDfont{
	int fontSize;
	int rowStart;
	int rowWidth;
	int colStart;
	int colWidth;
	int fontSpace;
	int color_r;
	int color_g;
	int color_b;
}OSDFONT;

OSDFONT osdfont{30, 1, 60, 1, 20, 1, 255, 255, 255};
Cv310Text* osdText;

char osdRow[OSD_MAX_ROW];
char osdStr[OSD_MAX_ROW][OSD_MAX_STR];

void __stdcall DisConnectFunc(LLONG lLoginID, char* pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
    //disconnect...
}

void __stdcall HaveReConnect(LLONG lLoginID, char* pchDVRIP, LONG nDVRPort, LDWORD dwUser)
{
    //reconnect...
}

void CALLBACK RealDataCB(
	LLONG  lRealHandle,
	DWORD  dwDataType,
	BYTE  *pBuffer,
	DWORD  dwBufSize,
	LDWORD dwUser)	
{
	if (0 == dwDataType && dwBufSize > 0)
	{
		PLAY_InputData(PLAYPORT, pBuffer, dwBufSize);
	}
}

void CALLBACK DecCBFun(LONG nPort, char * pBuf, LONG nSize, FRAME_INFO * pFrameInfo, void* pUserData, LONG nReserved2)
{
	if (pFrameInfo->nType == T_IYUV){
		cv::Mat yuv(IMAGE_HEIGHT + IMAGE_HEIGHT / 2, IMAGE_WIDTH, CV_8UC1, pBuf);	//图片的宽高要与视频分辨率对应
		cv::Mat bgr;
		WaitForSingleObject(mutex, INFINITE);
		cv::cvtColor(yuv, bgr, CV_YUV2RGB_YV12);
		for (int i = 0; i < OSD_MAX_ROW; i++){   //通过Cv310Text进行叠加
			if (1 == osdRow[i])
				osdText->putText(bgr, osdStr[i], cv::Point((osdfont.colStart + 2) * osdfont.colWidth, (osdfont.rowStart + i) * osdfont.rowWidth), cv::Scalar(osdfont.color_b, osdfont.color_g, osdfont.color_r));
		}
		//cv::imshow("video", bgr);
		//cv::waitKey(1);
		ReleaseMutex(mutex);
	}
}

int main()
{
	osdText = new Cv310Text("字体.ttf");

	CvScalar size;
	size.val[0] = osdfont.fontSize;
	size.val[1] = 0.5;
	size.val[2] = osdfont.fontSpace * 1.0 / 10;

	float p = 1.0;
	osdText->setFont(NULL, &size, NULL, &p);

	for(int i = 0; i < OSD_MAX_ROW; i++){
		osdRow[i] = 1;
		strncpy(osdStr, "叠加测试ABCDE12345", OSD_MAX_STR);
	}
	
	const char* ip = "192.168.1.108";
	int port = 37777;
	const char* user = "admin";
	const char* passwd = "admin";
	
	PLAY_OpenStream(PLAYPORT, NULL, 0,100 * 1024);
	PLAY_SetDecCallBack(PLAYPORT, DecCBFun);
	PLAY_Play(PLAYPORT, NULL);
	
        CLIENT_Init(DisConnectFunc, 0);
        CLIENT_SetAutoReconnect(&HaveReConnect, 0);

	NET_PARAM stuNetParam = { 0 };
	stuNetParam.nConnectTime = 3000;
	CLIENT_SetNetworkParam(&stuNetParam);

	NET_DEVICEINFO_Ex stuDevInfoEx = { 0 };
	int nError = 0;

	LONG hLogin = CLIENT_LoginEx2(ip, port, user, passwd, EM_LOGIN_SPEC_CAP_TCP, NULL, &stuDevInfoEx, &nError);
	if (hLogin == 0)
	{
		std::cout << "CLIENT_LoginEx2 GetLastError: " << CLIENT_GetLastError() << std::endl;
		return 1;
	}

	LONG hPlay = CLIENT_RealPlay(hLogin, 0, NULL);
	if (0 == hPlay){
		std::cout << "CLIENT_RealPlay GetLastError " << CLIENT_GetLastError() << std::endl;
		CLIENT_Logout(hLogin);
		CLIENT_Cleanup();
		return 1;
	}
	if (FALSE == CLIENT_SetRealDataCallBack(hPlay, RealDataCB, NULL)){
		std::cout << "CLIENT_SetRealDataCallBack GetLastError " << CLIENT_GetLastError() << std::endl;
		CLIENT_StopRealPlay(hPlay);
		CLIENT_Logout(hLogin);
		CLIENT_Cleanup();
		return 1;
	}
	
	Sleep(60 * 1000);
	
	CLIENT_Logout(hLogin);
	CLIENT_StopRealPlay(hPlay);
	CLIENT_Cleanup();
	
	PLAY_Stop(PLAYPORT);
	PLAY_CloseStream(PLAYPORT);
	
	return 0;
}
参考链接: https://www.cnblogs.com/dwdxdy/p/3713990.html 


你可能感兴趣的:(opencv,C++,Win32,Windows)