OnlineCardIdentifor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "stdafx.h"
  2. #include "OnlineCardIdentifor.h"
  3. namespace OnLineCard{
  4. class RCS_AUTO_LOCK{
  5. public:
  6. RCS_AUTO_LOCK(RTL_CRITICAL_SECTION*cs) :_cs(cs){
  7. //// printf("加锁 %p\n", _cs);
  8. //::EnterCriticalSection(_cs);
  9. }
  10. ~RCS_AUTO_LOCK(){
  11. //// printf("退出锁 %p\n", _cs);
  12. //LeaveCriticalSection(_cs);
  13. }
  14. private:
  15. CRITICAL_SECTION* _cs;
  16. };
  17. COnlineCardIdentifor::COnlineCardIdentifor():
  18. m_result_buffer(nullptr)
  19. {
  20. InitializeCriticalSection(&m_althom_rcs);
  21. InitializeCriticalSection(&m_buffer_rcs);
  22. }
  23. COnlineCardIdentifor::~COnlineCardIdentifor()
  24. {
  25. for (int i = 0; i < max_result_buffer_count; i++){
  26. result[i] = NULL;
  27. }
  28. delete[]((OMR_RESULT*)m_result_buffer);
  29. DeleteCriticalSection(&m_althom_rcs);
  30. DeleteCriticalSection(&m_buffer_rcs);
  31. }
  32. ServiceState COnlineCardIdentifor::OnStarting(void)
  33. {
  34. m_next_result_index = 0;
  35. for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
  36. {
  37. m_Wordkers[i].Start();
  38. }
  39. // printf("申请总缓冲区\n");
  40. m_result_buffer = new OMR_RESULT[max_result_buffer_count];
  41. for (int i = 0; i < max_result_buffer_count; i++)
  42. {
  43. result[i] = &((OMR_RESULT*)m_result_buffer)[i];
  44. m_buffer_state[i] = result_buffer_state::unused;
  45. }
  46. return IService::OnStarting();
  47. }
  48. void COnlineCardIdentifor::FreeResult(identify::result::OMR_RESULT * &result)
  49. {
  50. RCS_AUTO_LOCK _lock(&m_buffer_rcs);
  51. for (int i = 0; i < max_result_buffer_count; i++)
  52. {
  53. if (this->result[i] == result){
  54. m_buffer_state[i] = unused; result = NULL;
  55. break;
  56. }
  57. }
  58. }
  59. int COnlineCardIdentifor::GetMubanCount()
  60. {
  61. if (m_template)return m_template->pages.size();
  62. else return 0;
  63. }
  64. ServiceState COnlineCardIdentifor::OnRunning(void)
  65. {
  66. IplImage * img = NULL;
  67. IFeeder* feeder = NULL;
  68. CIdentifyTask* task = NULL;
  69. feeder_param imgparam;
  70. bool getImgSuccess = false;
  71. do
  72. {
  73. task = taskMananger->getCurrentIdentifyTask();
  74. if (task != NULL){
  75. feeder = task->GetFeeder();
  76. getImgSuccess = feeder->GetNext(TRUE, img, imgparam);
  77. if (!getImgSuccess){
  78. task->getResultBufferManager()->setIdentifyComplete(true);
  79. taskMananger->getNextIdentifyTask();
  80. }
  81. }
  82. } while (task != NULL&&task != taskMananger->MainTask() && img == NULL);
  83. if (!getImgSuccess) {
  84. return stoping;
  85. }
  86. int out_size = max_result_buffer;
  87. int buffer_index = -1;
  88. void * out_result = NULL;
  89. while (out_result == NULL){
  90. {
  91. RCS_AUTO_LOCK _lock(&m_buffer_rcs);
  92. for (int i = 0; i < max_result_buffer_count; i++)
  93. {
  94. int _index = (m_next_result_index + i) % max_result_buffer_count;
  95. if (m_buffer_state[_index] != unused) continue;
  96. out_result = result[_index];
  97. buffer_index = _index;
  98. m_buffer_state[buffer_index] = identifing;
  99. break;
  100. }
  101. }
  102. if (out_result == NULL)Sleep(10);
  103. }
  104. COnlineCardIdentifyWorker * worker = NULL;
  105. while (worker == NULL){
  106. for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
  107. {
  108. if (m_Wordkers[i].GetIdentifyState() == COnlineCardIdentifyWorker::identify_state::waiting){
  109. worker = &m_Wordkers[i];
  110. break;
  111. }
  112. }
  113. if (worker == NULL)Sleep(10);
  114. }
  115. identify::result::OMR_RESULT& omr_result = *(identify::result::OMR_RESULT*)(out_result);
  116. omr_result.reset();
  117. omr_result.phy_card_number = phy_card_number++;
  118. if (m_bUseQr){
  119. omr_result.paper_id = imgparam.paper_id;
  120. }
  121. else{
  122. omr_result.paper_id = -1;
  123. }
  124. omr_result.img_path = imgparam.img_path;
  125. if (imgparam.srcimg_type == SRC_IMG_HANDLE_TYPE_COPY){
  126. string img_path0 = imgparam.img_path;
  127. CStringA img_name = PathFindFileNameA(img_path0.c_str());
  128. CString img_path;
  129. img_path.Format(_T("%s\\%s"), m_exc_img_dir,/*GetTimeString()+ _T("_") +*/CString(img_name));
  130. char img_path_c[520];
  131. WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
  132. omr_result.img_path = img_path_c;
  133. omr_result.img_oldpath = imgparam.img_path;
  134. CopyFile(CString(imgparam.img_path.c_str()), img_path, FALSE);
  135. }
  136. else if (imgparam.srcimg_type == SRC_IMG_HANDLE_TYPE_SAVE){
  137. CString img_path;
  138. img_path.Format(_T("%s\\%06d.jpg"), m_exc_img_dir, omr_result.phy_card_number);;
  139. char img_path_c[520];
  140. WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
  141. omr_result.img_path = img_path_c;
  142. omr_result.img_oldpath = imgparam.img_path;
  143. cvSaveImage(omr_result.img_path.c_str(), img);
  144. }
  145. else{
  146. omr_result.img_path = imgparam.img_path;
  147. omr_result.img_oldpath = imgparam.img_path;
  148. }
  149. worker->SetRCS(&m_althom_rcs,&m_buffer_rcs);
  150. worker->SetTemplate(m_template);
  151. worker->SetQr(m_bUseQr, m_strQr);
  152. worker->SetOnlineScanType(m_nOnlineScanType);
  153. worker->OnLineCardIdentify(img, out_result, out_size, &m_buffer_state[buffer_index]);
  154. return running;
  155. }
  156. ServiceState COnlineCardIdentifor::OnStoping(void)
  157. {
  158. // printf("COnlineCardIdentifor stop\n");
  159. for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
  160. {
  161. m_Wordkers[i].Stop();
  162. }
  163. for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
  164. {
  165. while (m_Wordkers[i].GetServiceSate() != stoped){
  166. Sleep(10);
  167. }
  168. }
  169. return IService::OnStoping();
  170. }
  171. OMR_RESULT * COnlineCardIdentifor::GetResult(BOOL wait)
  172. {
  173. do
  174. {
  175. {
  176. RCS_AUTO_LOCK _lock(&m_buffer_rcs);
  177. if (m_buffer_state[m_next_result_index] == identified){
  178. m_buffer_state[m_next_result_index] = handling;
  179. OMR_RESULT *ret = (OMR_RESULT *)result[m_next_result_index];
  180. m_next_result_index = (m_next_result_index + 1) % max_result_buffer_count;
  181. return ret;
  182. }
  183. BOOL isstoped = GetServiceSate() == stoped;
  184. if (isstoped&&m_buffer_state[m_next_result_index] == unused)return NULL;
  185. }
  186. if (wait)Sleep(10);
  187. } while (wait);
  188. return NULL;
  189. }
  190. }