#include "StdAfx.h" #include #include "Ocr_Dict.h" #include #include "Common_Funcs.h" #include "common_api.h" typedef struct _WordNode { ULONG offset; string Word; }WORDNODE; void InsertWordInLtoR(vector &list,WORDNODE &node) { vector::iterator iter = list.begin(); while(iter != list.end()) { if(node.offset > iter->offset) { ++iter; continue; } list.insert(iter,node); return; } list.push_back(node); } Ocr_Dict::Ocr_Dict(void) { } Ocr_Dict::~Ocr_Dict(void) { ULONG i = 0; OcrNode *pNode; for(i = 0;i < m_nodes.size();i++) { pNode = m_nodes[i]; delete pNode; } m_nodes.clear(); } //in topleft -> bottomright sequance BOOL Ocr_Dict::OcrFindMatchs(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string OUT &str) { DWORD i; int cutneed;//0 del,1 keep, 2 cut ULONG x,y; BOOL ret = FALSE; WORDNODE node; NormalizeRect(area); RECT subArea; RECT tempArea; vector arealists; arealists.push_back(area); vector templist; for(i = 0;i < m_nodes.size();i++) { //need more effeciancy,and Bug Fix Needed!!! //cut it to slide vector::iterator iter = arealists.begin(); while(iter != arealists.end()) { subArea = (*iter); cutneed = 0; node.Word = m_nodes[i]->m_NodeName; if(FindMatchPoint(fullPic,subArea,color,sim,node.Word,x,y) == TRUE) { ret = TRUE; node.offset = x - area.left; InsertWordInLtoR(templist,node); //left half tempArea = subArea; tempArea.right = x; if(tempArea.right > tempArea.left) { cutneed = 1; //push it (*iter) = tempArea; //arealists.insert(iter,tempArea) //j = 0; } //right half subArea.left = x; subArea.left += m_nodes[i]->m_size.cx; if(subArea.left < subArea.right) { if(cutneed == 1) iter = arealists.insert(iter,subArea); else (*iter) = tempArea; } if(cutneed == 0) { //erase iter = arealists.erase(iter); } continue; } ++iter; } } str = ""; //get strings for(i = 0;i < templist.size();i++) { str += templist[i].Word; } return ret; } ULONG Ocr_Dict::FindMatchPoint_Internal(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string IN &str) { return 0; } //in topleft -> bottomright sequance BOOL Ocr_Dict::FindMatchPoint(OcrScreen &fullPic, RECT &area, OCRCOLOR color, ULONG sim, string IN &str, ULONG &x, ULONG &y) { ULONG j = 0; ULONG i = 0; OcrNode* pNode = NULL; 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) { return FALSE; } for(i = 0;i < m_nodes.size();i++) { if (_stricmp(m_nodes[i]->m_NodeName.c_str(), str.c_str()) == 0) { pNode = m_nodes[i]; break; } } if(pNode == NULL) { return FALSE; } if(pNode->m_NodeName == string("gtitle")) { #ifdef _DEBUG fullPic.SaveTheAreaToBitMapFile(area); #endif } //start here POINT startpt; RECT startoffset; LONG jumpline; if ((GetRectWidth(area) < pNode->m_size.cx) || (GetRectHeight(area) < pNode->m_size.cy)) { return FALSE; } for (j = 0; j < (ULONG)GetRectHeight(area); j++) { for (i = 0; i < (ULONG)GetRectWidth(area); i++) { if(j == 38) { j = j; } startoffset.left = area.left + i; startoffset.top = area.top + j; startoffset.right = startoffset.left + pNode->m_size.cx - 1; startoffset.bottom = startoffset.top + pNode->m_size.cy - 1; NormalizeRect(startoffset); if (RectContainPt(area, GetRectTopLeft(startoffset)) == FALSE || RectContainPt(area, GetRectBottomRight(startoffset)) == FALSE) { i = GetRectWidth(area);//just jump out } else { //ready to check startpt.x = startoffset.left; startpt.y = startoffset.top; jumpline = pNode->CheckMatch(fullPic,startpt,color,sim,str); if(jumpline < 0) { x = startpt.x; y = startpt.y; return TRUE; } else if(jumpline > 0) { i = i + jumpline - 1; } } } } return FALSE; } BOOL Ocr_Dict::IsReady() { if(m_nodes.size() > 0) { return TRUE; } return FALSE; } BOOL Ocr_Dict::IsNodeExist(string &keynode) { ULONG i = 0; OcrNode *pNode; for(i = 0;i < m_nodes.size();i++) { pNode = m_nodes[i]; if (_stricmp(pNode->m_NodeName.c_str(), keynode.c_str()) == 0) { //got a match return TRUE; } } return FALSE; } inline bool ForSort(OcrNode* &pA,OcrNode* &pB) { return (pA->m_TotalPixelsCount > pB->m_TotalPixelsCount); } void Ocr_Dict::ReSortSkiny() { sort(m_nodes.begin(),m_nodes.end(),ForSort); } BOOL Ocr_Dict::LoadOcrDictFromDirectory(const TCHAR *pszDirName) { string Path; GetModulePath(Path); Path += "\\"; Path += pszDirName; if(PathIsDirectory(Path.c_str()) == FALSE) { return FALSE; } //start here vector filelist; FindSubFiles(Path, filelist, false, string("\\*.bmp")); for (DWORD i = 0; i < filelist.size(); i++) { OcrNode *pNode = new OcrNode; if (pNode->LoadOcrFromFile(filelist[i].c_str()) == FALSE) { return FALSE; } m_nodes.push_back(pNode); } if(m_nodes.size() > 0) { ReSortSkiny(); return TRUE; } return FALSE; }