mrDarker
2025-07-14 1c0ac1c7924efb8a2cb6962d3eda4126533a5ac8
ENRIT/View/ViewScanImage.cpp
@@ -34,7 +34,10 @@
   m_nThreadCmd   = 0;
   m_IV2M = NULL;
   m_pHardware = NULL;
   m_pGlassData = NULL;
   m_pGlassData = NULL;
   m_nCurrentFolderIndex = 0;
   m_bIsBatchProcessing = FALSE;
}
CViewScanImage::~CViewScanImage()
@@ -160,6 +163,8 @@
   ON_EVENT(CViewScanImage, IDC_BTN_PROC, DISPID_CLICK, CViewScanImage::OnBnClickedBtnProc, VTS_NONE)
   ON_EVENT(CViewScanImage, IDC_BTN_LOAD_ALL, DISPID_CLICK, CViewScanImage::OnBnClickedLoadAll, VTS_NONE)
   ON_EVENT(CViewScanImage, IDC_SAVE_ALL, DISPID_CLICK, CViewScanImage::OnBnClickedSaveAll, VTS_NONE)
   ON_EVENT(CViewScanImage, IDC_BTN_START_BATCH, DISPID_CLICK, CViewScanImage::OnBtnStartBatch, VTS_NONE)
   ON_EVENT(CViewScanImage, IDC_BTN_STOP_BATCH, DISPID_CLICK, CViewScanImage::OnBtnStopBatch, VTS_NONE)
END_EVENTSINK_MAP()
@@ -345,6 +350,18 @@
      m_frameImg[nIndex].ViewUpdate();
   }
   return 1;
}
LRESULT CViewScanImage::OnBatchProcessNext(WPARAM wParam, LPARAM lParam)
{
   if (!m_bIsBatchProcessing) {
      return 0;
   }
   m_nCurrentFolderIndex++;
   ProcessNextFolder();
   return 0;
}
void CViewScanImage::SetRecipe(CHardwareSettings *pHW)
@@ -636,7 +653,6 @@
   scrollInfoH.nTrackPos   = 64;
   
   double dZoom = m_frameImg[0].GetZoomData();
   scrollInfoV.nPage   = m_frameImg[0].m_WndRect.Height();
   scrollInfoH.nPage   = (int)(m_frameImg[0].m_WndRect.Width() / dZoom);   
