张洋 há 3 semanas atrás
pai
commit
1a0b80b5fb

+ 1 - 1
bin/Win32/Debug/zhipuzi_pos_windows/skin/setting_ai_shibie.xml

@@ -10,7 +10,7 @@
 	<HorizontalLayout height="500" padding="0,20,0,0">
 		<Label text="摄像头识别区域设置" width="260" height="30"/>
 		<VerticalLayout width="600" height="450" padding="10,0,10,0">
-			<Control name="image_video" float="true" pos="0,0,600,450"></Control>
+			<Control name="image_video" float="true" pos="0,0,400,300"></Control>
 		</VerticalLayout>		
 	</HorizontalLayout>
 </Window>

+ 40 - 29
zhipuzi_pos_windows/page/CSettingPageUI.cpp

@@ -23,10 +23,14 @@ using boost::asio::ip::tcp;
 
 namespace
 {
-	const int kVideoPreviewWidth = 600;
-	const int kVideoPreviewHeight = 450;
-	const int kVideoMinWidth = 400;
-	const int kVideoMinHeight = 300;
+	//摄像头拍摄的图片的原始尺寸,本系统里面默认为800*600
+	const int kVideoWidth = 800;
+	const int kVideoHeight = 600;
+
+	//红色边框的最小尺寸
+	const int kVideoBorderMinWidth = 400;
+	const int kVideoBorderMinHeight = 300;
+
 	const int kVideoGripSize = 8;
 }
 
