CreateSmallPhoto.cpp 14 KB

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