@@ -1043,22 +1059,58 @@
void CViewScanImage::OnBnClickedLoadAll()  {ManualThreadStart(M_CMD_IMG_LOAD_ALL,-1);}
void CViewScanImage::OnBnClickedSaveAll()  {ManualThreadStart(M_CMD_IMG_SAVE_ALL,-1);}
void CViewScanImage::OnBtnStartBatch()
{
   // 启动批处理
   CFolderPickerDialog dlg(NULL, OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT, this, 0);
   if (dlg.DoModal() == IDOK) {
      CString strFolderPath = dlg.GetPathName();
      m_bIsBatchProcessing = TRUE;
      StartBatchProcess(strFolderPath);
   }
}
void CViewScanImage::OnBtnStopBatch()
{
   // 停止批处理
   m_bIsBatchProcessing = FALSE;
}
BOOL CViewScanImage::ManualThreadStart(int nCmd,int nIndex /*= -1*/)
{
   if(m_pManualProcess)
   {
      DWORD dwExitCode;
      dwExitCode=::WaitForSingleObject(m_pManualProcess->m_hThread, 1000);
      if((::GetExitCodeThread(m_pManualProcess->m_hThread ,&dwExitCode))&& (dwExitCode == STILL_ACTIVE))
      {
         ::TerminateThread(m_pManualProcess->m_hThread, dwExitCode);
   if (m_pManualProcess) {
      DWORD dwWait = ::WaitForSingleObject(m_pManualProcess->m_hThread, 3000);
      if (dwWait == WAIT_TIMEOUT) {
         DWORD dwExitCode;
         if ((::GetExitCodeThread(m_pManualProcess->m_hThread, &dwExitCode)) && (dwExitCode == STILL_ACTIVE)) {
            ::TerminateThread(m_pManualProcess->m_hThread, dwExitCode);
            m_pManualProcess = NULL;
            g_pLog->DisplayMessage(_T("Previous manual process was terminated due to timeout."));
         }
      }
      else {
         m_pManualProcess = NULL;
         if (dwWait == WAIT_OBJECT_0) {
            g_pLog->DisplayMessage(_T("Previous manual process completed successfully."));
         }
         else {
            g_pLog->DisplayMessage(_T("Previous manual process was terminated or failed."));
         }
      }
   }
   m_nIndex      = nIndex;
   m_nThreadCmd   = nCmd;
   m_pManualProcess = AfxBeginThread(ManualProcessAll,this);
   if (m_pManualProcess != NULL) {
      g_pLog->DisplayMessage(_T("Manual Process is already running. Please wait until it finishes."));
      return FALSE;
   }
   m_nIndex = nIndex;
   m_nThreadCmd = nCmd;
   m_pManualProcess = AfxBeginThread(ManualProcessAll, this);
   if (!m_pManualProcess) {
      g_pLog->DisplayMessage(_T("Failed to start manual process thread."));
      return FALSE;
   }
   return TRUE;
}
@@ -1260,8 +1312,88 @@
      g_pLog->DisplayMessage(_T("%s %d Save Completed : %s"),PANEL_SIDE[iSide],nFrame,strPath);
   }
}
void CViewScanImage::LoadAndProcessImages(const CString& strFolderPath)
{
   if (m_IV2M != NULL) {
      // 加载图像
      m_IV2M->IV2M_LoadImageFromFolder(-1, strFolderPath, m_pGlassData, [&](BYTE iSide) {
         int iView = GetViewIndex(iSide);
         InitScrollInfo(iSide, 0);
         m_frameImg[iView].Invalidate(FALSE);
         m_frameImg[iView].Invalidate();
      });
      for (int i = 0; i < 8; i++) {
         InitScrollInfo(i, 0);
         int nSide = GetSideIdx(i);
         m_frameImg[i].SetSide(nSide);
         m_frameImg[i].SetImage(GetBufferPoint(i, 0));
         m_frameImg[i].ViewUpdate();
      }
      if (m_IV2M != NULL) {
         m_IV2M->IV2M_InitView(VIEW_SCANIMAGE);
      }
      // 执行处理线程
      m_nIndex = -1;
      switch (m_nShowSelect)
      {
      case 0:
         m_IV2M->IV2M_InspectionProcessAll(M_CMD_PROC_ALL_START);
         break;
      case 1:
         m_IV2M->IV2M_InspectionProcessAll(M_CMD_PROC_LONG_START);
         break;
      case 2:
         m_IV2M->IV2M_InspectionProcessAll(M_CMD_PROC_SHORT_START);
         break;
      }
   }
}
void CViewScanImage::StartBatchProcess(const CString& strRootFolder)
{
   m_arrFolderList.RemoveAll();
   // 搜索所有子文件夹
   CFileFind finder;
   CString strSearchPath = strRootFolder + _T("\\*.*");
   BOOL bWorking = finder.FindFile(strSearchPath);
   while (bWorking) {
      bWorking = finder.FindNextFile();
      // 排除 . 和 .. 以及非文件夹
      if (finder.IsDots() || !finder.IsDirectory()) {
         continue;
      }
      m_arrFolderList.Add(finder.GetFilePath());
   }
   finder.Close();
   // 从第一个开始处理
   m_nCurrentFolderIndex = 0;
   ProcessNextFolder();
}
void CViewScanImage::ProcessNextFolder()
{
   if (!m_bIsBatchProcessing) {
      return;
   }
   if (m_nCurrentFolderIndex < m_arrFolderList.GetSize()) {
      CString strFolder = m_arrFolderList[m_nCurrentFolderIndex];
      LoadAndProcessImages(strFolder);
   }
   else {
      m_arrFolderList.RemoveAll();
      m_bIsBatchProcessing = FALSE;
   }
}
UINT CViewScanImage::ManualProcessAll(LPVOID pParam)
@@ -1286,14 +1418,6 @@
         pDlg->m_frameImg[iView].Invalidate(FALSE);
         pDlg->m_frameImg[iView].Invalidate();
      });
      //CString strFolderPath = _T("D:\\备份\\光检机\\EI、Rcut\\RCUT\\图片\\n8_NG");
      //pDlg->m_IV2M->IV2M_LoadImageFromFolder(-1, strFolderPath, pDlg->m_pGlassData, [&](BYTE iSide) {
      //   int iView = pDlg->GetViewIndex(iSide);
      //   pDlg->InitScrollInfo(iSide, 0);
      //   pDlg->m_frameImg[iView].Invalidate(FALSE);
      //   pDlg->m_frameImg[iView].Invalidate();
      //});
   }
   else if(pDlg->m_nThreadCmd == M_CMD_IMG_SAVE_ALL)
   {
@@ -1385,15 +1509,16 @@
   case 0: 
      ManualThreadStart(M_CMD_PROC_ALL_START   );
      break;
   case 1: 
      ManualThreadStart(M_CMD_PROC_LONG_START   );    
      break;
   case 2:
      ManualThreadStart(M_CMD_PROC_SHORT_START);  
      break;
   }
   //CString strFolderPath = _T("D:\\备份\\光检机\\EI、Rcut\\RCUT\\图片\\n8_NG");
   //LoadAndProcessImages(strFolderPath);
}
void CViewScanImage::OnBnClickedCheckAutoSize()