From 1e1fbe300ba2b22246c6743147087d10cf6c9798 Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期二, 15 七月 2025 17:25:05 +0800
Subject: [PATCH] 修复:InterfaceManager.cpp保存图片添加锁

---
 EdgeInspector_App/InterfaceManager.cpp        |   33 +++++++----
 EdgeInspector_App/MainFrm.cpp                 |    5 -
 EdgeInspector_App/View/ViewMain_ScanImage.cpp |   93 +++++++++++++------------------
 EdgeInspector_App/InterfaceManager.h          |    5 +
 4 files changed, 67 insertions(+), 69 deletions(-)

diff --git a/EdgeInspector_App/InterfaceManager.cpp b/EdgeInspector_App/InterfaceManager.cpp
index c1ef81f..e06011b 100644
--- a/EdgeInspector_App/InterfaceManager.cpp
+++ b/EdgeInspector_App/InterfaceManager.cpp
@@ -1821,21 +1821,26 @@
 {
 	g_pStatus->CheckDirectory(strPath);
 
-	BYTE* pBitBuffer;
-	FIBITMAP *bitmap = NULL;	
-
 	CString strSavePath = strPath;
 	strSavePath += _T(".jpg");
 
-	bitmap = FreeImage_Allocate(IMAGE_WIDTH, nLineCnt,8);
-	pBitBuffer = FreeImage_GetBits(bitmap);
-	if(pBitBuffer == NULL) {
-		FreeImage_Unload(bitmap);		
+	Lock();
+	FIBITMAP* bitmap = FreeImage_Allocate(IMAGE_WIDTH, nLineCnt, 8);
+	if (bitmap == NULL) {
+		Unlock();
 		return FALSE;
-	}			
+	}
 
-	LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide,ptStart.y);
+	BYTE* pBitBuffer = FreeImage_GetBits(bitmap);
+	if (pBitBuffer == NULL) {
+		FreeImage_Unload(bitmap);
+		Unlock();
+		return FALSE;
+	}
+
+	LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, ptStart.y);
 	if (lpImg == NULL) {
+		Unlock();
 		return FALSE;
 	}
 
@@ -1846,10 +1851,10 @@
 	sprintf_s(str_filename, "%s", W2A(strSavePath));
 
 	FreeImage_FlipVertical(bitmap);
-	FreeImage_Save(FIF_JPEG, bitmap, str_filename,JPEG_FULLIMAGE_RATE);
-	FreeImage_Unload(bitmap);		
+	FreeImage_Save(FIF_JPEG, bitmap, str_filename, JPEG_FULLIMAGE_RATE);
+	FreeImage_Unload(bitmap);
 
-	return TRUE;
+	Unlock();
 }
 
 BOOL CInterfaceManager::SaveFullImageModern(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality /*= 80*/)
@@ -1903,6 +1908,7 @@
 	cvReleaseImage(&pImgNew);
 	cvReleaseImageHeader(&pImg);
 #else
+	Lock();
 	// 原始图像数据转 cv::Mat
 	cv::Mat origin(nImgHeight, nImgWidth, CV_8UC1, lpOrigin + (nImgWidth * nStartY));
 
@@ -1921,9 +1927,12 @@
 	std::string strFilename = W2A(strPath);
 
 	if (!cv::imwrite(strFilename, resized, params)) {
+		Unlock();
 		g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), imwrite failed"), strPath, PANEL_SIDE[iSide], nStartY);
 		return FALSE;
 	}
