重构:保存图片逻辑,替换 IplImage 为 cv::Mat
- 将 保存图片中的 IplImage 接口升级为现代 OpenCV cv::Mat 实现
- 添加图像指针、尺寸等边界条件校验,避免空指针和非法尺寸写入
- 简化图像转换与保存流程,提高代码可维护性
- 保留 JPEG 压缩质量参数,保持原有功能不变
| | |
| | | #include <string> |
| | | #include <time.h> |
| | | |
| | | #define OFFLINE_KEY 0 //OFFLINE_KEY:1是离线模式;0是在线模式 |
| | | #define OFFLINE_KEY 1 //OFFLINE_KEY:1是离线模式;0是在线模式 |
| | | #define MINI_LED 0 //Mini车间 |
| | | #define MINI_NOTCH 0 //Mini车间, 是否使用刻槽功能 |
| | | #define HALCON_VISION_KEY 0 //是否启用HALCON |
| | |
| | | CPoint pos2I = m_pGlassData->TransformToGlobal((DimensionDir)pDefect->m_DefectInfo.m_nSideIdx, pDefect->m_DefectInfo.m_ptDefectPos_pxl); |
| | | #endif // HALCON_VISION_KEY |
| | | |
| | | |
| | | |
| | | |
| | | int nMinSize_X = m_pRecipe->m_SideParam[pDefect->m_DefectInfo.m_nSideIdx].m_InspectPrm[(int)pDefect->m_DefectInfo.m_DefectLoc].m_nMinSize_Filter_X_um; |
| | | int nMinSize_Y = m_pRecipe->m_SideParam[pDefect->m_DefectInfo.m_nSideIdx].m_InspectPrm[(int)pDefect->m_DefectInfo.m_DefectLoc].m_nMinSize_Filter_Y_um;; |
| | | |
| | |
| | | |
| | | BOOL CPostProcess::SaveDefectImage(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile,DimensionDir emDir) |
| | | { |
| | | FIBITMAP *bitmap = NULL; |
| | | BYTE *pBitBuffer; |
| | | if (pImage == NULL || szImage.cx <= 0 || szImage.cy <= 0) { |
| | | g_pLog->DisplayMessage(_T("Invalid image data or size")); |
| | | return FALSE; |
| | | } |
| | | |
| | | bitmap = FreeImage_Allocate(szImage.cx, szImage.cy,8); |
| | | Lock(); |
| | | FIBITMAP* bitmap = FreeImage_Allocate(szImage.cx, szImage.cy, 8); |
| | | if (bitmap == NULL) { |
| | | g_pLog->DisplayMessage(_T("FreeImage_Allocate failed")); |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | pBitBuffer = FreeImage_GetBits(bitmap); |
| | | if(pBitBuffer == NULL) |
| | | { |
| | | FreeImage_Unload(bitmap); |
| | | BYTE* pBitBuffer = FreeImage_GetBits(bitmap); |
| | | if(pBitBuffer == NULL) { |
| | | FreeImage_Unload(bitmap); |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | CopyMemory(pBitBuffer,pImage,szImage.cx*szImage.cy); |
| | | CopyMemory(pBitBuffer, pImage, szImage.cx * szImage.cy); |
| | | |
| | | FIBITMAP *pBit24 = FreeImage_ConvertTo24Bits(bitmap); |
| | | if(pBit24 == NULL) |
| | | if (pBit24 == NULL) { |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | char cFilePath[255]; |
| | | |
| | | #ifdef _UNICODE |
| | | int nSize = WideCharToMultiByte(CP_ACP, 0, strFile.GetBuffer(), -1, NULL, 0, NULL,NULL); |
| | | int nSize = WideCharToMultiByte(CP_ACP, 0, strFile.GetBuffer(), -1, NULL, 0, NULL,NULL); |
| | | WideCharToMultiByte(CP_ACP, 0, strFile.GetBuffer(), -1, cFilePath, nSize, NULL, NULL); |
| | | #else |
| | | sprintf_s(cFilePath,sizeof(char)*255,_T("%s"),(LPSTR)(LPCTSTR)strFile); |
| | | #endif |
| | | |
| | | FreeImage_FlipVertical(pBit24); |
| | | |
| | | FreeImage_Save(FIF_JPEG, pBit24, cFilePath,JPEG_RATE); |
| | | |
| | | FreeImage_Unload(bitmap); |
| | | FreeImage_Unload(pBit24); |
| | | |
| | | Unlock(); |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | BOOL CPostProcess::SaveDefectImage2(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile) |
| | | BOOL CPostProcess::SaveDefectImageModern(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile) |
| | | { |
| | | IplImage *pImageTmp = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8,1); |
| | | if (!pImage || szImage.cx <= 0 || szImage.cy <= 0) { |
| | | return FALSE; |
| | | } |
| | | |
| | | #if 0 |
| | | IplImage* pImageTmp = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8, 1); |
| | | cvZero(pImageTmp); |
| | | |
| | | |
| | | // add point to the image |
| | | for (int i = 0; i<szImage.cy; i++) |
| | | { |
| | | for (int j = 0; j<szImage.cx; j++) |
| | | { |
| | | pImageTmp->imageData[i*pImageTmp->widthStep+j] = pImage[i*szImage.cx+j]; |
| | | for (int i = 0; i < szImage.cy; i++) { |
| | | for (int j = 0; j < szImage.cx; j++) { |
| | | pImageTmp->imageData[i * pImageTmp->widthStep + j] = pImage[i * szImage.cx + j]; |
| | | } |
| | | } |
| | | |
| | | IplImage *pColorImg = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8, 3); |
| | | IplImage* pColorImg = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8, 3); |
| | | cvZero(pColorImg); |
| | | |
| | | cvCvtColor(pImageTmp, pColorImg, CV_GRAY2BGR); |
| | | cvRectangle(pColorImg, CvPoint(rectDefect.left, rectDefect.top), CvPoint(rectDefect.left + rectDefect.Width(), rectDefect.top + rectDefect.Height()), cvScalar(0, 0, 255), 1); |
| | | |
| | | cvRectangle(pColorImg,CvPoint(rectDefect.left,rectDefect.top), CvPoint(rectDefect.left+rectDefect.Width(),rectDefect.top+rectDefect.Height()), cvScalar(0,0,255), 1); |
| | | |
| | | USES_CONVERSION; |
| | | USES_CONVERSION; |
| | | char str_filename[1024]; |
| | | sprintf_s(str_filename, "%s", W2A(strFile)); |
| | | cvSaveImage(str_filename, pColorImg); |
| | | |
| | | cvReleaseImage(&pColorImg); |
| | | cvReleaseImage(&pImageTmp); |
| | | #else |
| | | Lock(); |
| | | cv::Mat grayImg(szImage.cy, szImage.cx, CV_8UC1, pImage); |
| | | cv::Mat colorImg; |
| | | cv::cvtColor(grayImg, colorImg, cv::COLOR_GRAY2BGR); |
| | | |
| | | cv::rectangle( |
| | | colorImg, |
| | | cv::Rect(rectDefect.left, rectDefect.top, rectDefect.Width(), rectDefect.Height()), |
| | | cv::Scalar(0, 0, 255), |
| | | 1 |
| | | ); |
| | | |
| | | USES_CONVERSION; |
| | | std::string strFilename = W2A(strFile); |
| | | |
| | | if (!cv::imwrite(strFilename, colorImg)) { |
| | | Unlock(); |
| | | g_pLog->DisplayMessage(_T("Failed to save image to %s"), strFile); |
| | | return FALSE; |
| | | } |
| | | Unlock(); |
| | | #endif // 0 |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | BOOL CPostProcess::SaveDefectImage_with_Title(CDefect* pDefect, CSize szImage,CRect rectDefect,CString strFile, CvScalar color) |
| | | { |
| | | Lock(); |
| | | if (pDefect == NULL || pDefect->m_Image == NULL) { |
| | | g_pLog->DisplayMessage(_T("Invalid defect data or image")); |
| | | return FALSE; |
| | | } |
| | | |
| | | if (szImage.cx <= 0 || szImage.cy <= 0) { |
| | | g_pLog->DisplayMessage(_T("Invalid image size: %d x %d"), szImage.cx, szImage.cy); |
| | | return FALSE; |
| | | } |
| | | |
| | | BOOL bSuccess = TRUE; |
| | | |
| | | Lock(); |
| | | // 标题文本 |
| | | CString strTitle; |
| | | int nSideIdx = (0 <= pDefect->m_DefectInfo.m_nSideIdx && pDefect->m_DefectInfo.m_nSideIdx < MAX_SIDE_COUNT) ? pDefect->m_DefectInfo.m_nSideIdx : MAX_SIDE_COUNT; |
| | |
| | | } |
| | | Unlock(); |
| | | |
| | | // 成功标志 |
| | | BOOL bSuccess = TRUE; |
| | | |
| | | try { |
| | | // 将 CDefect 的 m_Image 缓冲区复制到 cv::Mat |
| | | Lock(); |
| | | cv::Mat imgTmp(szImage.cy, szImage.cx, CV_8UC1, pDefect->m_Image); |
| | | |
| | | // 创建彩色图像 |
| | | cv::Mat colorImg(szImage.cy, szImage.cx, CV_8UC3); |
| | | cv::cvtColor(imgTmp, colorImg, cv::COLOR_GRAY2BGR); |
| | | |
| | | // 在图像上画矩形 |
| | | cv::rectangle(colorImg, cv::Point(rectDefect.left, rectDefect.top), |
| | | cv::Point(rectDefect.right, rectDefect.bottom), color, 1); |
| | | cv::rectangle(colorImg, cv::Point(rectDefect.left, rectDefect.top), cv::Point(rectDefect.right, rectDefect.bottom), color, 1); |
| | | cv::rectangle(colorImg, cv::Point(0, szImage.cy - 40), cv::Point(szImage.cx, szImage.cy), cv::Scalar(0, 0, 0), cv::FILLED); |
| | | |
| | | // 添加标题 |
| | | cv::rectangle(colorImg, cv::Point(0, szImage.cy - 40), |
| | | cv::Point(szImage.cx, szImage.cy), cv::Scalar(0, 0, 0), cv::FILLED); |
| | | |
| | | // 设置字体 |
| | | int fontFace = cv::FONT_HERSHEY_TRIPLEX; |
| | | double fontScale = 0.7; |
| | | int thickness = 1; |
| | | |
| | | // 将标题文本放到图像上 |
| | | CT2CA pszConvertedAnsiTitle(strTitle); |
| | | cv::String strCVTitle(pszConvertedAnsiTitle); |
| | | cv::putText(colorImg, strCVTitle, cv::Point(10, szImage.cy - 10), |
| | | fontFace, fontScale, CV_RGB(255, 255, 255), thickness); |
| | | cv::putText(colorImg, strCVTitle, cv::Point(10, szImage.cy - 10), fontFace, fontScale, CV_RGB(255, 255, 255), thickness); |
| | | |
| | | // 将图像保存到文件 |
| | | CT2CA pszConvertedAnsiFile(strFile); |
| | | cv::String strStdFile(pszConvertedAnsiFile); |
| | | cv::imwrite(strStdFile, colorImg); |
| | | // cv::imwrite("D:\\Inspection\\Data\\IMG_DEFECT\\20250409\\Manual\\Manual_A_TOP_Chip_0.bmp", colorImg); |
| | | Unlock(); |
| | | } |
| | | catch (cv::Exception& e) { |
| | | // OpenCV异常处理 |
| | | bSuccess = FALSE; |
| | | Unlock(); |
| | | g_pLog->DisplayMessage(_T("OpenCV Exception: %s"), e.what()); |
| | | } |
| | | return bSuccess; |
| | | |
| | |
| | | BOOL DefectProcess_Judge(CDefect *pDefect); |
| | | BOOL DefectProcess(CDefect* pDefect,CInspectCamera *pInspect, CCameraSettings* pCamera); |
| | | BOOL SaveDefectImage(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile,DimensionDir emDir); |
| | | BOOL SaveDefectImage2(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile); |
| | | BOOL SaveDefectImageModern(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile); |
| | | BOOL SaveDefectImage_with_Title(CDefect* pDefect, CSize szImage, CRect rectDefect, CString strFile, CvScalar color = cvScalar(0, 0, 255)); |
| | | int MeasrueCutLineResult(DimensionDir dir); |
| | | |
| | |
| | | pINSPECTFULLIMAGE_BUFFER pBuffer; |
| | | int nQuality = pHard->m_nSaveImageQuality; |
| | | |
| | | if(nQuality < 0 || nQuality > 100) |
| | | nQuality = 30; |
| | | |
| | | if (nQuality < 0 || nQuality > 100) { |
| | | nQuality = 30; |
| | | } |
| | | |
| | | strFolder.Format(_T("%s"),m_PostProcess.GetResultPath(EM_RESULT_FULL)); |
| | | CreateDirectory(strFolder,NULL); |
| | | |
| | | strPanelID = g_pBase->m_strHPanelID; |
| | | if(strPanelID.IsEmpty() == TRUE) |
| | | if (strPanelID.IsEmpty() == TRUE) { |
| | | strPanelID = _T("TestGlass"); |
| | | } |
| | | |
| | | for(iCam=0;iCam<pHard->GetCameraCount();iCam++) |
| | | { |
| | | for(iScan=0;iScan<MAX_SCAN_COUNT;iScan++) |
| | | { |
| | | for(iCam=0;iCam<pHard->GetCameraCount();iCam++) { |
| | | for(iScan=0;iScan<MAX_SCAN_COUNT;iScan++) { |
| | | pCamera = pHard->GetCameraSettings(iCam,iScan); |
| | | if(pCamera == NULL) |
| | | if (pCamera == NULL) { |
| | | continue; |
| | | } |
| | | |
| | | pBuffer = m_pInspect[iCam]->GetFullImgBuffer(iScan); |
| | | if(pBuffer == NULL) |
| | | if (pBuffer == NULL) { |
| | | continue; |
| | | } |
| | | |
| | | g_pLog->DisplayMessage(_T("Sava %s iCam=%d, iScan=%d, szImage.cx=%ld, pBuffer->szImage.cy=%ld"), |
| | | PANEL_SIDE[pCamera->m_eDimension], iCam, iScan, pBuffer->szImage.cx, pBuffer->szImage.cy); |
| | | g_pLog->DisplayMessage(_T("Sava %s iCam=%d, iScan=%d, szImage.cx=%ld, pBuffer->szImage.cy=%ld"), PANEL_SIDE[pCamera->m_eDimension], iCam, iScan, pBuffer->szImage.cx, pBuffer->szImage.cy); |
| | | |
| | | if(pBuffer->pImage != NULL && pBuffer->szImage.cx > 100 && pBuffer->szImage.cy > 100) |
| | | { |
| | | if(pBuffer->pImage != NULL && pBuffer->szImage.cx > 100 && pBuffer->szImage.cy > 100) { |
| | | strFileJpg.Format(_T("%s\\%s_%s_%s.jpg"),strFolder, strPanelID, PANEL_SIDE[pCamera->m_eDimension],g_pBase->m_strLoadingTime); |
| | | SaveFullImage2(strFileJpg, pBuffer->pImage, pBuffer->szImage.cx, pBuffer->szImage.cy, (int)pCamera->m_eDimension, 0, nQuality); |
| | | SaveFullImageModern(strFileJpg, pBuffer->pImage, pBuffer->szImage.cx, pBuffer->szImage.cy, (int)pCamera->m_eDimension, 0, nQuality); |
| | | } |
| | | } |
| | | } |
| | |
| | | { |
| | | g_pStatus->CheckDirectory(strPath); |
| | | |
| | | FIBITMAP *bitmap = NULL; |
| | | BYTE *pBitBuffer; |
| | | BYTE* pBitBuffer; |
| | | FIBITMAP *bitmap = NULL; |
| | | |
| | | CString savePath = strPath; |
| | | savePath += _T(".jpg"); |
| | | CString strSavePath = strPath; |
| | | strSavePath += _T(".jpg"); |
| | | |
| | | bitmap = FreeImage_Allocate(IMAGE_WIDTH, nLineCnt,8); |
| | | |
| | | pBitBuffer = FreeImage_GetBits(bitmap); |
| | | if(pBitBuffer == NULL) |
| | | { |
| | | if(pBitBuffer == NULL) { |
| | | FreeImage_Unload(bitmap); |
| | | return FALSE; |
| | | } |
| | | |
| | | LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide,ptStart.y); |
| | | if(lpImg == NULL) |
| | | if (lpImg == NULL) { |
| | | return FALSE; |
| | | } |
| | | |
| | | CopyMemory(pBitBuffer,lpImg,IMAGE_WIDTH*nLineCnt); |
| | | CopyMemory(pBitBuffer, lpImg, IMAGE_WIDTH * nLineCnt); |
| | | |
| | | USES_CONVERSION; |
| | | char str_filename[1024]; |
| | | sprintf_s(str_filename, "%s", W2A(savePath)); |
| | | sprintf_s(str_filename, "%s", W2A(strSavePath)); |
| | | |
| | | FreeImage_FlipVertical(bitmap); |
| | | |
| | | FreeImage_Save(FIF_JPEG, bitmap, str_filename,JPEG_FULLIMAGE_RATE); |
| | | |
| | | FreeImage_Unload(bitmap); |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | BOOL CInterfaceManager::SaveFullImage2(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality /*= 80*/) |
| | | BOOL CInterfaceManager::SaveFullImageModern(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality /*= 80*/) |
| | | { |
| | | if(lpOrigin == NULL) |
| | | return FALSE; |
| | | |
| | | if(nImgHeight < 100) |
| | | { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Height is 0"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | if (!lpOrigin || nImgHeight < 100) { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Invalid image or height"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | return FALSE; |
| | | } |
| | | |
| | | //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 1"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | |
| | | g_pStatus->CheckDirectory(strPath); |
| | | |
| | | //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 2"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | |
| | | int p[3]; |
| | | p[0] = CV_IMWRITE_JPEG_QUALITY; |
| | |
| | | double tmp = 70 / 100.0; |
| | | double dRatio = 1. - tmp; |
| | | dRatio = dRatio - 0.01 < 0.0 ? 1.0 : dRatio; |
| | | IplImage *Img = cvCreateImageHeader(cvSize(nImgWidth,nImgHeight),8,1); |
| | | IplImage *img2 = cvCreateImage(cvSize((int)(nImgWidth* dRatio),(int)(nImgHeight*dRatio)),8,1); |
| | | |
| | | //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 3"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | #if 0 |
| | | IplImage* pImg = cvCreateImageHeader(cvSize(nImgWidth, nImgHeight), 8, 1); |
| | | IplImage* pImgNew = cvCreateImage(cvSize((int)(nImgWidth * dRatio), (int)(nImgHeight * dRatio)), 8, 1); |
| | | |
| | | if(Img == NULL || img2 == NULL) |
| | | { |
| | | if (pImg == NULL || pImgNew == NULL) { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), img pointer is null"), strPath, PANEL_SIDE[iSide], nStartY); |
| | | return FALSE; |
| | | } |
| | | |
| | | g_pLog->DisplayMessage(_T("Save Full Image Start(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality); |
| | | g_pLog->DisplayMessage(_T("Save Full Image Start(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality); |
| | | |
| | | if(nStartY < 0 || iSide < 0 || iSide >= MAX_DIMENSION_COUNT) |
| | | { |
| | | if (nStartY < 0 || iSide < 0 || iSide >= MAX_DIMENSION_COUNT) { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d)"), strPath, PANEL_SIDE[iSide], nStartY); |
| | | return FALSE; |
| | | } |
| | | |
| | | LPBYTE lpImg = lpOrigin+(nStartY*nImgWidth); |
| | | if(lpImg == NULL) |
| | | { |
| | | LPBYTE lpImg = lpOrigin + (nStartY * nImgWidth); |
| | | if (lpImg == NULL) { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Buffer is NULL"), strPath, PANEL_SIDE[iSide], nStartY); |
| | | return FALSE; |
| | | } |
| | | |
| | | //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 4"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | cvSetData(pImg, lpImg, nImgWidth); |
| | | |
| | | cvSetData(Img,lpImg,nImgWidth); |
| | | |
| | | cvResize(Img,img2); |
| | | cvResize(pImg, pImgNew); |
| | | USES_CONVERSION; |
| | | char str_filename[200]; |
| | | sprintf_s(str_filename, "%s", W2A(strPath)); |
| | | cvSaveImage(str_filename,img2, p); |
| | | cvSaveImage(str_filename, pImgNew, p); |
| | | |
| | | //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 5"), strPath, PANEL_SIDE[iSide], nImgHeight); |
| | | cvReleaseImage(&pImgNew); |
| | | cvReleaseImageHeader(&pImg); |
| | | #else |
| | | // 原始图像数据转 cv::Mat |
| | | cv::Mat origin(nImgHeight, nImgWidth, CV_8UC1, lpOrigin + (nImgWidth * nStartY)); |
| | | |
| | | cvReleaseImage(&img2); |
| | | cvReleaseImageHeader(&Img); |
| | | // 缩放图像 |
| | | cv::Size szNew(static_cast<int>(nImgWidth * dRatio), static_cast<int>(nImgHeight * dRatio)); |
| | | cv::Mat resized; |
| | | cv::resize(origin, resized, szNew, 0, 0, cv::INTER_LINEAR); |
| | | |
| | | // 设置 JPEG 压缩参数 |
| | | std::vector<int> params = { |
| | | cv::IMWRITE_JPEG_QUALITY, |
| | | nQuality |
| | | }; |
| | | |
| | | USES_CONVERSION; |
| | | std::string strFilename = W2A(strPath); |
| | | |
| | | if (!cv::imwrite(strFilename, resized, params)) { |
| | | g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), imwrite failed"), strPath, PANEL_SIDE[iSide], nStartY); |
| | | return FALSE; |
| | | } |
| | | #endif |
| | | |
| | | g_pLog->DisplayMessage(_T("Save Full Image Success(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality); |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | #define IMAGE_COMPRESSRATIO 50 |
| | | #define IMAGE_COMPRESSRATIO 50 |
| | | int CInterfaceManager::LoadFullImage(CString strPath,int iSide,int nStartY, CGlass_Data *pGlassData,const bool bIsDebugImg) |
| | | { |
| | | int nHeight = -1; |
| | | int nHeight = -1; |
| | | if (strPath.IsEmpty() || iSide < 0 || iSide > MAX_DIMENSION_COUNT) { |
| | | g_pLog->DisplayMessage(_T("Load Full Image Fail(%s, %d)"), strPath, iSide); |
| | | return nHeight; |
| | | } |
| | | |
| | | #if 0 |
| | | USES_CONVERSION; |
| | | char str_filename[200]; |
| | | sprintf_s(str_filename, "%s", W2A(strPath)); |
| | | |
| | | IplImage *Iplimg = cvLoadImage(str_filename, CV_LOAD_IMAGE_GRAYSCALE); |
| | | LPBYTE lpImg = NULL; |
| | | LPBYTE lpImg = NULL; |
| | | IplImage *pIplimg = cvLoadImage(str_filename, CV_LOAD_IMAGE_GRAYSCALE); |
| | | |
| | | if(Iplimg) |
| | | { |
| | | if(bIsDebugImg == true) //Debug image |
| | | { |
| | | if(pIplimg) { |
| | | if(bIsDebugImg == true) { |
| | | //Debug image |
| | | UINT uiReadLine = 0; |
| | | |
| | | // lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY); |
| | | // if(lpImg != NULL) |
| | | // { |
| | | // memcpy(lpImg, &Iplimg->imageData[0] , Iplimg->widthStep*Iplimg->height); |
| | | // } |
| | | // |
| | | for(int i = 0; i < Iplimg->height; i++) |
| | | { |
| | | lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY); |
| | | if (lpImg != NULL) { |
| | | memcpy(lpImg, &pIplimg->imageData[0], pIplimg->widthStep * pIplimg->height); |
| | | } |
| | | |
| | | for(int i = 0; i < pIplimg->height; i++) { |
| | | lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY+i); |
| | | if(lpImg == NULL) |
| | | if (lpImg == NULL) { |
| | | break; |
| | | memcpy(lpImg, &Iplimg->imageData[uiReadLine] , Iplimg->widthStep); |
| | | uiReadLine += Iplimg->widthStep; |
| | | } |
| | | |
| | | memcpy(lpImg, &pIplimg->imageData[uiReadLine] , pIplimg->widthStep); |
| | | uiReadLine += pIplimg->widthStep; |
| | | } |
| | | |
| | | if(pGlassData != NULL) |
| | | { |
| | | if(pGlassData != NULL) { |
| | | CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide); |
| | | if(pSideData != NULL) |
| | | { |
| | | if(pSideData != NULL) { |
| | | pSideData->m_nGlassStartLine = 0; |
| | | pSideData->m_nGlassEndLine = Iplimg->height + nStartY; |
| | | pSideData->m_nGlassEndLine = pIplimg->height + nStartY; |
| | | } |
| | | } |
| | | |
| | | nHeight = Iplimg->height; |
| | | cvReleaseImage(&Iplimg); |
| | | nHeight = pIplimg->height; |
| | | cvReleaseImage(&pIplimg); |
| | | } |
| | | else //Full image |
| | | { |
| | | else { |
| | | //Full image |
| | | const double dRatio = 1.0 - static_cast<double>(IMAGE_COMPRESSRATIO / 100.0); |
| | | IplImage *resizeImage = cvCreateImage(cvSize((int)(Iplimg->width/dRatio), (int)(Iplimg->height/dRatio)), IPL_DEPTH_8U, 1); |
| | | IplImage *pResizeImage = cvCreateImage(cvSize((int)(pIplimg->width/dRatio), (int)(pIplimg->height/dRatio)), IPL_DEPTH_8U, 1); |
| | | |
| | | cvResize(Iplimg, resizeImage, CV_INTER_CUBIC); |
| | | |
| | | cvResize(pIplimg, pResizeImage, CV_INTER_CUBIC); |
| | | |
| | | lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY); |
| | | if(lpImg != NULL) |
| | | { |
| | | memcpy(lpImg, &Iplimg->imageData[0] , resizeImage->widthStep*resizeImage->height); |
| | | if(lpImg != NULL) { |
| | | memcpy(lpImg, &pIplimg->imageData[0] , pResizeImage->widthStep * pResizeImage->height); |
| | | } |
| | | |
| | | // UINT uiReadLine = 0; |
| | | // for(int i = 0; i < resizeImage->height; i++) |
| | | // { |
| | | // lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY+i); |
| | | // if(lpImg == NULL) |
| | | // break; |
| | | // memcpy(lpImg, &resizeImage->imageData[uiReadLine] , resizeImage->widthStep); |
| | | // uiReadLine += resizeImage->widthStep; |
| | | // } |
| | | UINT uiReadLine = 0; |
| | | for (int i = 0; i < pResizeImage->height; i++) { |
| | | lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY + i); |
| | | if (lpImg == NULL) { |
| | | break; |
| | | } |
| | | |
| | | if(pGlassData != NULL) |
| | | { |
| | | memcpy(lpImg, &pResizeImage->imageData[uiReadLine], pResizeImage->widthStep); |
| | | uiReadLine += pResizeImage->widthStep; |
| | | } |
| | | |
| | | if(pGlassData != NULL) { |
| | | CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide); |
| | | if(pSideData != NULL) |
| | | { |
| | | if(pSideData != NULL) { |
| | | pSideData->m_nGlassStartLine = 0; |
| | | pSideData->m_nGlassEndLine = Iplimg->height + nStartY; |
| | | pSideData->m_nGlassEndLine = pIplimg->height + nStartY; |
| | | } |
| | | } |
| | | |
| | | nHeight = resizeImage->height; |
| | | cvReleaseImage(&Iplimg); |
| | | cvReleaseImage(&resizeImage); |
| | | nHeight = pResizeImage->height; |
| | | cvReleaseImage(&pIplimg); |
| | | cvReleaseImage(&pResizeImage); |
| | | } |
| | | strPath.Append(_T("_OK")); |
| | | } |
| | | else |
| | | else { |
| | | strPath.Append(_T("_NG")); |
| | | } |
| | | #else |
| | | // 加载灰度图像 |
| | | std::string strFilename = CT2A(strPath); |
| | | cv::Mat img = cv::imread(strFilename, cv::IMREAD_GRAYSCALE); |
| | | |
| | | if (!img.empty()) { |
| | | g_pLog->DisplayMessage(_T("Image Size: %d x %d"), img.cols, img.rows); |
| | | |
| | | if (bIsDebugImg) { |
| | | for (int i = 0; i < img.rows; ++i) { |
| | | LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY + i); |
| | | if (lpImg == NULL) { |
| | | break; |
| | | } |
| | | |
| | | memcpy(lpImg, img.ptr(i), img.cols); |
| | | } |
| | | |
| | | if (pGlassData != NULL) { |
| | | CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide); |
| | | if (pSideData != NULL) { |
| | | pSideData->m_nGlassStartLine = 0; |
| | | pSideData->m_nGlassEndLine = img.rows + nStartY; |
| | | } |
| | | } |
| | | |
| | | nHeight = img.rows; |
| | | } |
| | | else { |
| | | // 按比例缩放 |
| | | const double dRatio = 1.0 - static_cast<double>(IMAGE_COMPRESSRATIO) / 100.0; |
| | | int newWidth = static_cast<int>(img.cols / dRatio); |
| | | int newHeight = static_cast<int>(img.rows / dRatio); |
| | | |
| | | cv::Size newSize(newWidth, newHeight); |
| | | cv::Mat resized; |
| | | cv::resize(img, resized, newSize, 0, 0, cv::INTER_CUBIC); |
| | | |
| | | LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY); |
| | | if (lpImg != NULL) { |
| | | memcpy(lpImg, resized.data, resized.cols * resized.rows); |
| | | } |
| | | |
| | | if (pGlassData != NULL) { |
| | | CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide); |
| | | if (pSideData != NULL) { |
| | | pSideData->m_nGlassStartLine = 0; |
| | | pSideData->m_nGlassEndLine = img.rows + nStartY; |
| | | } |
| | | } |
| | | |
| | | nHeight = resized.rows; |
| | | } |
| | | |
| | | strPath.Append(_T("_OK")); |
| | | } |
| | | else { |
| | | strPath.Append(_T("_NG")); |
| | | } |
| | | #endif // 0 |
| | | |
| | | g_pLog->DisplayMessage(_T("%s"),(TCHAR*)(LPCTSTR)strPath); |
| | | |
| | | SetSlashText(GLOBAL_DEFINE::emText, strPath, RGB(0,0,0)); |
| | | |
| | | return nHeight; |
| | |
| | | BOOL IsScanNow(); |
| | | int LoadFullImage(CString strPath,int iSide,int nStartY = 0, CGlass_Data *pGlassData = NULL, const bool bIsDebugImg = true); |
| | | BOOL SaveFullImage(CString strPath,int iSide,CPoint ptStart,int nLineCnt); |
| | | BOOL SaveFullImage2(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality = 50); |
| | | BOOL SaveFullImageModern(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality = 50); |
| | | LPBYTE GetGrabBuffer(DimensionDir eDir,int iVline); |
| | | BOOL SetLiveGrabCommand(DimensionDir eDir,BOOL bStop); |
| | | void WriteLogManager(TCHAR *str); |