简化取图流程

This commit is contained in:
masayume_ht 2021-08-08 13:30:00 +08:00
parent e00095d470
commit c2d230d199
2 changed files with 143 additions and 94 deletions

View File

@ -35,9 +35,7 @@ static void scanner_cmd(std::shared_ptr<IUsb>& usb, int cmd)
GScanO1003399::GScanO1003399():
m_imgthread(1)
,m_imgprocthread(1)
GScanO1003399::GScanO1003399()
{
im_data.reset(new std::vector<char>());
//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<void(int)> fun)
{
}
@ -121,7 +125,7 @@ int GScanO1003399::aquire_bmpdata(std::vector<unsigned char>& 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<unsigned char>& 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<std::vector<char>> buff(new std::vector<char>);
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<std::vector<char>> buff(new std::vector<char>);
// 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,6 +507,7 @@ void GScanO1003399::usb_run()
if ((get_ErrorCode() != PAPER_JAM) && (get_ErrorCode() != DETECT_DOUBLE_FEED) && (get_ErrorCode() != DETECT_STAPLE))
{
keeplastimg = false;
while (im_dev_count())
im_rx();
scanflag = false;
}
@ -508,6 +517,7 @@ void GScanO1003399::usb_run()
if (scanner_read_reg(m_usb, SR_GET_KEEP_LAST_PAPER))
{
this_thread::sleep_for(chrono::milliseconds(100));
while(im_dev_count()>( m_param.is_duplex?2:1))
im_rx();
if (m_param.is_duplex)
pop_dev_im();
@ -516,6 +526,7 @@ void GScanO1003399::usb_run()
else
{
keeplastimg = false;
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<std::vector<char>> buffi(new std::vector<char>);
//std::shared_ptr<std::vector<char>> buffi(new std::vector<char>);
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<cv::Mat> mats;
if (m_param.multi_output_red)
image += 2;
else
image++;
//vector<cv::Mat> 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<std::vector<char>> 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<char>);
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);
}
}

View File

@ -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<std::shared_ptr<std::vector<char>>> imgs;
//std::future<void> fu_rx;
std::queue<std::future<void>> fu_imgpro;
ThreadPool m_imgthread;
ThreadPool m_imgprocthread;
BlockingQueue<std::string> m_paths;
//std::queue<std::future<void>> fu_imgpro;
std::shared_ptr <std::thread> m_imgprocthread;
std::shared_ptr<std::thread> m_usbthread;
std::shared_ptr<std::vector<char>> im_data;
std::queue<std::string> m_paths;
BlockingQueue<std::shared_ptr<std::vector<unsigned char>>> m_imagedata;
std::atomic_int image;
std::atomic_int imagecount;
void* huagods;
deviceevent_callback dev_callback;
};