CreateSmallPhoto.cpp 14 KB

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