123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- #include "StdAfx.h"
- #include <shlwapi.h>
- #include "Ocr_Dict.h"
- #include <algorithm>
- #include "Common_Funcs.h"
- #include "common_api.h"
- typedef struct _WordNode {
- ULONG offset;
- string Word;
- }WORDNODE;
- void InsertWordInLtoR(vector<WORDNODE> &list,WORDNODE &node)
- {
- vector<WORDNODE>::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<RECT> arealists;
- arealists.push_back(area);
- vector<WORDNODE> templist;
- for(i = 0;i < m_nodes.size();i++)
- {
- //need more effeciancy,and Bug Fix Needed!!!
- //cut it to slide
- vector<RECT>::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<string> 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;
- }
|