Ocr_Dict.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "StdAfx.h"
  2. #include <shlwapi.h>
  3. #include "Ocr_Dict.h"
  4. #include <algorithm>
  5. #include "Common_Funcs.h"
  6. #include "common_api.h"
  7. typedef struct _WordNode {
  8. ULONG offset;
  9. string Word;
  10. }WORDNODE;
  11. void InsertWordInLtoR(vector<WORDNODE> &list,WORDNODE &node)
  12. {
  13. vector<WORDNODE>::iterator iter = list.begin();
  14. while(iter != list.end())
  15. {
  16. if(node.offset > iter->offset)
  17. {
  18. ++iter;
  19. continue;
  20. }
  21. list.insert(iter,node);
  22. return;
  23. }
  24. list.push_back(node);
  25. }
  26. Ocr_Dict::Ocr_Dict(void)
  27. {
  28. }
  29. Ocr_Dict::~Ocr_Dict(void)
  30. {
  31. ULONG i = 0;
  32. OcrNode *pNode;
  33. for(i = 0;i < m_nodes.size();i++)
  34. {
  35. pNode = m_nodes[i];
  36. delete pNode;
  37. }
  38. m_nodes.clear();
  39. }
  40. //in topleft -> bottomright sequance
  41. BOOL Ocr_Dict::OcrFindMatchs(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string OUT &str)
  42. {
  43. DWORD i;
  44. int cutneed;//0 del,1 keep, 2 cut
  45. ULONG x,y;
  46. BOOL ret = FALSE;
  47. WORDNODE node;
  48. NormalizeRect(area);
  49. RECT subArea;
  50. RECT tempArea;
  51. vector<RECT> arealists;
  52. arealists.push_back(area);
  53. vector<WORDNODE> templist;
  54. for(i = 0;i < m_nodes.size();i++)
  55. {
  56. //need more effeciancy,and Bug Fix Needed!!!
  57. //cut it to slide
  58. vector<RECT>::iterator iter = arealists.begin();
  59. while(iter != arealists.end())
  60. {
  61. subArea = (*iter);
  62. cutneed = 0;
  63. node.Word = m_nodes[i]->m_NodeName;
  64. if(FindMatchPoint(fullPic,subArea,color,sim,node.Word,x,y) == TRUE)
  65. {
  66. ret = TRUE;
  67. node.offset = x - area.left;
  68. InsertWordInLtoR(templist,node);
  69. //left half
  70. tempArea = subArea;
  71. tempArea.right = x;
  72. if(tempArea.right > tempArea.left)
  73. {
  74. cutneed = 1;
  75. //push it
  76. (*iter) = tempArea;
  77. //arealists.insert(iter,tempArea)
  78. //j = 0;
  79. }
  80. //right half
  81. subArea.left = x;
  82. subArea.left += m_nodes[i]->m_size.cx;
  83. if(subArea.left < subArea.right)
  84. {
  85. if(cutneed == 1)
  86. iter = arealists.insert(iter,subArea);
  87. else
  88. (*iter) = tempArea;
  89. }
  90. if(cutneed == 0)
  91. {
  92. //erase
  93. iter = arealists.erase(iter);
  94. }
  95. continue;
  96. }
  97. ++iter;
  98. }
  99. }
  100. str = "";
  101. //get strings
  102. for(i = 0;i < templist.size();i++)
  103. {
  104. str += templist[i].Word;
  105. }
  106. return ret;
  107. }
  108. ULONG Ocr_Dict::FindMatchPoint_Internal(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string IN &str)
  109. {
  110. return 0;
  111. }
  112. //in topleft -> bottomright sequance
  113. BOOL Ocr_Dict::FindMatchPoint(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string IN &str, ULONG &x, ULONG &y)
  114. {
  115. ULONG j = 0;
  116. ULONG i = 0;
  117. OcrNode* pNode = NULL;
  118. if (fullPic.m_CurrentSize.cx < GetRectWidth(area) || fullPic.m_CurrentSize.cy < GetRectHeight(area) || area.left > fullPic.m_CurrentSize.cx || area.top > fullPic.m_CurrentSize.cy)
  119. {
  120. return FALSE;
  121. }
  122. for(i = 0;i < m_nodes.size();i++)
  123. {
  124. if (_stricmp(m_nodes[i]->m_NodeName.c_str(), str.c_str()) == 0)
  125. {
  126. pNode = m_nodes[i];
  127. break;
  128. }
  129. }
  130. if(pNode == NULL)
  131. {
  132. return FALSE;
  133. }
  134. if(pNode->m_NodeName == string("gtitle"))
  135. {
  136. #ifdef _DEBUG
  137. fullPic.SaveTheAreaToBitMapFile(area);
  138. #endif
  139. }
  140. //start here
  141. POINT startpt;
  142. RECT startoffset;
  143. LONG jumpline;
  144. if ((GetRectWidth(area) < pNode->m_size.cx) || (GetRectHeight(area) < pNode->m_size.cy))
  145. {
  146. return FALSE;
  147. }
  148. for (j = 0; j < (ULONG)GetRectHeight(area); j++)
  149. {
  150. for (i = 0; i < (ULONG)GetRectWidth(area); i++)
  151. {
  152. if(j == 38)
  153. {
  154. j = j;
  155. }
  156. startoffset.left = area.left + i;
  157. startoffset.top = area.top + j;
  158. startoffset.right = startoffset.left + pNode->m_size.cx - 1;
  159. startoffset.bottom = startoffset.top + pNode->m_size.cy - 1;
  160. NormalizeRect(startoffset);
  161. if (RectContainPt(area, GetRectTopLeft(startoffset)) == FALSE
  162. || RectContainPt(area, GetRectBottomRight(startoffset)) == FALSE)
  163. {
  164. i = GetRectWidth(area);//just jump out
  165. }
  166. else
  167. {
  168. //ready to check
  169. startpt.x = startoffset.left;
  170. startpt.y = startoffset.top;
  171. jumpline = pNode->CheckMatch(fullPic,startpt,color,sim,str);
  172. if(jumpline < 0)
  173. {
  174. x = startpt.x;
  175. y = startpt.y;
  176. return TRUE;
  177. }
  178. else if(jumpline > 0)
  179. {
  180. i = i + jumpline - 1;
  181. }
  182. }
  183. }
  184. }
  185. return FALSE;
  186. }
  187. BOOL Ocr_Dict::IsReady()
  188. {
  189. if(m_nodes.size() > 0)
  190. {
  191. return TRUE;
  192. }
  193. return FALSE;
  194. }
  195. BOOL Ocr_Dict::IsNodeExist(string &keynode)
  196. {
  197. ULONG i = 0;
  198. OcrNode *pNode;
  199. for(i = 0;i < m_nodes.size();i++)
  200. {
  201. pNode = m_nodes[i];
  202. if (_stricmp(pNode->m_NodeName.c_str(), keynode.c_str()) == 0)
  203. {
  204. //got a match
  205. return TRUE;
  206. }
  207. }
  208. return FALSE;
  209. }
  210. inline bool ForSort(OcrNode* &pA,OcrNode* &pB)
  211. {
  212. return (pA->m_TotalPixelsCount > pB->m_TotalPixelsCount);
  213. }
  214. void Ocr_Dict::ReSortSkiny()
  215. {
  216. sort(m_nodes.begin(),m_nodes.end(),ForSort);
  217. }
  218. BOOL Ocr_Dict::LoadOcrDictFromDirectory(const TCHAR *pszDirName)
  219. {
  220. string Path;
  221. GetModulePath(Path);
  222. Path += "\\";
  223. Path += pszDirName;
  224. if(PathIsDirectory(Path.c_str()) == FALSE)
  225. {
  226. return FALSE;
  227. }
  228. //start here
  229. vector<string> filelist;
  230. FindSubFiles(Path, filelist, false, string("\\*.bmp"));
  231. for (DWORD i = 0; i < filelist.size(); i++)
  232. {
  233. OcrNode *pNode = new OcrNode;
  234. if (pNode->LoadOcrFromFile(filelist[i].c_str()) == FALSE)
  235. {
  236. return FALSE;
  237. }
  238. m_nodes.push_back(pNode);
  239. }
  240. if(m_nodes.size() > 0)
  241. {
  242. ReSortSkiny();
  243. return TRUE;
  244. }
  245. return FALSE;
  246. }