fast-yolo4/fast/PlayWnd.cpp

501 lines
13 KiB
C++
Raw Normal View History

2024-09-25 09:43:03 +08:00
#include "stdafx.h"
#include "Fast.h"
#include "PlayWnd.h"
#include "PtzScreen.h"
2024-09-29 13:58:12 +08:00
#include "DrawImage.h"
#include "ImageCalibrator.h"
2024-09-25 09:43:03 +08:00
#include "dhplay.h"
//#include "RealPlayAndPTZControlDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
2024-09-29 13:58:12 +08:00
#define LOGIN_TIMER 1
2024-09-25 09:43:03 +08:00
/////////////////////////////////////////////////////////////////////////////
// CPlayWnd dialog
CPlayWnd::CPlayWnd():m_nWndID(0), m_FlagRect(FALSE),bIsPlaying(false)
{
//{{AFX_DATA_INIT(CPlayWnd)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_nIndex = 0;
2024-09-29 13:58:12 +08:00
m_nPlayPort = 0;
//cv::namewindow("fsf");
2024-09-25 09:43:03 +08:00
}
BEGIN_MESSAGE_MAP(CPlayWnd, CWnd)
//{{AFX_MSG_MAP(CPlayWnd)
ON_WM_ERASEBKGND()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_ACTIVATE()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_ACTIVATEAPP()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(VIDEO_MENU_BASE, VIDEO_MENU_END, OnVideoMenu)
ON_MESSAGE(VIDEO_REPAINT, OnRepaintWnd)
//}}AFX_MSG_MAP
ON_WM_CREATE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPlayWnd message handlers
BOOL CPlayWnd::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect rt;
GetClientRect(&rt);
CBrush br;
br.CreateSolidBrush(VIDEO_BACK_COLOR);
pDC->FillRect(&rt,&br);
return TRUE;
}
LRESULT CPlayWnd::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
CPtzScreen *pContainer = (CPtzScreen *)GetParent();
// CRealPlayAndPTZControlDlg *pMainWnd = (CRealPlayAndPTZControlDlg *)(AfxGetApp()->GetMainWnd());
// CDialogTransmit * pTransWnd = (CDialogTransmit *)pMainWnd->m_pTabTransmit;
if(pContainer)
{
switch(message)
{
case WM_LBUTTONUP:
{
pointEnd.x = LOWORD(lParam);
pointEnd.y = HIWORD(lParam);
if(m_FlagRect == TRUE)
{
m_FlagRect =FALSE;
KillTimer(2);
RECT rt;
GetClientRect(&rt);
pContainer->m_pRectEventFunc(rt,pointStart,pointEnd,pContainer->m_dwRectEvent);
}
}
break;
case WM_MOUSEMOVE:
{
RECT rt;
GetClientRect(&rt);
long x = LOWORD(lParam);
long y = HIWORD(lParam);
x = x>rt.right?rt.right:x;
x = x<rt.left?rt.left:x;
y = y>rt.bottom?rt.bottom:y;
y = y<rt.top?rt.top:y;
pointEnd.x = x;
pointEnd.y = y;
}
break;
case WM_LBUTTONDOWN:
{
pContainer->SetActivePage(this); // <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>
if (pContainer->m_pMessageProc) // ѡ<><D1A1><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD>
{
pContainer->m_pMessageProc(m_nWndID, WM_RBUTTONDOWN, pContainer->m_dwMessageUser);
}
if (bIsPlaying == false)
{
break;
}
pointStart.x = LOWORD(lParam);
pointStart.y = HIWORD(lParam);
m_FlagRect =TRUE;
SetTimer(2,16,NULL);
break;
}
case WM_LBUTTONDBLCLK:
{
BOOL bMulti = pContainer->GetMultiScreen();
pContainer->SetMultiScreen(!bMulti);
}
break;
default:
break;
}
}
return CWnd::DefWindowProc(message, wParam, lParam);
}
void CPlayWnd::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
/*CPtzScreen *pContainer = (CPtzScreen *)GetParent();
pContainer->SetActivePage(this);
CMenu menu;
menu.CreatePopupMenu();
menu.AppendMenu(MF_STRING | pContainer->GetFullScreen() ? MF_CHECKED : MF_UNCHECKED, VIDEO_MENU_FULLSCREEN, ConvertString(NAME_MENU_FULLSCREEN));
menu.AppendMenu(MF_STRING | pContainer->GetMultiScreen() ? MF_CHECKED : MF_UNCHECKED, VIDEO_MENU_MULTISCREEN, ConvertString(NAME_MENU_MULTISCREEN));
menu.AppendMenu(MF_STRING | pContainer->GetAutoAdjustPos() ? MF_CHECKED : MF_UNCHECKED, VIDEO_MENU_AUTOADJUST, ConvertString(NAME_MENU_AUTOADJUST));
menu.AppendMenu(MF_STRING | pContainer->m_pGetParams(m_nWndID, 0, pContainer->m_dwGetParams) ? MF_CHECKED : MF_UNCHECKED, VIDEO_MENU_EXITDECODE, ConvertString(NAME_MENU_EXITDECODE));
menu.AppendMenu(MF_STRING | pContainer->m_pGetParams(m_nWndID, 1, pContainer->m_dwGetParams) ? MF_CHECKED : MF_UNCHECKED, VIDEO_MENU_EXITCYCLE, ConvertString(NAME_MENU_EXITCYCLE));
TrackPopupMenu(
menu.m_hMenu,
TPM_LEFTALIGN,
point.x,
point.y,
0,
m_hWnd,
NULL);*/
}
void CPlayWnd::OnVideoMenu(UINT nID)
{
CPtzScreen *pContainer = (CPtzScreen *)GetParent();
switch(nID)
{
case VIDEO_MENU_FULLSCREEN:
pContainer->SetFullScreen(!pContainer->GetFullScreen());
break;
case VIDEO_MENU_MULTISCREEN:
pContainer->SetMultiScreen(!pContainer->GetMultiScreen());
break;
case VIDEO_MENU_AUTOADJUST:
pContainer->SetAutoAdjustPos(!pContainer->GetAutoAdjustPos());
break;
case VIDEO_MENU_PRINTSCREEN:
break;
case VIDEO_MENU_RECORDVIDEO:
break;
case VIDEO_MENU_EXITDECODE:
pContainer->m_pSetParams(m_nWndID, 0, pContainer->m_dwSetParams);
break;
case VIDEO_MENU_EXITCYCLE:
pContainer->m_pSetParams(m_nWndID, 1, pContainer->m_dwSetParams);
break;
default:
break;
}
}
LRESULT CPlayWnd::OnRepaintWnd(WPARAM wParam, LPARAM lParam)
{
Invalidate();
return 0;
}
BOOL CPlayWnd::DestroyWindow()
{
return CWnd::DestroyWindow();
}
void CPlayWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState, pWndOther, bMinimized);
// TODO: Add your message handler code here
}
void CPlayWnd::OnTimer(UINT_PTR nIDEvent)
{
2024-09-29 13:58:12 +08:00
switch (nIDEvent)
{
case 1:
m_llLoginId = Login();
KillTimer(1);
break;
}
2024-09-25 09:43:03 +08:00
CWnd::OnTimer(nIDEvent);
}
void CPlayWnd::OnActivateApp(BOOL bActive, DWORD hTask)
{
CWnd::OnActivateApp(bActive, hTask);
}
//Display log in failure reason
void CPlayWnd::ShowLoginErrorReason(int nError)
{
if (1 == nError) MessageBox(ConvertString("Invalid password!"), ConvertString("Prompt"));
else if (2 == nError) MessageBox(ConvertString("Invalid account!"), ConvertString("Prompt"));
else if (3 == nError) MessageBox(ConvertString("Timeout!"), ConvertString("Prompt"));
else if (4 == nError) MessageBox(ConvertString("The user has logged in!"), ConvertString("Prompt"));
else if (5 == nError) MessageBox(ConvertString("The user has been locked!"), ConvertString("Prompt"));
else if (6 == nError) MessageBox(ConvertString("The user has listed into illegal!"), ConvertString("Prompt"));
else if (7 == nError) MessageBox(ConvertString("The system is busy!"), ConvertString("Prompt"));
else if (9 == nError) MessageBox(ConvertString("You Can't find the network server!"), ConvertString("Prompt"));
else MessageBox(ConvertString("Login failed!"), ConvertString("Prompt"));
}
2024-09-29 13:58:12 +08:00
// netsdk ʵʱ<CAB5>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void CALL_METHOD fRealDataCB(LLONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser)
{
CPlayWnd *pWnd = (CPlayWnd*)dwUser;
if (pWnd->m_lRealHandle != lRealHandle)return;
// <20>Ѵ<EFBFBD><D1B4><EFBFBD>ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>playsdk<64><6B>
PLAY_InputData(pWnd->m_nWndID, pBuffer, dwBufSize);
return;
}
2024-09-25 09:43:03 +08:00
2024-09-29 13:58:12 +08:00
LLONG CPlayWnd::Login()
2024-09-25 09:43:03 +08:00
{
//<2F><><EFBFBD>ε<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>¼ID
2024-09-29 13:58:12 +08:00
//m_strCameraIp, m_nCameraPort, m_strUserName, m_strPassWord);
2024-09-25 09:43:03 +08:00
char *pchDVRIP;
//CString strDvrIP = GetDvrIP();
2024-09-29 13:58:12 +08:00
pchDVRIP = (LPSTR)(LPCSTR)m_strCameraIp;
WORD wDVRPort = (WORD)m_nCameraPort;
char *pchUserName = (LPSTR)(LPCSTR)m_strUserName;
char *pchPassword = (LPSTR)(LPCSTR)m_strPassWord;
/*
2024-09-25 09:43:03 +08:00
NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY stInparam;
memset(&stInparam, 0, sizeof(stInparam));
stInparam.dwSize = sizeof(stInparam);
strncpy(stInparam.szIP, pchDVRIP, sizeof(stInparam.szIP) - 1);
strncpy(stInparam.szPassword, pchPassword, sizeof(stInparam.szPassword) - 1);
strncpy(stInparam.szUserName, pchUserName, sizeof(stInparam.szUserName) - 1);
stInparam.nPort = wDVRPort;
stInparam.emSpecCap = EM_LOGIN_SPEC_CAP_TCP;
NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY stOutparam;
memset(&stOutparam, 0, sizeof(stOutparam));
stOutparam.dwSize = sizeof(stOutparam);
LLONG lRet = CLIENT_LoginWithHighLevelSecurity(&stInparam, &stOutparam);
if (0 == lRet)
{
//Display log in failure reason
ShowLoginErrorReason(stOutparam.nError);
return -1;
}
else
{
//m_LoginID = lRet;
int nRetLen = 0;
NET_DEV_CHN_COUNT_INFO stuChn = { sizeof(NET_DEV_CHN_COUNT_INFO) };
stuChn.stuVideoIn.dwSize = sizeof(stuChn.stuVideoIn);
stuChn.stuVideoOut.dwSize = sizeof(stuChn.stuVideoOut);
BOOL bRet = CLIENT_QueryDevState(lRet, DH_DEVSTATE_DEV_CHN_COUNT, (char*)&stuChn, stuChn.dwSize, &nRetLen);
if (!bRet)
{
DWORD dwError = CLIENT_GetLastError() & 0x7fffffff;
}
return lRet;
}
2024-09-29 13:58:12 +08:00
//SetWindowText(ConvertString("CapturePicture"));*/
PLAY_GetFreePort(&m_nWndID);
PLAY_SetStreamOpenMode(m_nWndID, STREAME_REALTIME);
PLAY_OpenStream(m_nWndID, NULL, 0, 1024 * 512 * 6);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶץͼ<D7A5>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>Իص<D4BB><D8B5><EFBFBD>YUV<55><56><EFBFBD><EFBFBD>
PLAY_SetDisplayCallBack(m_nWndID, fDisplayCB, this);
PLAY_Play(m_nWndID, NULL);
NET_DEVICEINFO_Ex stLoginInfo = { 0 };
int nErrcode = 0;
LLONG lLoginHandle = CLIENT_LoginEx2(pchDVRIP, m_nCameraPort, pchUserName, pchPassword, (EM_LOGIN_SPAC_CAP_TYPE)0, NULL, &stLoginInfo, &nErrcode);
if (0 == lLoginHandle)
{
//cout << "Login device failed" << endl;
//cin >> szIpAddr;
return -1;
}
else
{
//cout << "Login device success" << endl;
}
//<2F><><EFBFBD><EFBFBD>
m_lRealHandle = CLIENT_RealPlayEx(lLoginHandle, 0, 0);
if (0 == m_lRealHandle)
{
//cout << "CLIENT_RealPlayEx fail!" << endl;
Sleep(100000);
return -1;
}
//cout << "CLIENT_RealPlayEx success!" << endl;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
CLIENT_SetRealDataCallBack(m_lRealHandle, fRealDataCB, (LDWORD)this);
return lLoginHandle;
2024-09-25 09:43:03 +08:00
}
int CPlayWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_strCameraIp.IsEmpty())
{
return -1;
}
2024-09-29 13:58:12 +08:00
SetTimer(1, 5000, NULL);
2024-09-25 09:43:03 +08:00
return 0;
}
2024-09-29 13:58:12 +08:00
void CPlayWnd::StopPlayForServerMode()
2024-09-25 09:43:03 +08:00
{
2024-09-29 13:58:12 +08:00
/*
BOOL bRealPlay = CLIENT_StopRealPlayEx(m_DispHanle);
if (bRealPlay)
2024-09-25 09:43:03 +08:00
{
2024-09-29 13:58:12 +08:00
this->SetWndPlaying(false);
2024-09-25 09:43:03 +08:00
2024-09-29 13:58:12 +08:00
//And then close PLAY_Play
BOOL bPlay = g_PlayAPI.PLAY_Stop(m_nWndID);
if (bPlay)
2024-09-25 09:43:03 +08:00
{
2024-09-29 13:58:12 +08:00
//At last close PLAY_OpenStream
BOOL bStream = g_PlayAPI.PLAY_CloseStream(m_nWndID);
2024-09-25 09:43:03 +08:00
}
2024-09-29 13:58:12 +08:00
}*/
2024-09-25 09:43:03 +08:00
}
// playsdk <20>ص<EFBFBD> yuv<75><76><EFBFBD><EFBFBD>
void CALL_METHOD CPlayWnd::fDisplayCB(LONG nPort, char * pBuf, LONG nSize, LONG nWidth, LONG nHeight, LONG nStamp, LONG nType, void* pReserved)
{
2024-09-29 13:58:12 +08:00
CPlayWnd *pShowWnd = (CPlayWnd *)(pReserved);
2024-09-25 09:43:03 +08:00
2024-09-29 13:58:12 +08:00
if (pShowWnd->m_nWndID != nPort)return;
2024-09-25 09:43:03 +08:00
//TRACE("%d\n", pThis->m_nWndID);
2024-09-29 13:58:12 +08:00
2024-09-25 09:43:03 +08:00
//if (pThis->m_nWndID > 1)return;
2024-09-29 13:58:12 +08:00
pShowWnd->m_nIndex++;
if (pShowWnd->m_nIndex == 24)
2024-09-25 09:43:03 +08:00
{
2024-09-29 13:58:12 +08:00
2024-09-25 09:43:03 +08:00
cv::Mat cv_img;
cv::Mat cv_yuv(nHeight + nHeight / 2, nWidth, CV_8UC1, pBuf);//pFrameΪYUV<55><56><EFBFBD>ݵ<EFBFBD>ַ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> CV_8UC1<43><31> CV_8UC3.
cv_img = cv::Mat(nHeight, nWidth, CV_8UC3);
cv::cvtColor(cv_yuv, cv_img, cv::COLOR_YUV2BGR_I420); //cv::COLOR_YUV2BGR_I420
//cv::imshow("video", cv_img);
2024-09-29 13:58:12 +08:00
cv::Mat imgTmp;
CRect rect;
pShowWnd->GetClientRect(&rect); // <20><>ȡ<EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>С
int nWidth = cv_img.cols * rect.Height() *1.0 / cv_img.rows;
if (rect.Width() == 0)return;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭվչƽ
if (theApp.m_bCalibrator)
{
cv_img = CImageCalibrator::imageCalibration(cv_img);
}
cv::resize(cv_img, imgTmp, cv::Size(nWidth, rect.Height()));// <20><><EFBFBD><EFBFBD>Mat<61><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pShowWnd->m_nIndex = 0;
pShowWnd->DrawMat(imgTmp, (rect.Width() - nWidth) / 2);
cv::waitKey(10);
2024-09-25 09:43:03 +08:00
}
return;
}
//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31>opencv<63><76><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
//<2F><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>Ҫչʾ<D5B9><CABE>Picture Control<6F><6C>ID
2024-09-29 13:58:12 +08:00
void CPlayWnd::DrawMat(cv::Mat& imageMat, int nOffset)
2024-09-25 09:43:03 +08:00
{
2024-09-29 13:58:12 +08:00
/*cv::Mat img;
2024-09-25 09:43:03 +08:00
CRect rect;
GetClientRect(&rect); // <20><>ȡ<EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>С
2024-09-29 13:58:12 +08:00
int nWidth = imageMat.cols * rect.Height() *1.0 / imageMat.rows;
cv::resize(imageMat, img, cv::Size(nWidth, rect.Height()));// <20><><EFBFBD><EFBFBD>Mat<61><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// תһ<D7AA>¸<EFBFBD>ʽ ,<2C><><EFBFBD>ο<EFBFBD><CEBF>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD>,*/
/*switch (imgTmp.channels())
2024-09-25 09:43:03 +08:00
{
case 1:
cv::cvtColor(imgTmp, imgTmp, CV_GRAY2BGRA); // GRAY<41><59>ͨ<EFBFBD><CDA8>
break;
case 3:
cv::cvtColor(imgTmp, imgTmp, CV_BGR2BGRA); // BGR<47><52>ͨ<EFBFBD><CDA8>
break;
default:
break;
}
int pixelBytes = imgTmp.channels() * (imgTmp.depth() + 1); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6>ٸ<EFBFBD><D9B8>ֽ<EFBFBD>
// <20><><EFBFBD><EFBFBD>bitmapinfo(<28><><EFBFBD><EFBFBD>ͷ)
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 8 * pixelBytes;
bitInfo.bmiHeader.biWidth = imgTmp.cols;
bitInfo.bmiHeader.biHeight = -imgTmp.rows;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
// Mat.data + bitmap<61><70><EFBFBD><EFBFBD>ͷ -> MFC
CDC* pDC = GetDC();
::StretchDIBits(
pDC->GetSafeHdc(),
(rect.Width()-nWidth)/2, 0, nWidth, rect.Height(),
0, 0, nWidth, rect.Height(),
imgTmp.data,
&bitInfo,
DIB_RGB_COLORS,
SRCCOPY
);
2024-09-29 13:58:12 +08:00
ReleaseDC(pDC);*/
CImage image;
image.Create(imageMat.cols, imageMat.rows, 24);
for (int y = 0; y < imageMat.rows; ++y) {
const uchar* src = imageMat.ptr<uchar>(y);
uchar* dst = reinterpret_cast<uchar*>(image.GetBits()) + y * image.GetPitch();
for (int x = 0; x < imageMat.cols; ++x) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
src += 3;
dst += 3;
}
2024-09-25 09:43:03 +08:00
}
2024-09-29 13:58:12 +08:00
HDC hdc = ::GetDC(m_hWnd);
image.Draw(hdc, CRect(CPoint(nOffset, 0), CSize(imageMat.cols, imageMat.rows)));
image.Destroy();
::ReleaseDC(m_hWnd, hdc);
2024-09-25 09:43:03 +08:00
}
2024-09-29 13:58:12 +08:00