@@ -3459,6 +3463,9 @@ void CSettingPageUI::SwitchSettingPage(std::wstring page_name)
 
 void CSettingPageUI::HandleUpdateVideo()
 {
+	//在刚启动摄像头线程的时候,读取一次数据库中的裁剪框坐标就行了,拖动过程中只修改m_videoBorderRect变量,鼠标释放的时候再保存到数据库中
+	LoadVideoBorderRect();
+
 	while (m_is_show_video_capture == true)
 	{
 		if (m_pMainWnd->GetCurPageName() != CMainWnd::SETTING)
@@ -3482,8 +3489,13 @@ void CSettingPageUI::HandleUpdateVideo()
 			cvtColor(img, img, cv::COLOR_GRAY2BGR);
 		}
 
-		//缩放尺寸,适合在界面上展示,过大过小都不好看,这个尺寸是根据界面上image控件的大小来定的,可以根据实际情况调整
-		cv::resize(img, img, cv::Size(kVideoPreviewWidth, kVideoPreviewHeight), 0, 0, cv::INTER_LINEAR);
+		//缩放尺寸,适合在界面上展示,过大过小都不好看,这个尺寸是根据界面上image控件的大小来定的,可以根据实际情况调整(摄像头拍摄出来的,默认为800*600)
+		CControlUI* pImage = static_cast<CControlUI*>(this->FindSubControl(_T("image_video")));
+		RECT imageRect = pImage->GetPos();
+		int imageWidth = imageRect.right - imageRect.left;
+		int imageHeight = imageRect.bottom - imageRect.top;
+
+		cv::resize(img, img, cv::Size(imageWidth, imageHeight), 0, 0, cv::INTER_LINEAR);
 
 		// 创建或更新位图
 		HDC hDC = GetDC(NULL);
@@ -3515,8 +3527,6 @@ void CSettingPageUI::HandleUpdateVideo()
 
 void CSettingPageUI::UpdateVideoShow()
 {
-	LoadVideoBorderRect();
-
 	HDC hDC = m_pManager->GetPaintDC();
 	if (!hDC || !m_hBitmap)
 	{
@@ -3633,18 +3643,18 @@ void CSettingPageUI::LoadVideoBorderRect()
 
 	int x = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_x"), 0);
 	int y = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_y"), 0);
-	int w = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_w"), kVideoPreviewWidth);
-	int h = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_h"), kVideoPreviewHeight);
+	int w = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_w"), kVideoWidth);
+	int h = readInt(CSetting::GetInstance()->GetParam("setting_ai_video_rect_h"), kVideoHeight);
 
-	if (w < kVideoMinWidth) w = kVideoMinWidth;
-	if (h < kVideoMinHeight) h = kVideoMinHeight;
-	if (w > kVideoPreviewWidth) w = kVideoPreviewWidth;
-	if (h > kVideoPreviewHeight) h = kVideoPreviewHeight;
+	if (w < kVideoBorderMinWidth) w = kVideoBorderMinWidth;
+	if (h < kVideoBorderMinHeight) h = kVideoBorderMinHeight;
+	if (w > kVideoWidth) w = kVideoWidth;
+	if (h > kVideoHeight) h = kVideoHeight;
 
 	if (x < 0) x = 0;
 	if (y < 0) y = 0;
-	if (x + w > kVideoPreviewWidth) x = kVideoPreviewWidth - w;
-	if (y + h > kVideoPreviewHeight) y = kVideoPreviewHeight - h;
+	if (x + w > kVideoWidth) x = kVideoWidth - w;
+	if (y + h > kVideoHeight) y = kVideoHeight - h;
 
 	m_videoBorderRect.left = x;
 	m_videoBorderRect.top = y;
@@ -3691,10 +3701,10 @@ RECT CSettingPageUI::GetVideoBorderDisplayRect(const RECT& imageRect)
 		return rc;
 	}
 
-	rc.left = imageRect.left + MulDiv(m_videoBorderRect.left, imageWidth, kVideoPreviewWidth);
-	rc.top = imageRect.top + MulDiv(m_videoBorderRect.top, imageHeight, kVideoPreviewHeight);
-	rc.right = imageRect.left + MulDiv(m_videoBorderRect.right, imageWidth, kVideoPreviewWidth);
-	rc.bottom = imageRect.top + MulDiv(m_videoBorderRect.bottom, imageHeight, kVideoPreviewHeight);
+	rc.left = imageRect.left + MulDiv(m_videoBorderRect.left, imageWidth, kVideoWidth);
+	rc.top = imageRect.top + MulDiv(m_videoBorderRect.top, imageHeight, kVideoHeight);
+	rc.right = imageRect.left + MulDiv(m_videoBorderRect.right, imageWidth, kVideoWidth);
+	rc.bottom = imageRect.top + MulDiv(m_videoBorderRect.bottom, imageHeight, kVideoHeight);
 
 	return rc;
 }
@@ -3737,7 +3747,7 @@ bool CSettingPageUI::HandleVideoBorderMouseMessage(UINT uMsg, WPARAM wParam, LPA
 		return false;
 	}
 
-	LoadVideoBorderRect();
+	//LoadVideoBorderRect();
 
 	RECT imageRect = GetVideoImageRect();
 	int imageWidth = imageRect.right - imageRect.left;
@@ -3747,6 +3757,7 @@ bool CSettingPageUI::HandleVideoBorderMouseMessage(UINT uMsg, WPARAM wParam, LPA
 		return false;
 	}
 
+	//计算红色边框在界面上的显示位置
 	RECT borderRect = GetVideoBorderDisplayRect(imageRect);
 
 	auto setCursorByHit = [](int hitType) -> bool
@@ -3828,36 +3839,36 @@ bool CSettingPageUI::HandleVideoBorderMouseMessage(UINT uMsg, WPARAM wParam, LPA
 			return true;
 		}
 
-		int dx = MulDiv(pt.x - m_videoBorderDragStartPt.x, kVideoPreviewWidth, imageWidth);
-		int dy = MulDiv(pt.y - m_videoBorderDragStartPt.y, kVideoPreviewHeight, imageHeight);
+		int dx = MulDiv(pt.x - m_videoBorderDragStartPt.x, kVideoWidth, imageWidth);
+		int dy = MulDiv(pt.y - m_videoBorderDragStartPt.y, kVideoHeight, imageHeight);
 
 		RECT newRect = m_videoBorderDragStartRect;
 
 		if (m_videoBorderHitType == HTLEFT || m_videoBorderHitType == HTTOPLEFT || m_videoBorderHitType == HTBOTTOMLEFT)
 		{
-			int maxLeft = static_cast<int>(m_videoBorderDragStartRect.right) - kVideoMinWidth;
+			int maxLeft = static_cast<int>(m_videoBorderDragStartRect.right) - kVideoBorderMinWidth;
 			int left = std::max(0, std::min(static_cast<int>(m_videoBorderDragStartRect.left) + dx, maxLeft));
 			newRect.left = static_cast<LONG>(left);
 		}
 
 		if (m_videoBorderHitType == HTRIGHT || m_videoBorderHitType == HTTOPRIGHT || m_videoBorderHitType == HTBOTTOMRIGHT)
 		{
-			int minRight = static_cast<int>(m_videoBorderDragStartRect.left) + kVideoMinWidth;
-			int right = std::max(minRight, std::min(static_cast<int>(m_videoBorderDragStartRect.right) + dx, kVideoPreviewWidth));
+			int minRight = static_cast<int>(m_videoBorderDragStartRect.left) + kVideoBorderMinWidth;
+			int right = std::max(minRight, std::min(static_cast<int>(m_videoBorderDragStartRect.right) + dx, kVideoWidth));
 			newRect.right = static_cast<LONG>(right);
 		}
 
 		if (m_videoBorderHitType == HTTOP || m_videoBorderHitType == HTTOPLEFT || m_videoBorderHitType == HTTOPRIGHT)
 		{
-			int maxTop = static_cast<int>(m_videoBorderDragStartRect.bottom) - kVideoMinHeight;
+			int maxTop = static_cast<int>(m_videoBorderDragStartRect.bottom) - kVideoBorderMinHeight;
 			int top = std::max(0, std::min(static_cast<int>(m_videoBorderDragStartRect.top) + dy, maxTop));
 			newRect.top = static_cast<LONG>(top);
 		}
 
 		if (m_videoBorderHitType == HTBOTTOM || m_videoBorderHitType == HTBOTTOMLEFT || m_videoBorderHitType == HTBOTTOMRIGHT)
 		{
-			int minBottom = static_cast<int>(m_videoBorderDragStartRect.top) + kVideoMinHeight;
-			int bottom = std::max(minBottom, std::min(static_cast<int>(m_videoBorderDragStartRect.bottom) + dy, kVideoPreviewHeight));
+			int minBottom = static_cast<int>(m_videoBorderDragStartRect.top) + kVideoBorderMinHeight;
+			int bottom = std::max(minBottom, std::min(static_cast<int>(m_videoBorderDragStartRect.bottom) + dy, kVideoHeight));
 			newRect.bottom = static_cast<LONG>(bottom);
 		}
 

+ 7 - 3
zhipuzi_pos_windows/page/CSettingPageUI.h

@@ -43,9 +43,10 @@ private:
 	//保存当前红色边框矩形位置到数据库中
 	void SaveVideoBorderRect();
 
-	//获取视频图像的显示矩形
+	//获取视频图像的image_video控件的pos矩形
 	RECT GetVideoImageRect();
 
+	//根据视频图像的image_video控件的pos矩形尺寸和图片实际尺寸的比例,计算出红色边框矩形在屏幕上按比例缩放后的显示位置
 	RECT GetVideoBorderDisplayRect(const RECT& imageRect);
 
 	int HitTestVideoBorder(POINT pt, const RECT& borderRect);
@@ -64,10 +65,13 @@ private:
 	HBITMAP m_hBitmap = NULL;
 	HDC m_hMemDC = NULL;
 
-	RECT m_videoBorderRect = { 0, 0, 400, 300 };
-	RECT m_videoBorderDragStartRect = { 0, 0, 400, 300 };
+	//红色裁剪框的位置Rect,这个坐标是相对800*600的真实坐标,可以直接存入数据库的,不随视频实际显示的尺寸变化,实际显示的红框会根据显示区域尺寸来同比例缩放
+	RECT m_videoBorderRect = { 0, 0, 800, 600 };
+	RECT m_videoBorderDragStartRect = { 0, 0, 800, 600 };
 	POINT m_videoBorderDragStartPt = { 0, 0 };
+	
 	bool m_videoBorderLoaded = false;
+
 	bool m_isDraggingVideoBorder = false;
 	int m_videoBorderHitType = HTNOWHERE;
 };