123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #include "stdafx.h"
- #include "OnlineCardIdentifor.h"
- namespace OnLineCard{
- class RCS_AUTO_LOCK{
- public:
- RCS_AUTO_LOCK(RTL_CRITICAL_SECTION*cs) :_cs(cs){
- //// printf("加锁 %p\n", _cs);
- //::EnterCriticalSection(_cs);
- }
- ~RCS_AUTO_LOCK(){
- //// printf("退出锁 %p\n", _cs);
- //LeaveCriticalSection(_cs);
- }
- private:
- CRITICAL_SECTION* _cs;
- };
- COnlineCardIdentifor::COnlineCardIdentifor():
- m_result_buffer(nullptr)
- {
- InitializeCriticalSection(&m_althom_rcs);
- InitializeCriticalSection(&m_buffer_rcs);
- }
- COnlineCardIdentifor::~COnlineCardIdentifor()
- {
- for (int i = 0; i < max_result_buffer_count; i++){
- result[i] = NULL;
- }
- delete[]((OMR_RESULT*)m_result_buffer);
- DeleteCriticalSection(&m_althom_rcs);
- DeleteCriticalSection(&m_buffer_rcs);
- }
- ServiceState COnlineCardIdentifor::OnStarting(void)
- {
- m_next_result_index = 0;
- for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
- {
- m_Wordkers[i].Start();
- }
- // printf("申请总缓冲区\n");
- m_result_buffer = new OMR_RESULT[max_result_buffer_count];
- for (int i = 0; i < max_result_buffer_count; i++)
- {
- result[i] = &((OMR_RESULT*)m_result_buffer)[i];
- m_buffer_state[i] = result_buffer_state::unused;
- }
- return IService::OnStarting();
- }
- void COnlineCardIdentifor::FreeResult(identify::result::OMR_RESULT * &result)
- {
- RCS_AUTO_LOCK _lock(&m_buffer_rcs);
- for (int i = 0; i < max_result_buffer_count; i++)
- {
- if (this->result[i] == result){
- m_buffer_state[i] = unused; result = NULL;
- break;
- }
- }
- }
- int COnlineCardIdentifor::GetMubanCount()
- {
- if (m_template)return m_template->pages.size();
- else return 0;
- }
- ServiceState COnlineCardIdentifor::OnRunning(void)
- {
- IplImage * img = NULL;
- IFeeder* feeder = NULL;
- CIdentifyTask* task = NULL;
- feeder_param imgparam;
- bool getImgSuccess = false;
- do
- {
- task = taskMananger->getCurrentIdentifyTask();
- if (task != NULL){
- feeder = task->GetFeeder();
- getImgSuccess = feeder->GetNext(TRUE, img, imgparam);
- if (!getImgSuccess){
- task->getResultBufferManager()->setIdentifyComplete(true);
- taskMananger->getNextIdentifyTask();
- }
- }
- } while (task != NULL&&task != taskMananger->MainTask() && img == NULL);
- if (!getImgSuccess) {
- return stoping;
- }
- int out_size = max_result_buffer;
- int buffer_index = -1;
- void * out_result = NULL;
- while (out_result == NULL){
- {
- RCS_AUTO_LOCK _lock(&m_buffer_rcs);
- for (int i = 0; i < max_result_buffer_count; i++)
- {
- int _index = (m_next_result_index + i) % max_result_buffer_count;
- if (m_buffer_state[_index] != unused) continue;
- out_result = result[_index];
- buffer_index = _index;
- m_buffer_state[buffer_index] = identifing;
- break;
- }
- }
- if (out_result == NULL)Sleep(10);
- }
- COnlineCardIdentifyWorker * worker = NULL;
- while (worker == NULL){
- for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
- {
- if (m_Wordkers[i].GetIdentifyState() == COnlineCardIdentifyWorker::identify_state::waiting){
- worker = &m_Wordkers[i];
- break;
- }
- }
- if (worker == NULL)Sleep(10);
- }
- identify::result::OMR_RESULT& omr_result = *(identify::result::OMR_RESULT*)(out_result);
- omr_result.reset();
- omr_result.phy_card_number = phy_card_number++;
- if (m_bUseQr){
- omr_result.paper_id = imgparam.paper_id;
- }
- else{
- omr_result.paper_id = -1;
- }
-
- omr_result.img_path = imgparam.img_path;
- if (imgparam.srcimg_type == SRC_IMG_HANDLE_TYPE_COPY){
- string img_path0 = imgparam.img_path;
- CStringA img_name = PathFindFileNameA(img_path0.c_str());
- CString img_path;
- img_path.Format(_T("%s\\%s"), m_exc_img_dir,/*GetTimeString()+ _T("_") +*/CString(img_name));
- char img_path_c[520];
- WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
- omr_result.img_path = img_path_c;
- omr_result.img_oldpath = imgparam.img_path;
- CopyFile(CString(imgparam.img_path.c_str()), img_path, FALSE);
- }
- else if (imgparam.srcimg_type == SRC_IMG_HANDLE_TYPE_SAVE){
- CString img_path;
- img_path.Format(_T("%s\\%06d.jpg"), m_exc_img_dir, omr_result.phy_card_number);;
- char img_path_c[520];
- WideCharToMultiByte(CP_ACP, 0, img_path, -1, img_path_c, 520, NULL, NULL);
- omr_result.img_path = img_path_c;
- omr_result.img_oldpath = imgparam.img_path;
- cvSaveImage(omr_result.img_path.c_str(), img);
- }
- else{
- omr_result.img_path = imgparam.img_path;
- omr_result.img_oldpath = imgparam.img_path;
- }
- worker->SetRCS(&m_althom_rcs,&m_buffer_rcs);
- worker->SetTemplate(m_template);
- worker->SetQr(m_bUseQr, m_strQr);
- worker->SetOnlineScanType(m_nOnlineScanType);
- worker->OnLineCardIdentify(img, out_result, out_size, &m_buffer_state[buffer_index]);
- return running;
- }
- ServiceState COnlineCardIdentifor::OnStoping(void)
- {
- // printf("COnlineCardIdentifor stop\n");
- for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
- {
- m_Wordkers[i].Stop();
- }
- for (int i = 0; i < ONLINE_WORKER_COUNT; i++)
- {
- while (m_Wordkers[i].GetServiceSate() != stoped){
- Sleep(10);
- }
- }
- return IService::OnStoping();
- }
- OMR_RESULT * COnlineCardIdentifor::GetResult(BOOL wait)
- {
- do
- {
- {
- RCS_AUTO_LOCK _lock(&m_buffer_rcs);
- if (m_buffer_state[m_next_result_index] == identified){
- m_buffer_state[m_next_result_index] = handling;
- OMR_RESULT *ret = (OMR_RESULT *)result[m_next_result_index];
- m_next_result_index = (m_next_result_index + 1) % max_result_buffer_count;
- return ret;
- }
- BOOL isstoped = GetServiceSate() == stoped;
- if (isstoped&&m_buffer_state[m_next_result_index] == unused)return NULL;
- }
- if (wait)Sleep(10);
- } while (wait);
- return NULL;
- }
- }
|