diff --git a/huagao/Device/GScanO1003399.cpp b/huagao/Device/GScanO1003399.cpp index 459f6f4b..5f7ef565 100644 --- a/huagao/Device/GScanO1003399.cpp +++ b/huagao/Device/GScanO1003399.cpp @@ -35,9 +35,7 @@ static void scanner_cmd(std::shared_ptr& usb, int cmd) -GScanO1003399::GScanO1003399(): - m_imgthread(1) - ,m_imgprocthread(1) +GScanO1003399::GScanO1003399() { im_data.reset(new std::vector()); //open(0x3072, 0x139); @@ -50,21 +48,28 @@ GScanO1003399::GScanO1003399(): GScanO1003399::~GScanO1003399() { - image = 0; + imagecount = 0; if (m_usbthread.get() && m_usbthread->joinable()) { b_usbthread = false; m_usbthread->join(); m_usbthread.reset(); } + m_usb.reset(); scanflag = false; b_imgprothread = false; - image = 0; - while (m_paths.size() > 0) + imgs.ShutDown(); + + while (m_paths.Size() > 0) { - remove(m_paths.front().c_str()); - m_paths.pop(); + remove(m_paths.Take().c_str()); + } + m_paths.ShutDown(); + + if (m_imgprocthread.get() && m_imgprocthread->joinable()) + { + m_imgprocthread->join(); } } @@ -110,7 +115,6 @@ void GScanO1003399::usbhotplug(bool isleft) } } - void GScanO1003399::DogEar_callback(std::function fun) { } @@ -121,7 +125,7 @@ int GScanO1003399::aquire_bmpdata(std::vector& bmpdata) StopWatch sw; while (true) { - if ((image == 0) && (!is_runing()) && (!scanflag) || (sw.elapsed_s() > 30)) + if ((imagecount == 0) && (!is_runing()) && (!scanflag) || (sw.elapsed_s() > 30)) { DoEvents(); this_thread::sleep_for(chrono::milliseconds(1)); @@ -150,7 +154,7 @@ int GScanO1003399::aquire_bmpdata(std::vector& bmpdata) FileTools::writelog(log_INFO, "aquire_bmpdata num =" + to_string(aquirenum++)); bmpdata = *(m_imagedata.Take()); UpdateScanInfo(get_imgnReaded(), countNTransfered()); - image--; + imagecount--; sw.reset(); return 0; } @@ -316,15 +320,14 @@ void GScanO1003399::Scanner_StartScan(UINT16 count) { roller_num = this->count(); scanflag = true; - while (m_paths.size() > 0) + while (m_paths.Size() > 0) { - remove(m_paths.front().c_str()); - m_paths.pop(); + remove(m_paths.Take().c_str()); } Set_ErrorCode(0); getimgnum = 0; aquirenum = 0; - image = 0; + imagecount = 0; keeplastimg = true; reset(); devState = DEV_ISRUNNING; @@ -350,40 +353,48 @@ void GScanO1003399::Scanner_StartScan(UINT16 count) //} start(); - if (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131) { - b_imgprothread = true; - m_imgprocthread.enqueue([this] { - while (b_imgprothread) - { - if (m_paths.size() < 1) - { - this_thread::sleep_for(chrono::milliseconds(10)); - continue; - } - auto path = m_paths.front(); - m_paths.pop(); - auto fd = fopen(path.c_str(), "rb+"); - if (fd) - { - auto lenght = FileTools::get_file_size(path.c_str()); - std::shared_ptr> buff(new std::vector); - buff->resize(lenght); - fread(buff->data(), lenght, 1, fd); - fclose(fd); - remove(path.c_str()); - imgproce(buff); - } - else { - FileTools::writelog(log_ERROR, "img tmp file read error file name =" + path); - } - - } - }); - } - else + bool procmode = (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131); + if (m_imgprocthread.get() && m_imgprocthread->joinable()) { b_imgprothread = false; + m_imgprocthread->join(); } + m_imgprocthread.reset(new thread(&GScanO1003399::proc, this, procmode)); + //if (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131) { + // b_imgprothread = true; + // m_imgprocthread.enqueue([this] { + // while (b_imgprothread) + // { + // if (m_paths.Size() < 1) + // { + // this_thread::sleep_for(chrono::milliseconds(10)); + // continue; + // } + // auto path = m_paths.Take(); + // if (!path.empty()) + // { + // auto fd = fopen(path.c_str(), "rb+"); + // if (fd) + // { + // auto lenght = FileTools::get_file_size(path.c_str()); + // std::shared_ptr> buff(new std::vector); + // buff->resize(lenght); + // fread(buff->data(), lenght, 1, fd); + // fclose(fd); + // remove(path.c_str()); + // imgproce(buff); + // } + // else { + // FileTools::writelog(log_ERROR, "img tmp file read error file name =" + path); + // } + // } + // } + // }); + //} + //else + //{ + // b_imgprothread = false; + //} } void GScanO1003399::Stop_scan() @@ -460,18 +471,15 @@ void GScanO1003399::usb_run() } memset(buff, 0, sizeof(buff)); auto length = m_usb->read_int(buff, sizeof(buff)); - if (((length) == sizeof(buff)) || im_dev_count()) + if (((length) == sizeof(buff)))//|| im_dev_count() { - if (im_dev_count()) //将取图消息提前防止未接收到img信息后不取图导致程序无法退出 2021.7.16 - if (is_runing()) - im_rx(); HGEIntInfo info = *(HGEIntInfo*)&buff; switch (info.From) { - //case IMG: - // if (is_runing()) - // im_rx(); - // break; + case IMG: + if (is_runing()) + im_rx(); + break; case MtBoard: FileTools::writelog(log_ERROR, "Got MotorBoard error code = " + to_string(info.Code)); break; @@ -499,7 +507,8 @@ void GScanO1003399::usb_run() if ((get_ErrorCode() != PAPER_JAM) && (get_ErrorCode() != DETECT_DOUBLE_FEED) && (get_ErrorCode() != DETECT_STAPLE)) { keeplastimg = false; - im_rx(); + while (im_dev_count()) + im_rx(); scanflag = false; } else { @@ -508,7 +517,8 @@ void GScanO1003399::usb_run() if (scanner_read_reg(m_usb, SR_GET_KEEP_LAST_PAPER)) { this_thread::sleep_for(chrono::milliseconds(100)); - im_rx(); + while(im_dev_count()>( m_param.is_duplex?2:1)) + im_rx(); if (m_param.is_duplex) pop_dev_im(); pop_dev_im(); @@ -516,7 +526,8 @@ void GScanO1003399::usb_run() else { keeplastimg = false; - im_rx(); + while (im_dev_count()) + im_rx(); } scanflag = false; } @@ -595,37 +606,29 @@ void GScanO1003399::im_rx() { //m_imgthread.enqueue([this] { - StopWatch sw; - while (im_dev_count()){ + //StopWatch sw; + //while (im_dev_count()){ if (is_dev_tx()) - { - this_thread::sleep_for(chrono::microseconds(20)); - continue; - } + return; if (keeplastimg) { - if(sw.elapsed_ms()>300) - break; - this_thread::sleep_for(chrono::microseconds(10)); if (m_param.is_duplex) { if(im_dev_count()<3) - continue; + return; } if(im_dev_count()<2) - continue; + return; } - std::shared_ptr> buffi(new std::vector); + //std::shared_ptr> buffi(new std::vector); + auto& buffi = im_data; int count = front_datasize(); buffi->resize(count); rx_cmd(); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); count = read_data(buffi->data(), count, count / (0.005 * 1024 * 1024)); // pop_dev_im(); - vector mats; - if (m_param.multi_output_red) - image += 2; - else - image++; + //vector mats; + m_param.multi_output_red? imagecount += 2: imagecount++; UpdateScanInfo(countNReaded(), get_imgTransfered()); if (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131) { @@ -635,7 +638,7 @@ void GScanO1003399::im_rx() { fwrite(buffi->data(), buffi->size(), 1, fd); fclose(fd); - m_paths.push(path); + m_paths.Put(path); } else { FileTools::writelog(log_ERROR, "img temp file save error flie name =" + path); @@ -643,17 +646,17 @@ void GScanO1003399::im_rx() } else { imgs.Put(buffi); - fu_imgpro.push(m_imgprocthread.enqueue([this] { - imgproce(imgs.Take()); - })); - while (fu_imgpro.size()>1) - { - fu_imgpro.front().get(); - if(fu_imgpro.size()>0) - fu_imgpro.pop(); - } + //fu_imgpro.push(m_imgprocthread.enqueue([this] { + // imgproce(imgs.Take()); + //})); + //while (fu_imgpro.size()>1) + //{ + // fu_imgpro.front().get(); + // if(fu_imgpro.size()>0) + // fu_imgpro.pop(); + //} } - } + //} //}); } @@ -664,6 +667,7 @@ int GScanO1003399::read_data(void* data, int length, int timeout) FileTools::writelog(log_INFO, "if (!m_usb.get() && !m_usb->is_connected())"); return 0; } + timeout = std::max(500, timeout); int readed = 0; int reading = 0; @@ -865,3 +869,50 @@ int GScanO1003399::codeconvter(HGEIntInfo code) return -1; return 0; } + + + +void GScanO1003399::proc(bool bcachemode) +{ + b_imgprothread = true; + while (b_imgprothread) + { + std::shared_ptr> buffer; + if (bcachemode) { + if (m_paths.Size() < 1) + { + this_thread::sleep_for(chrono::milliseconds(10)); + continue; + } + auto path = m_paths.Take(); + if (!path.empty()) + { + auto fd = fopen(path.c_str(), "rb+"); + if (fd) + { + auto lenght = FileTools::get_file_size(path.c_str()); + buffer.reset(new std::vector); + buffer->resize(lenght); + fread(buffer->data(), lenght, 1, fd); + fclose(fd); + remove(path.c_str()); + } + else { + FileTools::writelog(log_ERROR, "img tmp file read error file name =" + path); + } + } + + } + else + { + if (imgs.Size() < 1) + { + std::this_thread::sleep_for(chrono::milliseconds(2)); + continue; + } + buffer = imgs.Take(); + } + + imgproce(buffer); + } +} diff --git a/huagao/Device/GScanO1003399.h b/huagao/Device/GScanO1003399.h index 323a49ac..62ef67c4 100644 --- a/huagao/Device/GScanO1003399.h +++ b/huagao/Device/GScanO1003399.h @@ -69,7 +69,7 @@ private: int codeconvter(HGEIntInfo code); static void usbhotplug_callback(bool isconnect, void* userdata); void usbhotplug(bool isleft); - + void proc(bool bcachemode); private: volatile int devState; volatile int Error_Code; @@ -80,16 +80,14 @@ private: volatile bool b_imgprothread; volatile bool b_usbthread; BlockingQueue>> imgs; - //std::future fu_rx; - std::queue> fu_imgpro; - ThreadPool m_imgthread; - ThreadPool m_imgprocthread; + BlockingQueue m_paths; + + //std::queue> fu_imgpro; + std::shared_ptr m_imgprocthread; std::shared_ptr m_usbthread; std::shared_ptr> im_data; - - std::queue m_paths; BlockingQueue>> m_imagedata; - std::atomic_int image; + std::atomic_int imagecount; void* huagods; deviceevent_callback dev_callback; };