FolderFeeder.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "StdAfx.h"
  2. #include "scan_common.h"
  3. #include "FolderFeeder.h"
  4. #include "basic_struct.h"
  5. #include <string>
  6. using namespace std;
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. static std::string g_img_dir;
  13. extern int g_my_scan_type;
  14. extern int g_my_continue_scan;
  15. CFolderFeeder::CFolderFeeder(void)
  16. {
  17. m_img =NULL;
  18. InitializeCriticalSection(&m_img_lock);
  19. m_nPageNumber = 2;//默认偶数页
  20. }
  21. CFolderFeeder::~CFolderFeeder(void)
  22. {
  23. DeleteCriticalSection(&m_img_lock);
  24. }
  25. BOOL CFolderFeeder::SupportPause( void )
  26. {
  27. return TRUE;
  28. }
  29. ServiceState CFolderFeeder::OnStarting( void )
  30. {
  31. m_next_param.type = identyfi_type_normal;
  32. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  33. m_img = NULL;
  34. m_count =0;
  35. current_file_type =FT_NONE;
  36. if (path_stack.size() > 0)
  37. {
  38. //检查文件夹是否非法
  39. if (CheckFileCountValid())
  40. return stoping;
  41. return running;
  42. }
  43. ServiceState ret = stoping;
  44. if (g_my_scan_type == 0)
  45. {
  46. TCHAR szFolderPath[MAX_PATH] ={0};
  47. BROWSEINFO sInfo;
  48. ::ZeroMemory(&sInfo,sizeof(BROWSEINFO));
  49. sInfo.pidlRoot =0;
  50. sInfo.lpszTitle =_T("请选择一个文件夹");
  51. sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN|BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_EDITBOX;
  52. sInfo.lpfn =NULL;
  53. LPITEMIDLIST lpidlBrowse= ::SHBrowseForFolder(&sInfo);
  54. if(lpidlBrowse!=NULL)
  55. {
  56. if(::SHGetPathFromIDList(lpidlBrowse,szFolderPath))
  57. {
  58. root_path =CT2A(szFolderPath);
  59. ret =running;
  60. std::queue<CString> empty_;
  61. path_stack.swap(empty_);
  62. path_stack.push(CString(root_path.c_str()));
  63. m_cur_find_type = unstart;
  64. }
  65. }
  66. if(lpidlBrowse!=NULL)
  67. {
  68. ::CoTaskMemFree(lpidlBrowse);
  69. }
  70. }
  71. else
  72. {
  73. root_path = g_img_dir;
  74. ret = running;
  75. std::queue<CString> empty_;
  76. path_stack.swap(empty_);
  77. path_stack.push(CString(g_img_dir.c_str()));
  78. m_cur_find_type = unstart;
  79. }
  80. return ret;
  81. }
  82. extern std::string UnicodeToAnsi(const CString& str);
  83. int CFolderFeeder::CheckFileCountValid()
  84. {
  85. auto funPostMessage = [&](int nType, string strMsg, string strDir)
  86. {
  87. IDENTIFOR_ERROR_MSG* pMsg = new IDENTIFOR_ERROR_MSG;
  88. pMsg->type = nType;
  89. strcpy(pMsg->msg, strMsg.c_str());
  90. char* pStr = (char*)calloc(strDir.size() + 1, 1);
  91. strcpy(pStr, strDir.c_str());
  92. ::PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)pMsg, (LPARAM)pStr);
  93. };
  94. std::queue<CString> queuePath = path_stack;
  95. int nFileCount = 0;
  96. do
  97. {
  98. do
  99. {
  100. if (current_file_type == FT_NONE)current_file_type++;
  101. if (m_cur_find_type == unstart)
  102. {
  103. switch (current_file_type)
  104. {
  105. case FT_JPG:
  106. m_finished = finder.FindFile(queuePath.front() + _T("\\*.JPG"));
  107. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  108. break;
  109. case FT_JPEG:
  110. m_finished = finder.FindFile(queuePath.front() + _T("\\*.JPEG"));
  111. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  112. break;
  113. case FT_PNG:
  114. m_finished = finder.FindFile(queuePath.front() + _T("\\*.PNG"));
  115. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  116. break;
  117. case FT_BMP:
  118. m_finished = finder.FindFile(queuePath.front() + _T("\\*.BMP"));
  119. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  120. break;
  121. case FT_TIF:
  122. m_finished = finder.FindFile(queuePath.front() + _T("\\*.TIF"));
  123. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  124. break;
  125. case FT_TIFF:
  126. m_finished = finder.FindFile(queuePath.front() + _T("\\*.TIFF"));
  127. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  128. break;
  129. }
  130. m_cur_find_type = file;
  131. }
  132. int nFileNameLength = 0;
  133. bool bSameLength = true;
  134. while (m_cur_find_type == file&&m_finished)
  135. {
  136. m_finished = finder.FindNextFile();
  137. if (finder.IsDirectory())continue;
  138. int nLength = finder.GetFilePath().GetLength();
  139. if (nFileNameLength ==0)
  140. {
  141. nFileNameLength = nLength;
  142. }
  143. else if (nFileNameLength != nLength)
  144. {
  145. bSameLength = false;
  146. break;
  147. }
  148. nFileCount++;
  149. }
  150. //检查文件夹的图片非法
  151. if (!bSameLength)
  152. {
  153. string strDir = UnicodeToAnsi(queuePath.front());
  154. funPostMessage(3, "所选文件夹中,文件命名长度不一致,为防止出现大量识别错误,请检查图片顺序并统一命名后重新扫描", strDir);
  155. return 1;
  156. }
  157. //检查文件夹的图片非法KO
  158. if (m_nPageNumber && nFileCount && nFileCount%m_nPageNumber)
  159. {
  160. string strDir = UnicodeToAnsi(queuePath.front());
  161. funPostMessage(2, "所选文件夹中,图像总数非模板的整数倍,存在缺页,请检查后重新扫描", strDir);
  162. return 1;
  163. }
  164. current_file_type++;
  165. m_cur_find_type = unstart;
  166. } while (current_file_type < FT_END);
  167. current_file_type = FT_NONE;
  168. m_cur_find_type = dir;
  169. m_finished = finder.FindFile(queuePath.front() + _T("\\*.*"));
  170. while (m_finished)
  171. {
  172. m_finished = finder.FindNextFile();
  173. if (finder.IsDirectory() && !finder.IsDots())
  174. {
  175. queuePath.push(finder.GetFilePath());
  176. }
  177. }
  178. queuePath.pop();
  179. m_cur_find_type = unstart;
  180. }
  181. while (queuePath.size()>0);
  182. if (nFileCount < 1)
  183. {
  184. if (m_hwnd != NULL)
  185. {
  186. if (g_my_continue_scan != 1)
  187. {
  188. IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
  189. msg->type = 1;
  190. strcpy(msg->msg, "文件夹中没有任何图像(*.jpg,*.png,*.bmp)文件!");
  191. PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
  192. return 1;
  193. }
  194. }
  195. }
  196. return 0;
  197. }
  198. ServiceState CFolderFeeder::OnRunning( void )
  199. {
  200. ServiceState nextState = IService::OnRunning();
  201. CString strDbg;
  202. if(nextState != running) return nextState;
  203. if(m_img==NULL)
  204. {
  205. do
  206. {
  207. do
  208. {
  209. if(current_file_type==FT_NONE)current_file_type++;
  210. if(m_cur_find_type ==unstart)
  211. {
  212. switch(current_file_type)
  213. {
  214. case FT_JPG:
  215. strDbg = path_stack.front();
  216. m_finished =finder.FindFile(path_stack.front()+_T("\\*.JPG"));
  217. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  218. break;
  219. case FT_JPEG:
  220. m_finished =finder.FindFile(path_stack.front()+_T("\\*.JPEG"));
  221. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  222. break;
  223. case FT_PNG:
  224. m_finished =finder.FindFile(path_stack.front()+_T("\\*.PNG"));
  225. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  226. break;
  227. case FT_BMP:
  228. m_finished =finder.FindFile(path_stack.front()+_T("\\*.BMP"));
  229. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  230. break;
  231. case FT_TIF:
  232. m_finished =finder.FindFile(path_stack.front()+_T("\\*.TIF"));
  233. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  234. break;
  235. case FT_TIFF:
  236. m_finished =finder.FindFile(path_stack.front()+_T("\\*.TIFF"));
  237. m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  238. break;
  239. }
  240. m_cur_find_type = file;
  241. }
  242. while(m_cur_find_type==file&&m_finished)
  243. {
  244. m_finished = finder.FindNextFile();
  245. if(finder.IsDirectory())continue;
  246. CString img_path =finder.GetFilePath();
  247. if (m_reStartFlag)
  248. {
  249. if (m_reStartFile == img_path)
  250. {
  251. m_reStartFlag = false;
  252. continue;
  253. }
  254. else
  255. {
  256. continue;
  257. }
  258. }
  259. OutputDebugString(_T("读取试卷")+img_path+_T("\r\n"));
  260. m_next_param.img_path =CT2A(img_path);
  261. m_img =cvLoadImage(m_next_param.img_path.c_str(),1);
  262. m_count++;
  263. return running;
  264. }
  265. current_file_type++;
  266. m_cur_find_type=unstart;
  267. }
  268. while (current_file_type<FT_END);
  269. current_file_type =FT_NONE;
  270. m_cur_find_type =dir;
  271. m_finished=finder.FindFile(path_stack.front()+_T("\\*.*"));
  272. while(m_finished){
  273. m_finished = finder.FindNextFile();
  274. if(finder.IsDirectory()&&!finder.IsDots()){
  275. path_stack.push(finder.GetFilePath());
  276. }
  277. }
  278. path_stack.pop();
  279. m_cur_find_type = unstart;
  280. } while (path_stack.size()>0);
  281. if(m_count<1)
  282. {
  283. if(m_hwnd != NULL)
  284. {
  285. if (g_my_continue_scan != 1)
  286. {
  287. IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
  288. msg->type = 1;
  289. strcpy(msg->msg, "文件夹中没有任何图像(*.jpg,*.png,*.bmp)文件!");
  290. PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
  291. }
  292. }
  293. }
  294. return stoping;
  295. }else{
  296. Sleep(10);
  297. return running;
  298. }
  299. }
  300. bool CFolderFeeder::GetNext( BOOL wait,IplImage* & img, feeder_param& param )
  301. {
  302. do
  303. {
  304. BOOL isStoped = m_serviceState == stoped;
  305. if(m_img!=NULL){
  306. EnterCriticalSection(&m_img_lock);
  307. if(m_img!=NULL){
  308. img = m_img;
  309. param=m_next_param;
  310. m_img =NULL;
  311. LeaveCriticalSection(&m_img_lock);
  312. return true;
  313. }
  314. LeaveCriticalSection(&m_img_lock);
  315. }
  316. if(isStoped)return false;
  317. if(wait)Sleep(10);
  318. } while (wait);
  319. return false;
  320. }
  321. void CFolderFeeder::SetFolder( const std::string &img_dir )
  322. {
  323. std::queue<CString> empty_;
  324. path_stack.swap(empty_);
  325. root_path =img_dir;
  326. path_stack.push(CString(root_path.c_str()));
  327. m_cur_find_type = unstart;
  328. if (g_my_scan_type == 1)
  329. g_img_dir = img_dir;
  330. }
  331. void CFolderFeeder::SetReStartConfig(CString file, bool flag)
  332. {
  333. m_reStartFile = file;
  334. m_reStartFlag = flag;
  335. }