+
+	Unlock();
 #endif
 
 	g_pLog->DisplayMessage(_T("Save Full Image Success(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality);	
diff --git a/EdgeInspector_App/InterfaceManager.h b/EdgeInspector_App/InterfaceManager.h
index 3087daa..85c8bab 100644
--- a/EdgeInspector_App/InterfaceManager.h
+++ b/EdgeInspector_App/InterfaceManager.h
@@ -123,6 +123,11 @@
 
 	long long GetCurrentTimestamp();
 
+private:
+	CRITICAL_SECTION m_criticalSection;
+	inline void Lock() { ::InitializeCriticalSection(&m_criticalSection); }
+	inline void Unlock() { ::DeleteCriticalSection(&m_criticalSection); }
+
 #if USE_WEBSOCKET
 protected:
 	void CreateWebSocketClients(WebSocketClientPool** wsClients, CString cstrURI, int nThreadCount);
diff --git a/EdgeInspector_App/MainFrm.cpp b/EdgeInspector_App/MainFrm.cpp
index c9a5675..9589286 100644
--- a/EdgeInspector_App/MainFrm.cpp
+++ b/EdgeInspector_App/MainFrm.cpp
@@ -331,8 +331,7 @@
 
 BOOL CMainFrame::IV2M_SaveFullImage(CString strPath,int iSide,CPoint ptStart,int nLength)
 {
-	if(IsScanNow())
-	{
+	if(IsScanNow()) {
 		g_pLog->DisplayMessage(_T("Doing Inspection"));
 		return TRUE;
 	}
@@ -341,7 +340,7 @@
 
 	//SetSlashText(GLOBAL_DEFINE::emText, _T("Image Save Start"), RGB(0,0,0));
 
-	BOOL bRet = SaveFullImage(strPath,iSide,ptStart,nLength);
+	BOOL bRet = SaveFullImage(strPath, iSide, ptStart, nLength);
 
 	SetSlashText(GLOBAL_DEFINE::emText, strPath, RGB(0,0,0));
 
diff --git a/EdgeInspector_App/View/ViewMain_ScanImage.cpp b/EdgeInspector_App/View/ViewMain_ScanImage.cpp
index 9b6af37..e616eeb 100644
--- a/EdgeInspector_App/View/ViewMain_ScanImage.cpp
+++ b/EdgeInspector_App/View/ViewMain_ScanImage.cpp
@@ -1159,8 +1159,9 @@
 
 void CViewMain_ScanImage::SaveFullImageAll()
 {
-	if(m_pGlassData == NULL || m_IV2M == NULL)
+	if (m_pGlassData == NULL || m_IV2M == NULL) {
 		return;
+	}
 
 	int				iView;
 	CPoint			ptStart;
@@ -1170,7 +1171,7 @@
 	int				iSide;
 	int				nTotalLength = 0;
 	int				nFrame = 0;
-	CGlassRecipe *pGlassRcp = m_pGlassData->GetGlassRecipe();
+	CGlassRecipe    *pGlassRcp = m_pGlassData->GetGlassRecipe();
 
 #if OFFLINE_KEY == 1
 	if (NULL == pGlassRcp) return;
@@ -1179,81 +1180,72 @@
 	CString			strRcpName = pGlassRcp->GetRecipeName();
 	CString			strGlassID = g_pBase->m_strHPanelID;
 
-	if(strGlassID.IsEmpty() == TRUE)
-	{
+	if(strGlassID.IsEmpty() == TRUE) {
 		strGlassID = _T("Manual_ID");
 	}
-	if(strRcpName.IsEmpty() == TRUE)
-	{
+	if(strRcpName.IsEmpty() == TRUE) {
 		strRcpName = _T("TestRcp");
 	}
 
 	strGlassID.Trim(_T(' '));
 
 	CTime	time = CTime::GetCurrentTime();
-
-	CString			strDate,strTime;
+	CString	strDate, strTime;
 
 	strDate.Format(_T("%04d%02d%02d"),time.GetYear(),time.GetMonth(),time.GetDay());
 	strTime.Format(_T("%02d%02d%02d"),time.GetHour(),time.GetMinute(),time.GetSecond());
 
-	for(iView=0;iView<MAX_DIMENSION_COUNT;iView++)
-	{
+	for(iView=0;iView<MAX_DIMENSION_COUNT;iView++) {
 		iSide  = GetSideIdx(iView);		
 
-		int			iCam = m_pHardware->GetCameraIndexToDimension((DimensionDir)iSide);
-		int			iScan = m_pHardware->GetScanToDimension((DimensionDir)iSide);
+		int	iCam = m_pHardware->GetCameraIndexToDimension((DimensionDir)iSide);
+		int	iScan = m_pHardware->GetScanToDimension((DimensionDir)iSide);
 
 		CCameraSettings *pCamera = m_pHardware->GetCameraSettings(iCam,iScan);
-		if(pCamera == NULL)
+		if (pCamera == NULL) {
 			return;
+		}
 
 		nTotalLength = (pCamera->m_nGrabFrameCount-1)*pCamera->m_FrameSize.cy;
-		//nFrame = nTotalLength/nOneFrameSize;
 
 		ptStart.x = 0;
-		ptStart.y =  max(0,0);
+		ptStart.y = max(0,0);
 
-		int			nMaxSize = (pCamera->m_nGrabFrameCount-1)*pCamera->m_FrameSize.cy;
+		int	nMaxSize = (pCamera->m_nGrabFrameCount-1)*pCamera->m_FrameSize.cy;
 
 		CSide_Data* pSideData = m_pGlassData->GetSideData((DimensionDir)iSide);
-		if(pSideData != NULL)
-		{
+		if(pSideData != NULL) {
 			ptStart.y = max(0,pSideData->m_nGlassStartLine-1000);
-
 			nTotalLength = min((int)((pSideData->m_nPreGlassEndLine + 1000) - ptStart.y),nMaxSize);
 		}	
 
-		if(nTotalLength < 10000)
+		if (nTotalLength < 10000) {
 			nTotalLength = nMaxSize;
+		}
 
-		nFrame = nTotalLength/nOneFrameSize;
-		if(ptStart.y < 0)
+		nFrame = nTotalLength / nOneFrameSize;
+		if (ptStart.y < 0) {
 			ptStart.y = 0;
-		
-		for(int i = 0; i < nFrame ; i++)
-		{
+		}
+
+		for(int i = 0; i < nFrame ; i++) {
 			nLength = nOneFrameSize;
 
-			if(ptStart.y+nLength >= nMaxSize)
+			if (ptStart.y + nLength >= nMaxSize) {
 				continue;
+			}
 
 			strPath.Format(_T("%s\\DebugFullImage\\%s\\%s\\%s\\%s\\%s__%s_%d"),PATH_INSPECTION_DATA,strDate,strRcpName,strGlassID,strTime,strGlassID,PANEL_SIDE[iSide],i);
-
 			m_IV2M->IV2M_SaveFullImage(strPath,iSide,ptStart,nLength);		
-
 			g_pLog->DisplayMessage(_T("%s %d Save Completed : %s"),PANEL_SIDE[iSide],i,strPath);
 
 			ptStart.y += nLength; 
 		}
 
 		nLength = nTotalLength%nOneFrameSize;
-		if(nLength > 10)
-		{		
-			if(ptStart.y+nLength < nMaxSize)
-			{
+		if(nLength > 10) {		
+			if(ptStart.y+nLength < nMaxSize) {
 				strPath.Format(_T("%s\\DebugFullImage\\%s\\%s\\%s\\%s\\%s__%s_%d"),PATH_INSPECTION_DATA,strDate,strRcpName,strGlassID,strTime,strGlassID,PANEL_SIDE[iSide],nFrame);		
-
 				m_IV2M->IV2M_SaveFullImage(strPath,iSide,ptStart,nLength);		
 			}			
 		}
@@ -1272,18 +1264,20 @@
 	int nTotalLength = 0;
 	int nFrame = 0;
 
-	if(m_pGlassData == NULL || m_IV2M == NULL)
+	if (m_pGlassData == NULL || m_IV2M == NULL) {
 		return;
-	
+	}
+
 	CGlassRecipe *pGlassRcp = m_pGlassData->GetGlassRecipe();
 	CString		strRcpName = pGlassRcp->GetRecipeName();
 
-	int			iCam = m_pHardware->GetCameraIndexToDimension((DimensionDir)iSide);
-	int			iScan = m_pHardware->GetScanToDimension((DimensionDir)iSide);
+	int	iCam = m_pHardware->GetCameraIndexToDimension((DimensionDir)iSide);
+	int	iScan = m_pHardware->GetScanToDimension((DimensionDir)iSide);
 
 	CCameraSettings *pCamera = m_pHardware->GetCameraSettings(iCam,iScan);
-	if(pCamera == NULL)
+	if (pCamera == NULL) {
 		return;
+	}
 
 	nTotalLength = pCamera->m_nGrabFrameCount*pCamera->m_FrameSize.cy;
 	nFrame = nTotalLength/nOneFrameSize;
@@ -1292,35 +1286,30 @@
 	ptStart.y =  max(0,0);
 
 	CSide_Data *pSideData = m_pGlassData->GetSideData((DimensionDir)iSide);
-	if(pSideData != NULL)
-	{
+	if(pSideData != NULL) {
 		ptStart.y = max(0,pSideData->m_nGlassStartLine-1000);
 
 		nTotalLength = min((pSideData->m_nGlassEndLine + 1000) - ptStart.y,pCamera->m_nGrabFrameCount*pCamera->m_FrameSize.cy);
 	}	
 	
 	CString			strGlassID = g_pBase->m_strHPanelID;
-	if(strGlassID.IsEmpty() == TRUE)
-	{
+	if(strGlassID.IsEmpty() == TRUE) {
 		strGlassID = _T("Manual_ID");
 	}
 
-	if(strRcpName.IsEmpty() == TRUE)
-	{
+	if(strRcpName.IsEmpty() == TRUE) {
 		strRcpName = _T("TestRcp");
 	}
 
 	strGlassID.Trim(_T(' '));
 
 	CTime	time = CTime::GetCurrentTime();
-
-	CString			strDate,strTime;
+	CString	strDate,strTime;
 
 	strDate.Format(_T("%04d%02d%02d"),time.GetYear(),time.GetMonth(),time.GetDay());
 	strTime.Format(_T("%02d%02d%02d"),time.GetHour(),time.GetMinute(),time.GetSecond());
 
-	for(int i = 0; i < nFrame ; i++)
-	{
+	for(int i = 0; i < nFrame ; i++) {
 		nLength = nOneFrameSize;
 	
 		strPath.Format(_T("%s\\DebugFullImage\\%s\\%s\\%s\\%s\\%s__%s_%d"),PATH_INSPECTION_DATA,strDate,strRcpName,strGlassID,strTime,strGlassID,PANEL_SIDE[iSide],i);
@@ -1331,18 +1320,14 @@
 		g_pLog->DisplayMessage(_T("%s %d Save Completed : %s"),PANEL_SIDE[iSide],i,strPath);
 	}
 
-	
-	nLength = nTotalLength%nOneFrameSize;
-	if(nLength)
-	{		
+	nLength = nTotalLength % nOneFrameSize;
+	if(nLength) {		
 		strPath.Format(_T("%s\\DebugFullImage\\%s\\%s\\%s\\%s\\%s__%s_%d"),PATH_INSPECTION_DATA,strDate,strRcpName,strGlassID,strTime,strGlassID,PANEL_SIDE[iSide],nFrame);		
 
 		m_IV2M->IV2M_SaveFullImage(strPath,iSide,ptStart,nLength);		
 
 		g_pLog->DisplayMessage(_T("%s %d Save Completed : %s"),PANEL_SIDE[iSide],nFrame,strPath);
 	}
-
-
 }
 
 UINT CViewMain_ScanImage::ManualProcessAll(LPVOID pParam)

--
Gitblit v1.9.3