CreateSmallPhoto.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #include "stdafx.h"
  2. #include "CreateSmallPhoto.h"
  3. #include <process.h>
  4. #include <afxdb.h>
  5. #include "./helper/ffsco.h"
  6. #include "BranchInfo.h"
  7. #include <comutil.h>
  8. #pragma comment(lib, "comsuppw.lib")
  9. #include "IMGCommon.h"
  10. using namespace helper_coffs;
  11. unsigned __stdcall WorkThread(LPVOID lpParam);
  12. CreateSmallPhoto::CreateSmallPhoto()
  13. {
  14. m_hExitEvent = NULL;
  15. m_hThread = NULL;
  16. lCreating = 0;
  17. }
  18. CreateSmallPhoto::~CreateSmallPhoto()
  19. {
  20. TerminateThread();
  21. }
  22. //-----------------------------------------------------------------------
  23. /*
  24. // 函数:GetOrderNumFromDB
  25. // 描述:从数据库里获取订单号
  26. // 参数:
  27. CStringArray& orderArr 返回未取件的订单号
  28. CString* pField 条件限制的字段
  29. // 返回:
  30. */
  31. //-----------------------------------------------------------------------
  32. void CreateSmallPhoto::GetOrdersFromDB(OUT CStringArray& orderArr, IN CString* pField)
  33. {
  34. CDatabase *pdb = NULL;
  35. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  36. if ( pdb = NULL )
  37. return;
  38. CRecordset myset(pdb);
  39. CString sql = "select id from dindan";
  40. if(pField && (*pField) != _T(""))
  41. sql += " where " + (*pField);
  42. myset.Open(CRecordset::forwardOnly, sql);
  43. while (!myset.IsEOF())
  44. {
  45. CString strOrder = _T("");
  46. myset.GetFieldValue("id", strOrder);
  47. if(strOrder != _T(""))
  48. orderArr.Add(strOrder);
  49. myset.MoveNext();
  50. }
  51. myset.Close();
  52. }
  53. /************************************************************************/
  54. /*
  55. 函数: CreatePhoto
  56. 描述: 生成小图
  57. 参数:
  58. IN LPCTSTR lpDir, 目录
  59. IN BOOL bMPhoto 是否生成m图
  60. 返回:
  61. 示例:
  62. 注意:
  63. */
  64. /************************************************************************/
  65. BOOL CreateSmallPhoto::CreatePhoto(IN CString& strDir, IN BOOL bMPhoto)
  66. {
  67. //先删除ok文件
  68. DelOKFiles(&strDir);
  69. // 如果不是客户原片或修好的片目录,设置bMPhoto参数为0,不生成M缩略图;
  70. if (strDir.Find("客户原片(管理软件)$") != -1 || strDir.Find("修好的片(管理软件)$") != -1)
  71. {
  72. }
  73. else
  74. bMPhoto = 0;
  75. ffsco o;
  76. o.dirs(1);
  77. o.find(LPCSTR(strDir), LPCSTR("*.*"));
  78. ffsco::typeT coo;
  79. ffsco::typeT::iterator it;
  80. coo = o.co_file();
  81. int oldsize = coo.size();
  82. int nFileCount = 0;
  83. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  84. for (it = coo.begin(); coo.end() != it; it++)
  85. {
  86. try
  87. {
  88. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  89. break;
  90. // 遍历到的jpg文件如果是缩略图则不处理,继续遍历;
  91. path = (*it).c_str();
  92. temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  93. if (temp.GetAt(0) == 's')continue;
  94. if (temp.GetAt(0) == 'm')continue;
  95. spath = path.Left(path.ReverseFind('\\') + 1);
  96. mpath = spath;
  97. spath += "s";
  98. mpath += "m";
  99. spath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  100. mpath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  101. Image* pImg = NULL;
  102. LoadImageFromBuf(&pImg, path);
  103. if (pImg == NULL)
  104. continue;
  105. #if 0
  106. if (pImg->GetWidth() == 0)
  107. {
  108. // 由于没有判断spath是否是jpg或jpeg的相片文件,导致这里泄漏;
  109. if (pImg)
  110. delete pImg;
  111. pImg = NULL;
  112. continue;
  113. }
  114. CRect rc(0, 0, 100, 90);
  115. RectFitDes(pImg->GetWidth(), pImg->GetHeight(), rc);
  116. Image *simg = pImg->GetThumbnailImage(rc.Width(), rc.Height(), NULL, NULL);
  117. if (simg == NULL)
  118. {
  119. if (pImg)
  120. delete pImg;
  121. pImg = NULL;
  122. return FALSE;
  123. }
  124. Image* simg2 = NULL;
  125. if (bMPhoto)
  126. {
  127. CRect rc2(0, 0, 2000, 2000);
  128. RectFitDes(pImg->GetWidth(), pImg->GetHeight(), rc2);
  129. simg2 = pImg->GetThumbnailImage(rc2.Width(), rc2.Height(), NULL, NULL);
  130. if (simg2 == NULL)
  131. {
  132. if (pImg)
  133. delete pImg;
  134. pImg = NULL;
  135. return FALSE;
  136. }
  137. }
  138. //生成s图
  139. Graphics graphic(simg);//防止GetThumbnailImage影响质量
  140. graphic.Clear(Color(255, 255, 255, 255));
  141. graphic.DrawImage(pImg, 0, 0, simg->GetWidth(), simg->GetHeight());
  142. if (bMPhoto)
  143. {
  144. //生成m图
  145. Graphics graphic2(simg2);//防止GetThumbnailImage影响质量
  146. graphic2.Clear(Color(255, 255, 255, 255));
  147. graphic2.DrawImage(pImg, 0, 0, simg2->GetWidth(), simg2->GetHeight());
  148. }
  149. ///////////////////////////////
  150. UINT totalBufferSize;
  151. UINT numProperties;
  152. pImg->GetPropertySize(&totalBufferSize, &numProperties);
  153. // 分配将接收到的属性项的缓冲区。
  154. PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
  155. // 填补缓冲区。
  156. pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
  157. // 打印每个属性项的标识数据成员。
  158. for (UINT j = 0; j < numProperties; ++j)
  159. {
  160. if (PropertyTagOrientation == pAllItems[j].id)
  161. {
  162. short* ptrLong = (short*)(pAllItems[j].value);
  163. int ret = (int)*ptrLong;
  164. if (ret == 8)
  165. {
  166. if (simg2)simg2->RotateFlip(Rotate270FlipNone);
  167. simg->RotateFlip(Rotate270FlipNone);
  168. }
  169. else if (ret == 6)
  170. {
  171. if (simg2)simg2->RotateFlip(Rotate90FlipNone);
  172. simg->RotateFlip(Rotate90FlipNone);
  173. }
  174. break;
  175. }
  176. }
  177. free(pAllItems);
  178. ///////////////////////////////
  179. ::SaveImageToFile(simg, spath); nFileCount++;
  180. delete simg;
  181. delete pImg;
  182. if (bMPhoto)
  183. {
  184. ::SaveImageToFile2(simg2, mpath, 100); nFileCount++;
  185. delete simg2;
  186. }
  187. #else
  188. IMGCommon::ImgThumbnail(pImg, SET_PIX(100,100), spath, 100);
  189. if (bMPhoto)
  190. IMGCommon::ImgThumbnail(pImg, SET_PIX(2000,2000), mpath, 100);
  191. #endif
  192. }
  193. catch (...)
  194. {
  195. WriteTextLog("出错在函数CreatePhoto中");
  196. }
  197. }
  198. CreateOKFiles(strDir);
  199. return TRUE;
  200. }
  201. /************************************************************************/
  202. /*
  203. 函数:LoadImageFromBuf
  204. 描述:生成图片;
  205. 参数:
  206. Image **img,
  207. BYTE *pData,
  208. DWORD leng
  209. 返回:
  210. 示例:
  211. 注意:
  212. */
  213. /************************************************************************/
  214. void CreateSmallPhoto::LoadImageFromBuf(Image **img, BYTE *pData, DWORD leng)
  215. {
  216. try
  217. {
  218. if (pData == NULL)return;
  219. if (*img)delete *img;
  220. *img = NULL;
  221. // Allocate global memory on which to create stream
  222. HGLOBAL m_hMem = GlobalAlloc(GMEM_MOVEABLE, leng);
  223. if (m_hMem == NULL)return;
  224. BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
  225. memcpy(pmem, pData, leng);
  226. IStream* pstm;
  227. CreateStreamOnHGlobal(m_hMem, TRUE, &pstm);
  228. // load from stream
  229. *img = Gdiplus::Image::FromStream(pstm);
  230. // free/release stuff
  231. GlobalUnlock(m_hMem);
  232. pstm->Release();
  233. }
  234. catch (...)
  235. {
  236. }
  237. }
  238. /************************************************************************/
  239. /*
  240. 函数:LoadImageFromBuf
  241. 描述:生成图片;
  242. 参数:
  243. Image **img,
  244. CString path
  245. 返回:
  246. 示例:
  247. 注意:
  248. */
  249. /************************************************************************/
  250. void CreateSmallPhoto::LoadImageFromBuf(Image **img, CString path)
  251. {
  252. if ( !PathFileExists(path) )
  253. return;
  254. if ( *img )
  255. delete *img;
  256. *img = NULL;
  257. #ifdef UNICODE
  258. *img = Image::FromFile(path);
  259. #else
  260. BSTR strtmp = _bstr_t(path);
  261. // 设置true,使用内嵌的色彩管理;
  262. *img = Image::FromFile(strtmp, true);
  263. SysFreeString(strtmp);
  264. #endif
  265. }
  266. /************************************************************************/
  267. /*
  268. 函数:IsCreateMImage
  269. 描述:选片时是否使用m缩略图来代替原片放大使用;
  270. 参数:
  271. Image **img,
  272. CString path
  273. 返回: TRUE,FALSE
  274. */
  275. /************************************************************************/
  276. BOOL CreateSmallPhoto::IsCreateMImage()
  277. {
  278. BOOL bRet = FALSE;
  279. if (g_bSteal == 0)
  280. {
  281. CDatabase *pdb = NULL;
  282. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  283. if ( pdb = NULL )
  284. return FALSE;
  285. CRecordset myset(pdb);
  286. CString sql = "select * from version";
  287. myset.Open(CRecordset::forwardOnly, sql);
  288. myset.GetFieldValue("setcheck15", sql); // 选片时是否使用m缩略图来代替原片放大使用;
  289. myset.Close();
  290. bRet = atoi(sql);
  291. }
  292. return bRet;
  293. }
  294. /************************************************************************/
  295. /*
  296. 函数:Work
  297. 描述:
  298. 参数:
  299. 返回:
  300. */
  301. /************************************************************************/
  302. void CreateSmallPhoto::Work()
  303. {
  304. // 选片时是否使用m缩略图来代替原片放大使用;
  305. BOOL bMphoto = IsCreateMImage();
  306. //读取所有未取件的订单号
  307. CStringArray orderArr;
  308. CString strField = _T("status3='未取'");
  309. GetOrdersFromDB(orderArr, &strField);
  310. if(orderArr.GetSize() == 0)
  311. {
  312. WriteTextLog("没有获取到订单号");
  313. return;
  314. }
  315. //组合目录
  316. CString initialPhotoPath = _T("");
  317. CString initialModifyPath = _T("");
  318. CString designPhotoPath = _T("");
  319. CString excellentModifyPath = _T("");
  320. char server[50] = {0};
  321. DWORD leng = 50;
  322. ::GetComputerName(server, &leng);
  323. initialPhotoPath = initialModifyPath = designPhotoPath = excellentModifyPath = server;
  324. initialPhotoPath = "\\\\" + initialPhotoPath + "\\客户原片(管理软件)$";
  325. initialModifyPath = "\\\\" + initialModifyPath + "\\修好的片(管理软件)$";
  326. designPhotoPath = "\\\\" + designPhotoPath + "\\设计好的片(管理软件)$";
  327. excellentModifyPath = "\\\\" + excellentModifyPath + "\\精修好的片(管理软件)$";
  328. GetPathFromNetShareName("客户原片(管理软件)$", initialPhotoPath);
  329. GetPathFromNetShareName("修好的片(管理软件)$", initialModifyPath);
  330. GetPathFromNetShareName("设计好的片(管理软件)$", designPhotoPath);
  331. GetPathFromNetShareName("精修好的片(管理软件)$", excellentModifyPath);
  332. initialPhotoPath.MakeLower();
  333. initialModifyPath.MakeLower();
  334. designPhotoPath.MakeLower();
  335. excellentModifyPath.MakeLower();
  336. //
  337. CStringArray pathArr;
  338. int i = 0;
  339. try
  340. {
  341. //筛选需要生成小图的目录
  342. if(CBranchInfo::GetInstance()->m_TblNetShareInfo.size() != 0)
  343. {
  344. vector<TblNetShareInfo*>::iterator iter = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
  345. for(; iter != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
  346. {
  347. TblNetShareInfo* p = (*iter);
  348. if(p != NULL)
  349. {
  350. CString strDir = p->szShareDirectory;
  351. if(strDir.Find(_T("$\\")) != -1)
  352. strDir = strDir.TrimRight(_T("\\"));
  353. ScreeningDir(&strDir, &orderArr, pathArr);
  354. }
  355. ++iter;
  356. }
  357. CString patharray[2] = { designPhotoPath, excellentModifyPath };
  358. for (i = 0; i < 2; i++)
  359. ScreeningDir(&patharray[i], &orderArr, pathArr);
  360. }
  361. //生成小图
  362. for(int i=0; i<pathArr.GetSize(); i++)
  363. {
  364. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  365. break;
  366. CreatePhoto(pathArr.ElementAt(i), bMphoto);
  367. }
  368. }
  369. catch (...)
  370. {
  371. WriteTextLog("出错在函数CreateSmallPhoto::GenThumb中");
  372. }
  373. InterlockedExchange(&lCreating, FALSE);
  374. _endthreadex(0);
  375. TerminateThread();
  376. return;
  377. }
  378. /************************************************************************/
  379. /*
  380. 函数:ScreeningDir
  381. 描述: 筛选目录
  382. 参数:
  383. IN const CString* pDir, 需要检测的根目录
  384. IN CStringArray* pOrderArr, 订单号集
  385. OUT CStringArray& dirArr 返回需要生成小图的目录
  386. 返回: TRUE,FALSE
  387. */
  388. /************************************************************************/
  389. BOOL CreateSmallPhoto::ScreeningDir(IN const CString* pDir, IN CStringArray* pOrderArr, OUT CStringArray& dirArr)
  390. {
  391. if(pDir == NULL || pOrderArr == NULL)
  392. return FALSE;
  393. ffsco o;
  394. o.dirs(0);
  395. o.find(LPCSTR((*pDir)), LPCSTR("*.*"));
  396. ffsco::typeT coo;
  397. ffsco::typeT::iterator it;
  398. coo = o.co_dir();
  399. int oldsize = 0;
  400. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  401. CString strOldPath = _T("");
  402. for (it = coo.begin(); coo.end() != it; it++)
  403. {
  404. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  405. break;
  406. path = it->c_str();
  407. path.MakeLower();
  408. if(path.CompareNoCase(strOldPath) == 0)
  409. continue;
  410. if(!PathFileExists(path))
  411. continue;
  412. if(path.GetAt(path.GetLength() - 1) != '\\')
  413. path += _T("\\");
  414. BOOL bNeed = FALSE;
  415. //先检测目录是否是要生成小图的目录
  416. for(int i=0; i<pOrderArr->GetSize(); i++)
  417. {
  418. CString strOrderNum = _T("\\") + pOrderArr->ElementAt(i) + _T("\\");
  419. if(path.Find(strOrderNum) != -1)
  420. {
  421. bNeed = TRUE;
  422. break;
  423. }
  424. }
  425. if(!bNeed)
  426. continue;
  427. //删除要生成小图的目录里的ok文件
  428. // DelOKFiles(&path);
  429. dirArr.Add(it->c_str());
  430. strOldPath = it->c_str();
  431. }
  432. return TRUE;
  433. }
  434. /************************************************************************/
  435. /*
  436. 函数:DelOKFiles
  437. 描述: 删除ok文件
  438. 参数:
  439. IN CString* pPath
  440. 返回: TRUE,FALSE
  441. */
  442. /************************************************************************/
  443. BOOL CreateSmallPhoto::DelOKFiles(IN CString* pPath)
  444. {
  445. if(pPath == NULL || (*pPath) == _T(""))
  446. return FALSE;
  447. ffsco o;
  448. o.dirs(1);
  449. o.find(LPCSTR((*pPath)), LPCSTR("*.*"));
  450. ffsco::typeT coo;
  451. ffsco::typeT::iterator it;
  452. coo = o.co_dir();
  453. int oldsize = coo.size();
  454. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  455. CString strOldPath = _T("");
  456. for (it = coo.begin(); coo.end() != it; it++)
  457. {
  458. CString str = it->c_str();
  459. if(str.GetAt(str.GetLength() - 1) != '\\')
  460. str += _T("\\");
  461. CString strTmp1 = str;
  462. CString strTmp2 = str;
  463. strTmp1 += _T("ok");
  464. strTmp2 += _T("ok.dat");
  465. if(PathFileExists(strTmp1))
  466. DeleteFile(strTmp1); // 删除ok文件;
  467. if(PathFileExists(strTmp2))
  468. DeleteFile(strTmp2); // 删除ok文件;
  469. }
  470. return TRUE;
  471. }
  472. unsigned __stdcall WorkThread(LPVOID lpParam)
  473. {
  474. CreateSmallPhoto* p = (CreateSmallPhoto*)lpParam;
  475. if(p != NULL)
  476. p->Work();
  477. return 0;
  478. }
  479. void CreateSmallPhoto::StartThread()
  480. {
  481. TerminateThread();
  482. lCreating = 1;
  483. m_hExitEvent = CreateEvent(0, TRUE, FALSE, NULL);
  484. unsigned int lThreadID = 0;
  485. m_hThread = (HANDLE)_beginthreadex(NULL, 0, WorkThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &lThreadID);
  486. }
  487. void CreateSmallPhoto::TerminateThread()
  488. {
  489. if(m_hExitEvent)
  490. SetEvent(m_hExitEvent);
  491. if(m_hThread)
  492. {
  493. ::WaitForSingleObject(m_hThread, INFINITE);
  494. CloseHandle(m_hThread);
  495. m_hThread = NULL;
  496. }
  497. if(m_hExitEvent)
  498. {
  499. CloseHandle(m_hExitEvent);
  500. m_hExitEvent = NULL;
  501. }
  502. }
  503. /************************************************************************/
  504. /*
  505. 函数:CreateOKFiles
  506. 描述: 生成ok文件
  507. 参数:
  508. IN CString& strDir, 要生成ok文件的目录
  509. IN int nOldSize 文件数量
  510. 返回: TRUE,FALSE
  511. */
  512. /************************************************************************/
  513. void CreateSmallPhoto::CreateOKFiles(IN CString& strDir, IN int nOldSize)
  514. {
  515. if(strDir == _T(""))
  516. return;
  517. try
  518. {
  519. CString strPath = _T("");
  520. CString strTmpDir = strDir;
  521. if(strDir.GetAt(strDir.GetLength() - 1) != '\\')
  522. strDir += _T("\\");
  523. ffsco o;
  524. o.dirs(1);
  525. o.find(LPCSTR(strDir), LPCSTR("*.*"));
  526. ffsco::typeT cooDir;
  527. ffsco::typeT::iterator it;
  528. cooDir = o.co_dir();
  529. for(it=cooDir.begin(); it!=cooDir.end(); it++)
  530. {
  531. strPath = it->c_str();
  532. if(strPath.GetAt(strPath.GetLength() - 1) != '\\')
  533. strPath += _T("\\");
  534. strPath += _T("ok");
  535. CFile fp;
  536. fp.Open(strPath, CFile::modeCreate | CFile::modeWrite);
  537. fp.Close();
  538. }
  539. }
  540. catch (...)
  541. {
  542. WriteTextLog("出错在函数CreateOKFiles中");
  543. }
  544. }