Identifor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "StdAfx.h"
  2. #include "scan_common.h"
  3. #include "Identifor.h"
  4. #include <opencv\cv.h>
  5. #include <opencv\cxcore.h>
  6. #include <opencv2\opencv.hpp>
  7. #include "basic_struct.h"
  8. #include "shlwapi.h"
  9. using namespace cv;
  10. CIdentifor::CIdentifor(void)
  11. {
  12. phy_card_number =0000000L;
  13. m_bUseQr = false;
  14. }
  15. void CIdentifor::SetUseQr(bool bUseQr, const std::string&strQr)
  16. {
  17. m_bUseQr = bUseQr;
  18. m_strQr = strQr;
  19. }
  20. void CIdentifor::SetOnlineScanType(int nOnlineScanType)
  21. {
  22. m_nOnlineScanType = nOnlineScanType;
  23. }
  24. CIdentifor::~CIdentifor(void)
  25. {
  26. }
  27. BOOL CIdentifor::SupportPause( void )
  28. {
  29. return FALSE;
  30. }
  31. ServiceState CIdentifor::OnStarting( void )
  32. {
  33. for (int i =0;i<IDENTIFY_WORKER_COUNT;i++)
  34. {
  35. m_identify_worker[i].Start();
  36. }
  37. for (int i = 0; i < IDENTIFY_WORKER_COUNT; i++)
  38. {
  39. while (m_identify_worker[i].GetServiceSate() != running){
  40. Sleep(10);
  41. }
  42. }
  43. return IService::OnStarting();
  44. }
  45. ServiceState CIdentifor::OnRunning( void )
  46. {
  47. IplImage * img = NULL;
  48. IFeeder* feeder = NULL;
  49. CIdentifyTask* task = NULL;
  50. feeder_param imgparam;
  51. bool getImgSuccess =false;
  52. do
  53. {
  54. task=taskMananger->getCurrentIdentifyTask();
  55. if(task != NULL){
  56. feeder =task->GetFeeder();
  57. getImgSuccess =feeder->GetNext(TRUE,img,imgparam);
  58. if(!getImgSuccess){
  59. task->getResultBufferManager()->setIdentifyComplete(true);
  60. taskMananger->getNextIdentifyTask();
  61. }
  62. }
  63. } while (task!=NULL&&task!=taskMananger->MainTask()&&img == NULL);
  64. if(!getImgSuccess) {
  65. return stoping;
  66. }
  67. CResultManager * m_main_result_buffer =task->getResultBufferManager();
  68. OMR_RESULT * out_result = m_main_result_buffer->getNextResult();
  69. CIdentifyWorker * worker = GetWorker();
  70. worker->SetUseQr(m_bUseQr, m_strQr);
  71. CString timeString =GetTimeString();
  72. OMR_RESULT& omr_result= *(out_result);
  73. omr_result.reset();
  74. switch(imgparam.type){
  75. case identyfi_type_normal:
  76. omr_result.phy_card_number =++phy_card_number;
  77. break;
  78. case identyfi_type_reidentify:
  79. omr_result.phy_card_number = imgparam.phy_number;
  80. omr_result.paper_id = imgparam.paper_id;
  81. break;
  82. case identyfi_type_manualmatch:
  83. omr_result.phy_card_number = imgparam.phy_number;
  84. omr_result.paper_id = imgparam.paper_id;
  85. break;
  86. }
  87. if(imgparam.srcimg_type==SRC_IMG_HANDLE_TYPE_COPY){
  88. string img_path0=imgparam.img_path;
  89. CStringA img_name = PathFindFileNameA(img_path0.c_str());
  90. CString img_path;
  91. img_path.Format(_T("%s\\%s"), m_exc_img_dir, CString(img_name));
  92. char img_path_c[520];
  93. WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
  94. omr_result.img_path = img_path_c;
  95. omr_result.img_oldpath = imgparam.img_path;
  96. CopyFile(CString(imgparam.img_path.c_str()),img_path,FALSE);
  97. }else if(imgparam.srcimg_type==SRC_IMG_HANDLE_TYPE_SAVE){
  98. CString img_path;
  99. img_path.Format(_T("%s\\%06d.jpg"), m_exc_img_dir, omr_result.phy_card_number);;
  100. char img_path_c[520];
  101. WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
  102. omr_result.img_path = img_path_c;
  103. omr_result.img_oldpath = imgparam.img_path;
  104. cvSaveImage(omr_result.img_path.c_str(),img);
  105. }else{
  106. omr_result.img_path = imgparam.img_path;
  107. omr_result.img_oldpath = imgparam.img_path;
  108. }
  109. switch(imgparam.type){
  110. case identyfi_type_normal:
  111. omr_result.paper_id=-1;
  112. worker->Identify(img,out_result,m_main_result_buffer->getBufferstate(out_result));
  113. break;
  114. case identyfi_type_reidentify:
  115. case identyfi_type_manualmatch:
  116. omr_result.paper_id = imgparam.paper_id;
  117. worker->Identify(img, out_result, m_main_result_buffer->getBufferstate(out_result));
  118. break;
  119. }
  120. return running;
  121. }
  122. ServiceState CIdentifor::OnPausing( void )
  123. {
  124. return IService::OnPausing();
  125. }
  126. ServiceState CIdentifor::OnResuming( void )
  127. {
  128. return IService::OnResuming();
  129. }
  130. ServiceState CIdentifor::OnStoping( void )
  131. {
  132. for (int i=0;i<IDENTIFY_WORKER_COUNT;i++)
  133. {
  134. m_identify_worker[i].Stop();
  135. }
  136. for (int i=0;i<IDENTIFY_WORKER_COUNT;i++)
  137. {
  138. while(m_identify_worker[i].GetServiceSate() != stoped){
  139. Sleep(10);
  140. }
  141. }
  142. return IService::OnStoping();
  143. }
  144. int CIdentifor::GetMubanCount()
  145. {
  146. return m_schema->size();
  147. }
  148. void CIdentifor::SetExcImgDir( CString img_dir )
  149. {
  150. m_exc_img_dir = img_dir;
  151. }
  152. BOOL CIdentifor::LoadSchema(const boost::shared_ptr<const ISCH_Schema>& schema)
  153. {
  154. m_schema = schema;
  155. for (int i=0;i<IDENTIFY_WORKER_COUNT;i++)
  156. {
  157. m_identify_worker[i].LoadSchema(m_schema);
  158. }
  159. m_isMubanLoaded = (schema == NULL|| schema->size()==0)?FALSE:TRUE;
  160. return m_isMubanLoaded;
  161. }
  162. BOOL CIdentifor::IsMubanLoaded()
  163. {
  164. return m_isMubanLoaded;
  165. }
  166. void CIdentifor::IdentifyTaskManager( CIdentifyTaskManager* taskManager )
  167. {
  168. this->taskMananger = taskManager;
  169. }
  170. CIdentifyWorker * CIdentifor::GetWorker()
  171. {
  172. CIdentifyWorker * worker =NULL;
  173. do{
  174. for (int i=0;i<IDENTIFY_WORKER_COUNT;i++)
  175. {
  176. if(m_identify_worker[i].GetIdentifyState() == CIdentifyWorker::IS_WAITING){
  177. worker = &m_identify_worker[i];
  178. break;
  179. }
  180. }
  181. if(worker == NULL)Sleep(10);
  182. } while(worker ==NULL);
  183. return worker;
  184. }
  185. void CIdentifor::Reset()
  186. {
  187. phy_card_number =0000000L;
  188. }