imstorage_manager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "stdafx.h"
  2. #include <io.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include "imstorage_manager.h"
  6. //namespace graft_gcv
  7. //{
  8. CRITICAL_SECTION g_cs;
  9. void getFiles( string path, vector<string>& files, time_t clean_time )
  10. {
  11. //文件句柄
  12. long hFile = 0;
  13. //文件信息
  14. struct _finddata_t fileinfo;
  15. string p;
  16. if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
  17. {
  18. do
  19. {
  20. //如果是目录,迭代之
  21. //如果不是,加入列表
  22. if((fileinfo.attrib & _A_SUBDIR))
  23. {
  24. if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
  25. getFiles( p.assign(path).append("\\").append(fileinfo.name), files ,clean_time);
  26. }
  27. else
  28. {
  29. if(clean_time>fileinfo.time_create){
  30. files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
  31. }
  32. }
  33. }while(_findnext(hFile, &fileinfo) == 0);
  34. _findclose(hFile);
  35. }
  36. }
  37. bool g_thread_run=false;
  38. bool g_thread_run_saver=true;
  39. int WINAPI TheadFuncClearn(LPVOID lpParam)
  40. {
  41. ThreadParamClean* threadParam = (ThreadParamClean *) lpParam;
  42. string folder =threadParam->folder;
  43. int store_days = threadParam->store_days;
  44. bool* state = threadParam->state;
  45. int cnt=0;
  46. __time64_t dtime = store_days*86400;
  47. //cout<<"folder: "<<folder<<endl;
  48. time_t scan_time = time(NULL);
  49. while(folder.size()>0)
  50. {
  51. if(!(*state)){break;}
  52. if(time(NULL)-scan_time < 3600000){
  53. Sleep(500);
  54. continue;
  55. }
  56. vector<string>filenames;
  57. time_t clean_time = time(NULL) - dtime;
  58. getFiles(folder,filenames,clean_time);
  59. scan_time = time(NULL);
  60. for(size_t i=0;i<filenames.size();++i){
  61. //delete the file
  62. remove(filenames[i].c_str());
  63. }
  64. }
  65. return 0;
  66. }
  67. int WINAPI TheadFuncSave(LPVOID lpParam)
  68. {
  69. ThreadParamSave* threadParam = (ThreadParamSave *) lpParam;
  70. int nIndex = threadParam->nIndex;
  71. queue<ImgParam>* imgQ =threadParam->imgQ;
  72. while(true)
  73. {
  74. if(!g_thread_run_saver){break;}
  75. if(imgQ->size()==0){
  76. Sleep(50);
  77. continue;
  78. }
  79. try{
  80. EnterCriticalSection(&g_cs);
  81. ImgParam& im_info = imgQ->front();
  82. cv::imwrite(im_info.img_name,im_info.image);
  83. //cout<<im_info.img_name<<"\t"<<imgQ->size()<<endl;
  84. imgQ->pop();
  85. }
  86. catch(...){
  87. //int tes = 0;
  88. }
  89. LeaveCriticalSection(&g_cs);
  90. }
  91. return 0;
  92. }
  93. CImStoreManager::CImStoreManager()
  94. :m_storeDays(7)
  95. ,m_storeDir(""),
  96. m_workHandle(0),
  97. m_workHandleSave(0)
  98. {
  99. InitializeCriticalSection(&g_cs);
  100. ThreadParamSave paramSave;
  101. paramSave.nIndex = 0;
  102. //paramSave.state = &g_thread_run;
  103. paramSave.imgQ = &m_images;
  104. m_workHandleSave = CreateThread(
  105. NULL,
  106. 0,
  107. (LPTHREAD_START_ROUTINE)TheadFuncSave,
  108. (LPVOID)&paramSave,
  109. NULL,
  110. NULL);
  111. Sleep(500);
  112. }
  113. CImStoreManager::~CImStoreManager(){
  114. g_thread_run=false;
  115. g_thread_run_saver=false;
  116. HANDLE handles[2];
  117. handles[0]=m_workHandle;
  118. handles[1]=m_workHandleSave;
  119. WaitForMultipleObjects(2,handles,TRUE,500);
  120. DeleteCriticalSection(&g_cs);
  121. }
  122. void CImStoreManager::restart_start_worker()
  123. {
  124. ThreadParamClean param_clean;
  125. param_clean.folder = m_storeDir;
  126. param_clean.store_days = m_storeDays;
  127. param_clean.state = &g_thread_run;
  128. g_thread_run=false;
  129. if(m_workHandle){
  130. WaitForSingleObject(m_workHandle,INFINITE);
  131. CloseHandle(m_workHandle);
  132. }
  133. g_thread_run=true;
  134. m_workHandle = CreateThread(
  135. NULL,
  136. 0,
  137. (LPTHREAD_START_ROUTINE)TheadFuncClearn,
  138. (LPVOID)&param_clean,
  139. NULL,
  140. NULL);
  141. Sleep(500);
  142. }
  143. int CImStoreManager::setStoreDir(string& folder)
  144. {
  145. int stat = access(folder.c_str(),0);
  146. if (stat==0){
  147. m_storeDir = folder;
  148. g_thread_run=false;
  149. restart_start_worker();
  150. }
  151. else{
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. void CImStoreManager::getStoreDir(string& folder)
  157. {
  158. folder = m_storeDir;
  159. }
  160. void CImStoreManager::setStoreDays(int days)
  161. {
  162. if(days>0){
  163. m_storeDays = days;
  164. g_thread_run=false;
  165. restart_start_worker();
  166. }
  167. }
  168. bool CImStoreManager::is_valid_folder()
  169. {
  170. int stat = access(m_storeDir.c_str(),0);
  171. if(stat==0){return true;}
  172. else{return false;}
  173. }
  174. int CImStoreManager::saveImage(Mat&img,string name_id)
  175. {
  176. if(!is_valid_folder()){return 1;}
  177. if(img.empty()){return 1;}
  178. string tar_file = m_storeDir+"/"+name_id+".jpg";
  179. ImgParam imgp;
  180. imgp.img_name=tar_file;
  181. imgp.image = img.clone();
  182. EnterCriticalSection(&g_cs);
  183. m_images.push(imgp);
  184. //cout<<"=========="<<imgp.img_name<<endl;
  185. LeaveCriticalSection(&g_cs);
  186. return 0;
  187. }
  188. //};