123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- #include "stdafx.h"
- #include "CreateSmallPhoto.h"
- #include <process.h>
- #include <afxdb.h>
- #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<TblNetShareInfo*>::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; i<pathArr.GetSize(); i++)
- {
- if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
- break;
- CreatePhoto(pathArr.ElementAt(i), bMphoto);
- }
- }
- catch (...)
- {
- WriteTextLog("出错在函数CreateSmallPhoto::GenThumb中");
- }
- InterlockedExchange(&lCreating, FALSE);
- _endthreadex(0);
- TerminateThread();
- return;
- }
- /************************************************************************/
- /*
- 函数:ScreeningDir
- 描述: 筛选目录
- 参数:
- IN const CString* pDir, 需要检测的根目录
- IN CStringArray* pOrderArr, 订单号集
- OUT CStringArray& dirArr 返回需要生成小图的目录
- 返回: TRUE,FALSE
- */
- /************************************************************************/
- BOOL CreateSmallPhoto::ScreeningDir(IN const CString* pDir, IN CStringArray* pOrderArr, OUT CStringArray& dirArr)
- {
- if(pDir == NULL || pOrderArr == NULL)
- return FALSE;
- ffsco o;
- o.dirs(0);
- o.find(LPCSTR((*pDir)), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_dir();
- int oldsize = 0;
- CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
- CString strOldPath = _T("");
- for (it = coo.begin(); coo.end() != it; it++)
- {
- if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
- break;
- path = it->c_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; i<pOrderArr->GetSize(); 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中");
- }
- }
|