RescanFromServerFeeder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "StdAfx.h"
  2. #include "RescanFromServerFeeder.h"
  3. #include "..\EvaluationUtil\HttpClient.h"
  4. #include "ServerConfig.h"
  5. CRescanFromServerFeeder::CRescanFromServerFeeder(void)
  6. {
  7. m_img =NULL;
  8. InitializeCriticalSection(&m_img_lock);
  9. }
  10. CRescanFromServerFeeder::~CRescanFromServerFeeder(void)
  11. {
  12. DeleteCriticalSection(&m_img_lock);
  13. }
  14. ServiceState CRescanFromServerFeeder::OnStarting( void )
  15. {
  16. m_img = NULL;
  17. m_count =0;
  18. CHttpClient httpClient;
  19. string response;
  20. CString url_str;
  21. url_str.Format(_T("%s/%s"),CServerConfig::server_address_type==SAT_LOCAL_IN?CServerConfig::server_url_local_in:CServerConfig::server_address_type==SAT_LOCAL_OUT?CServerConfig::server_url_local_out:CServerConfig::server_url,_T("scan/findPaperImages"));
  22. CString postData;
  23. if(m_mode==MODE_EXAMINNE)postData.Format(_T("examId=%ld&examCourseId=%ld&batch=%s&examineeId=%s"),examId,examCourseId,m_batch,m_examineeId);
  24. else postData.Format(_T("examId=%ld&examCourseId=%ld&batch=%s"),examId,examCourseId,m_batch);
  25. httpClient.HttpPost(url_str,postData,response);
  26. Json::Features features;
  27. Json::Reader re(features);
  28. Json::Value root;
  29. re.parse(response,root);
  30. if(!(root["success"].isInt()&&root["success"].asInt())){
  31. return stoping;
  32. }
  33. Json::Value rows =root["object"];
  34. if(!(rows.isArray()&&rows.size()>0)){
  35. return stoping;
  36. }
  37. m_imginfos =rows;
  38. sidd.setImageInfo(&m_imginfos);
  39. sidd.setImageDownloadDir(m_imgDownloadDir);
  40. sidd.Start();
  41. return ServiceState::running;
  42. }
  43. ServiceState CRescanFromServerFeeder::OnRunning( void )
  44. {
  45. ServiceState nextState = IService::OnRunning();
  46. if(nextState != running) return nextState;
  47. if(m_img==NULL){
  48. CString img_path;int img_index;
  49. bool success =sidd.GetNext(img_path,img_index);
  50. if(success){
  51. char img_path1[512];
  52. int len = WideCharToMultiByte(CP_ACP,0,img_path,-1,NULL,0,NULL,NULL);
  53. len=WideCharToMultiByte(CP_ACP,0,img_path,-1,img_path1,len,NULL,NULL);
  54. img_path1[len]=0;
  55. m_img =cvLoadImage(img_path1,1);
  56. m_param.type = identyfi_type_normal;
  57. m_param.srcimg_type = SRC_IMG_HANDLE_TYPE_COPY;
  58. m_param.img_path =img_path1;
  59. m_count++;
  60. return running;
  61. }else {
  62. return stoping;
  63. }
  64. }else{
  65. Sleep(10);
  66. return running;
  67. }
  68. }
  69. ServiceState CRescanFromServerFeeder::OnStoping( void )
  70. {
  71. m_imginfos=Json::Value();
  72. return stoped;
  73. }
  74. bool CRescanFromServerFeeder::GetNext( BOOL wait,IplImage* & img, feeder_param& param )
  75. {
  76. do
  77. {
  78. BOOL isStoped = m_serviceState == stoped;
  79. if(m_img!=NULL){
  80. EnterCriticalSection(&m_img_lock);
  81. if(m_img!=NULL){
  82. img = m_img;
  83. param=m_param;
  84. m_img=NULL;
  85. LeaveCriticalSection(&m_img_lock);
  86. return true;
  87. }
  88. LeaveCriticalSection(&m_img_lock);
  89. }
  90. if(isStoped)return false;
  91. if(wait)Sleep(10);
  92. } while (wait);
  93. return false;
  94. }