123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #include "StdAfx.h"
- #include "scan_common.h"
- #include "FolderFeeder.h"
- #include "basic_struct.h"
- #include <string>
- using namespace std;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- static std::string g_img_dir;
- extern int g_my_scan_type;
- extern int g_my_continue_scan;
- CFolderFeeder::CFolderFeeder(void)
- {
- m_img =NULL;
- InitializeCriticalSection(&m_img_lock);
- m_nPageNumber = 2;//默认偶数页
- }
- CFolderFeeder::~CFolderFeeder(void)
- {
- DeleteCriticalSection(&m_img_lock);
- }
- BOOL CFolderFeeder::SupportPause( void )
- {
- return TRUE;
- }
- ServiceState CFolderFeeder::OnStarting( void )
- {
- m_next_param.type = identyfi_type_normal;
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- m_img = NULL;
- m_count =0;
- current_file_type =FT_NONE;
- if (path_stack.size() > 0)
- {
- //检查文件夹是否非法
- if (CheckFileCountValid())
- return stoping;
- return running;
- }
- ServiceState ret = stoping;
- if (g_my_scan_type == 0)
- {
- TCHAR szFolderPath[MAX_PATH] ={0};
- BROWSEINFO sInfo;
- ::ZeroMemory(&sInfo,sizeof(BROWSEINFO));
- sInfo.pidlRoot =0;
- sInfo.lpszTitle =_T("请选择一个文件夹");
- sInfo.ulFlags = BIF_DONTGOBELOWDOMAIN|BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_EDITBOX;
- sInfo.lpfn =NULL;
- LPITEMIDLIST lpidlBrowse= ::SHBrowseForFolder(&sInfo);
- if(lpidlBrowse!=NULL)
- {
- if(::SHGetPathFromIDList(lpidlBrowse,szFolderPath))
- {
- root_path =CT2A(szFolderPath);
- ret =running;
- std::queue<CString> empty_;
- path_stack.swap(empty_);
- path_stack.push(CString(root_path.c_str()));
- m_cur_find_type = unstart;
- }
- }
- if(lpidlBrowse!=NULL)
- {
- ::CoTaskMemFree(lpidlBrowse);
- }
- }
- else
- {
- root_path = g_img_dir;
- ret = running;
- std::queue<CString> empty_;
- path_stack.swap(empty_);
- path_stack.push(CString(g_img_dir.c_str()));
- m_cur_find_type = unstart;
- }
- return ret;
- }
- extern std::string UnicodeToAnsi(const CString& str);
- int CFolderFeeder::CheckFileCountValid()
- {
- auto funPostMessage = [&](int nType, string strMsg, string strDir)
- {
- IDENTIFOR_ERROR_MSG* pMsg = new IDENTIFOR_ERROR_MSG;
- pMsg->type = nType;
- strcpy(pMsg->msg, strMsg.c_str());
- char* pStr = (char*)calloc(strDir.size() + 1, 1);
- strcpy(pStr, strDir.c_str());
- ::PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)pMsg, (LPARAM)pStr);
- };
- std::queue<CString> queuePath = path_stack;
- int nFileCount = 0;
- do
- {
- do
- {
- if (current_file_type == FT_NONE)current_file_type++;
- if (m_cur_find_type == unstart)
- {
- switch (current_file_type)
- {
- case FT_JPG:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.JPG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_JPEG:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.JPEG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_PNG:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.PNG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_BMP:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.BMP"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- case FT_TIF:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.TIF"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- case FT_TIFF:
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.TIFF"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- }
- m_cur_find_type = file;
- }
- int nFileNameLength = 0;
- bool bSameLength = true;
- while (m_cur_find_type == file&&m_finished)
- {
- m_finished = finder.FindNextFile();
- if (finder.IsDirectory())continue;
- int nLength = finder.GetFilePath().GetLength();
- if (nFileNameLength ==0)
- {
- nFileNameLength = nLength;
- }
- else if (nFileNameLength != nLength)
- {
- bSameLength = false;
- break;
- }
- nFileCount++;
- }
- //检查文件夹的图片非法
- if (!bSameLength)
- {
- string strDir = UnicodeToAnsi(queuePath.front());
- funPostMessage(3, "所选文件夹中,文件命名长度不一致,为防止出现大量识别错误,请检查图片顺序并统一命名后重新扫描", strDir);
- return 1;
- }
- //检查文件夹的图片非法KO
- if (m_nPageNumber && nFileCount && nFileCount%m_nPageNumber)
- {
- string strDir = UnicodeToAnsi(queuePath.front());
- funPostMessage(2, "所选文件夹中,图像总数非模板的整数倍,存在缺页,请检查后重新扫描", strDir);
- return 1;
- }
- current_file_type++;
- m_cur_find_type = unstart;
- } while (current_file_type < FT_END);
- current_file_type = FT_NONE;
- m_cur_find_type = dir;
- m_finished = finder.FindFile(queuePath.front() + _T("\\*.*"));
- while (m_finished)
- {
- m_finished = finder.FindNextFile();
- if (finder.IsDirectory() && !finder.IsDots())
- {
- queuePath.push(finder.GetFilePath());
- }
- }
- queuePath.pop();
- m_cur_find_type = unstart;
- }
- while (queuePath.size()>0);
- if (nFileCount < 1)
- {
- if (m_hwnd != NULL)
- {
- if (g_my_continue_scan != 1)
- {
- IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
- msg->type = 1;
- strcpy(msg->msg, "文件夹中没有任何图像(*.jpg,*.png,*.bmp)文件!");
- PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
- return 1;
- }
- }
- }
- return 0;
- }
- ServiceState CFolderFeeder::OnRunning( void )
- {
- ServiceState nextState = IService::OnRunning();
- CString strDbg;
- if(nextState != running) return nextState;
- if(m_img==NULL)
- {
- do
- {
- do
- {
- if(current_file_type==FT_NONE)current_file_type++;
- if(m_cur_find_type ==unstart)
- {
- switch(current_file_type)
- {
- case FT_JPG:
- strDbg = path_stack.front();
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.JPG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_JPEG:
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.JPEG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_PNG:
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.PNG"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
- break;
- case FT_BMP:
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.BMP"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- case FT_TIF:
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.TIF"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- case FT_TIFF:
- m_finished =finder.FindFile(path_stack.front()+_T("\\*.TIFF"));
- m_next_param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- break;
- }
- m_cur_find_type = file;
- }
- while(m_cur_find_type==file&&m_finished)
- {
- m_finished = finder.FindNextFile();
- if(finder.IsDirectory())continue;
- CString img_path =finder.GetFilePath();
- if (m_reStartFlag)
- {
- if (m_reStartFile == img_path)
- {
- m_reStartFlag = false;
- continue;
- }
- else
- {
- continue;
- }
- }
- OutputDebugString(_T("读取试卷")+img_path+_T("\r\n"));
- m_next_param.img_path =CT2A(img_path);
- m_img =cvLoadImage(m_next_param.img_path.c_str(),1);
- m_count++;
- return running;
- }
- current_file_type++;
- m_cur_find_type=unstart;
- }
- while (current_file_type<FT_END);
- current_file_type =FT_NONE;
- m_cur_find_type =dir;
- m_finished=finder.FindFile(path_stack.front()+_T("\\*.*"));
- while(m_finished){
- m_finished = finder.FindNextFile();
- if(finder.IsDirectory()&&!finder.IsDots()){
- path_stack.push(finder.GetFilePath());
- }
- }
- path_stack.pop();
- m_cur_find_type = unstart;
- } while (path_stack.size()>0);
- if(m_count<1)
- {
- if(m_hwnd != NULL)
- {
- if (g_my_continue_scan != 1)
- {
- IDENTIFOR_ERROR_MSG* msg = new IDENTIFOR_ERROR_MSG;
- msg->type = 1;
- strcpy(msg->msg, "文件夹中没有任何图像(*.jpg,*.png,*.bmp)文件!");
- PostMessage(m_hwnd, WM_IDENTIFOR_ERROR, (WPARAM)msg, 0);
- }
- }
- }
- return stoping;
- }else{
- Sleep(10);
- return running;
- }
- }
- bool CFolderFeeder::GetNext( BOOL wait,IplImage* & img, feeder_param& param )
- {
- do
- {
- BOOL isStoped = m_serviceState == stoped;
- if(m_img!=NULL){
- EnterCriticalSection(&m_img_lock);
- if(m_img!=NULL){
- img = m_img;
- param=m_next_param;
- m_img =NULL;
- LeaveCriticalSection(&m_img_lock);
- return true;
- }
- LeaveCriticalSection(&m_img_lock);
- }
- if(isStoped)return false;
- if(wait)Sleep(10);
- } while (wait);
- return false;
- }
- void CFolderFeeder::SetFolder( const std::string &img_dir )
- {
- std::queue<CString> empty_;
- path_stack.swap(empty_);
- root_path =img_dir;
- path_stack.push(CString(root_path.c_str()));
- m_cur_find_type = unstart;
- if (g_my_scan_type == 1)
- g_img_dir = img_dir;
- }
- void CFolderFeeder::SetReStartConfig(CString file, bool flag)
- {
- m_reStartFile = file;
- m_reStartFlag = flag;
- }
|