#include "stdafx.h" #include "CreateSmallPhoto.h" #include #include #include "./helper/ffsco.h" #include "BranchInfo.h" using namespace helper_coffs; unsigned __stdcall WorkThread(LPVOID lpParam); CreateSmallPhoto::CreateSmallPhoto() { m_hExitEvent = NULL; m_hThread = NULL; lCreating = 0; } CreateSmallPhoto::~CreateSmallPhoto() { TerminateThread(); } //----------------------------------------------------------------------- /* // 函数:GetOrderNumFromDB // 描述:从数据库里获取订单号 // 参数: CStringArray& orderArr 返回未取件的订单号 CString* pField 条件限制的字段 // 返回: */ //----------------------------------------------------------------------- void CreateSmallPhoto::GetOrdersFromDB(OUT CStringArray& orderArr, IN CString* pField) { CDatabase *pdb = NULL; ODBCConnGuard ConnGuard(pdb, -1, 3000); if ( pdb = NULL ) return; CRecordset myset(pdb); CString sql = "select id from dindan"; if(pField && (*pField) != _T("")) sql += " where " + (*pField); myset.Open(CRecordset::forwardOnly, sql); while (!myset.IsEOF()) { CString strOrder = _T(""); myset.GetFieldValue("id", strOrder); if(strOrder != _T("")) orderArr.Add(strOrder); myset.MoveNext(); } myset.Close(); } /************************************************************************/ /* 函数: CreatePhoto 描述: 生成小图 参数: IN LPCTSTR lpDir, 目录 IN BOOL bMPhoto 是否生成m图 返回: 示例: 注意: */ /************************************************************************/ BOOL CreateSmallPhoto::CreatePhoto(IN CString& strDir, IN BOOL bMPhoto) { //先删除ok文件 DelOKFiles(&strDir); // 如果不是客户原片或修好的片目录,设置bMPhoto参数为0,不生成M缩略图; if (strDir.Find("客户原片(管理软件)$") != -1 || strDir.Find("修好的片(管理软件)$") != -1) { } else bMPhoto = 0; ffsco o; o.dirs(1); o.find(LPCSTR(strDir), LPCSTR("*.*")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_file(); int oldsize = coo.size(); int nFileCount = 0; CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T("")); for (it = coo.begin(); coo.end() != it; it++) { try { if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0) break; // 遍历到的jpg文件如果是缩略图则不处理,继续遍历; path = (*it).c_str(); temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1); if (temp.GetAt(0) == 's')continue; if (temp.GetAt(0) == 'm')continue; spath = path.Left(path.ReverseFind('\\') + 1); mpath = spath; spath += "s"; mpath += "m"; spath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1); mpath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1); Image* pImg = NULL; LoadImageFromBuf(&pImg, path); if (pImg == NULL) continue; if (pImg->GetWidth() == 0) { // 由于没有判断spath是否是jpg或jpeg的相片文件,导致这里泄漏; if (pImg) delete pImg; pImg = NULL; continue; } CRect rc(0, 0, 100, 90); RectFitDes(pImg->GetWidth(), pImg->GetHeight(), rc); Image *simg = pImg->GetThumbnailImage(rc.Width(), rc.Height(), NULL, NULL); if (simg == NULL) { if (pImg) delete pImg; pImg = NULL; return FALSE; } Image* simg2 = NULL; if (bMPhoto) { CRect rc2(0, 0, 2000, 2000); RectFitDes(pImg->GetWidth(), pImg->GetHeight(), rc2); simg2 = pImg->GetThumbnailImage(rc2.Width(), rc2.Height(), NULL, NULL); if (simg2 == NULL) { if (pImg) delete pImg; pImg = NULL; return FALSE; } } //生成s图 Graphics graphic(simg);//防止GetThumbnailImage影响质量 graphic.Clear(Color(255, 255, 255, 255)); graphic.DrawImage(pImg, 0, 0, simg->GetWidth(), simg->GetHeight()); if (bMPhoto) { //生成m图 Graphics graphic2(simg2);//防止GetThumbnailImage影响质量 graphic2.Clear(Color(255, 255, 255, 255)); graphic2.DrawImage(pImg, 0, 0, simg2->GetWidth(), simg2->GetHeight()); } /////////////////////////////// UINT totalBufferSize; UINT numProperties; pImg->GetPropertySize(&totalBufferSize, &numProperties); // 分配将接收到的属性项的缓冲区。 PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize); // 填补缓冲区。 pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems); // 打印每个属性项的标识数据成员。 for (UINT j = 0; j < numProperties; ++j) { if (PropertyTagOrientation == pAllItems[j].id) { short* ptrLong = (short*)(pAllItems[j].value); int ret = (int)*ptrLong; if (ret == 8) { if (simg2)simg2->RotateFlip(Rotate270FlipNone); simg->RotateFlip(Rotate270FlipNone); } else if (ret == 6) { if (simg2)simg2->RotateFlip(Rotate90FlipNone); simg->RotateFlip(Rotate90FlipNone); } break; } } free(pAllItems); /////////////////////////////// ::SaveImageToFile(simg, spath); nFileCount++; delete simg; delete pImg; if (bMPhoto) { ::SaveImageToFile2(simg2, mpath, 100); nFileCount++; delete simg2; } } catch (...) { WriteTextLog("出错在函数CreatePhoto中"); } } CreateOKFiles(strDir); return TRUE; } /************************************************************************/ /* 函数:LoadImageFromBuf 描述:生成图片; 参数: Image **img, BYTE *pData, DWORD leng 返回: 示例: 注意: */ /************************************************************************/ void CreateSmallPhoto::LoadImageFromBuf(Image **img, BYTE *pData, DWORD leng) { try { if (pData == NULL)return; if (*img)delete *img; *img = NULL; // Allocate global memory on which to create stream HGLOBAL m_hMem = GlobalAlloc(GMEM_MOVEABLE, leng); if (m_hMem == NULL)return; BYTE* pmem = (BYTE*)GlobalLock(m_hMem); memcpy(pmem, pData, leng); IStream* pstm; CreateStreamOnHGlobal(m_hMem, TRUE, &pstm); // load from stream *img = Gdiplus::Image::FromStream(pstm); // free/release stuff GlobalUnlock(m_hMem); pstm->Release(); } catch (...) { } } /************************************************************************/ /* 函数:LoadImageFromBuf 描述:生成图片; 参数: Image **img, CString path 返回: 示例: 注意: */ /************************************************************************/ void CreateSmallPhoto::LoadImageFromBuf(Image **img, CString path) { try { CFile fp; if (fp.Open(path, CFile::modeRead)) { DWORD leng = fp.GetLength(); BYTE *pData = new BYTE[leng]; fp.Read(pData, leng); fp.Close(); LoadImageFromBuf(img, pData, leng); delete[]pData; } } catch (...) { } } /************************************************************************/ /* 函数:IsCreateMImage 描述:选片时是否使用m缩略图来代替原片放大使用; 参数: Image **img, CString path 返回: TRUE,FALSE */ /************************************************************************/ BOOL CreateSmallPhoto::IsCreateMImage() { BOOL bRet = FALSE; if (g_bSteal == 0) { CDatabase *pdb = NULL; ODBCConnGuard ConnGuard(pdb, -1, 3000); if ( pdb = NULL ) return FALSE; CRecordset myset(pdb); CString sql = "select * from version"; myset.Open(CRecordset::forwardOnly, sql); myset.GetFieldValue("setcheck15", sql); // 选片时是否使用m缩略图来代替原片放大使用; myset.Close(); bRet = atoi(sql); } return bRet; } /************************************************************************/ /* 函数:Work 描述: 参数: 返回: */ /************************************************************************/ void CreateSmallPhoto::Work() { // 选片时是否使用m缩略图来代替原片放大使用; BOOL bMphoto = IsCreateMImage(); //读取所有未取件的订单号 CStringArray orderArr; CString strField = _T("status3='未取'"); GetOrdersFromDB(orderArr, &strField); if(orderArr.GetSize() == 0) { WriteTextLog("没有获取到订单号"); return; } //组合目录 CString initialPhotoPath = _T(""); CString initialModifyPath = _T(""); CString designPhotoPath = _T(""); CString excellentModifyPath = _T(""); char server[50] = {0}; DWORD leng = 50; ::GetComputerName(server, &leng); initialPhotoPath = initialModifyPath = designPhotoPath = excellentModifyPath = server; initialPhotoPath = "\\\\" + initialPhotoPath + "\\客户原片(管理软件)$"; initialModifyPath = "\\\\" + initialModifyPath + "\\修好的片(管理软件)$"; designPhotoPath = "\\\\" + designPhotoPath + "\\设计好的片(管理软件)$"; excellentModifyPath = "\\\\" + excellentModifyPath + "\\精修好的片(管理软件)$"; GetPathFromNetShareName("客户原片(管理软件)$", initialPhotoPath); GetPathFromNetShareName("修好的片(管理软件)$", initialModifyPath); GetPathFromNetShareName("设计好的片(管理软件)$", designPhotoPath); GetPathFromNetShareName("精修好的片(管理软件)$", excellentModifyPath); initialPhotoPath.MakeLower(); initialModifyPath.MakeLower(); designPhotoPath.MakeLower(); excellentModifyPath.MakeLower(); // CStringArray pathArr; int i = 0; try { //筛选需要生成小图的目录 if(CBranchInfo::GetInstance()->m_TblNetShareInfo.size() != 0) { vector::iterator iter = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin(); for(; iter != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();) { TblNetShareInfo* p = (*iter); if(p != NULL) { CString strDir = p->szShareDirectory; if(strDir.Find(_T("$\\")) != -1) strDir = strDir.TrimRight(_T("\\")); ScreeningDir(&strDir, &orderArr, pathArr); } ++iter; } CString patharray[2] = { designPhotoPath, excellentModifyPath }; for (i = 0; i < 2; i++) ScreeningDir(&patharray[i], &orderArr, pathArr); } //生成小图 for(int i=0; ic_str(); path.MakeLower(); if(path.CompareNoCase(strOldPath) == 0) continue; if(!PathFileExists(path)) continue; if(path.GetAt(path.GetLength() - 1) != '\\') path += _T("\\"); BOOL bNeed = FALSE; //先检测目录是否是要生成小图的目录 for(int i=0; iGetSize(); i++) { CString strOrderNum = _T("\\") + pOrderArr->ElementAt(i) + _T("\\"); if(path.Find(strOrderNum) != -1) { bNeed = TRUE; break; } } if(!bNeed) continue; //删除要生成小图的目录里的ok文件 // DelOKFiles(&path); dirArr.Add(it->c_str()); strOldPath = it->c_str(); } return TRUE; } /************************************************************************/ /* 函数:DelOKFiles 描述: 删除ok文件 参数: IN CString* pPath 返回: TRUE,FALSE */ /************************************************************************/ BOOL CreateSmallPhoto::DelOKFiles(IN CString* pPath) { if(pPath == NULL || (*pPath) == _T("")) return FALSE; ffsco o; o.dirs(1); o.find(LPCSTR((*pPath)), LPCSTR("*.*")); ffsco::typeT coo; ffsco::typeT::iterator it; coo = o.co_dir(); int oldsize = coo.size(); CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T("")); CString strOldPath = _T(""); for (it = coo.begin(); coo.end() != it; it++) { CString str = it->c_str(); if(str.GetAt(str.GetLength() - 1) != '\\') str += _T("\\"); CString strTmp1 = str; CString strTmp2 = str; strTmp1 += _T("ok"); strTmp2 += _T("ok.dat"); if(PathFileExists(strTmp1)) DeleteFile(strTmp1); // 删除ok文件; if(PathFileExists(strTmp2)) DeleteFile(strTmp2); // 删除ok文件; } return TRUE; } unsigned __stdcall WorkThread(LPVOID lpParam) { CreateSmallPhoto* p = (CreateSmallPhoto*)lpParam; if(p != NULL) p->Work(); return 0; } void CreateSmallPhoto::StartThread() { TerminateThread(); lCreating = 1; m_hExitEvent = CreateEvent(0, TRUE, FALSE, NULL); unsigned int lThreadID = 0; m_hThread = (HANDLE)_beginthreadex(NULL, 0, WorkThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &lThreadID); } void CreateSmallPhoto::TerminateThread() { if(m_hExitEvent) SetEvent(m_hExitEvent); if(m_hThread) { ::WaitForSingleObject(m_hThread, INFINITE); CloseHandle(m_hThread); m_hThread = NULL; } if(m_hExitEvent) { CloseHandle(m_hExitEvent); m_hExitEvent = NULL; } } /************************************************************************/ /* 函数:CreateOKFiles 描述: 生成ok文件 参数: IN CString& strDir, 要生成ok文件的目录 IN int nOldSize 文件数量 返回: TRUE,FALSE */ /************************************************************************/ void CreateSmallPhoto::CreateOKFiles(IN CString& strDir, IN int nOldSize) { if(strDir == _T("")) return; try { CString strPath = _T(""); CString strTmpDir = strDir; if(strDir.GetAt(strDir.GetLength() - 1) != '\\') strDir += _T("\\"); ffsco o; o.dirs(1); o.find(LPCSTR(strDir), LPCSTR("*.*")); ffsco::typeT cooDir; ffsco::typeT::iterator it; cooDir = o.co_dir(); for(it=cooDir.begin(); it!=cooDir.end(); it++) { strPath = it->c_str(); if(strPath.GetAt(strPath.GetLength() - 1) != '\\') strPath += _T("\\"); strPath += _T("ok"); CFile fp; fp.Open(strPath, CFile::modeCreate | CFile::modeWrite); fp.Close(); } } catch (...) { WriteTextLog("出错在函数CreateOKFiles中"); } }