图像处理异常时,保存异常图片;关闭设备之前,调用一次stop

This commit is contained in:
gb 2023-12-07 16:56:55 +08:00
parent ed5f19354b
commit d0093d6199
3 changed files with 48 additions and 32 deletions

View File

@ -1100,7 +1100,9 @@ void hg_scanner::thread_handle_image_process(void)
break;
}
}
image_process(tiny_buffer, id);
invoke_stop = !image_process(tiny_buffer, id);
if (invoke_stop)
break;
}
catch (const std::exception& e)
{
@ -1109,13 +1111,14 @@ void hg_scanner::thread_handle_image_process(void)
is_dpi_color_check = false;
stop_fatal_ = SCANNER_ERR_DEVICE_DISTORTION;
}
else if (strstr(e.what(), "Insufficient "))
else if (e.what() && strstr(e.what(), "Insufficient "))
stop_fatal_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
else
stop_fatal_ = SCANNER_ERR_IMAGE_PROC_FATAL;
VLOG_MINI_1(LOG_LEVEL_FATAL, "[thread_handle_image_process]:is opencv Fatal and stop scanner: %s\n", e.what());
do_stop();
invoke_stop = true;
save_exception_image(tiny_buffer, id, "opencv-excep");
break;
}
@ -1125,6 +1128,7 @@ void hg_scanner::thread_handle_image_process(void)
stop_fatal_ = SCANNER_ERR_IMAGE_PROC_FATAL;
do_stop();
invoke_stop = true;
save_exception_image(tiny_buffer, id, "opencv-excep");
break;
}
}
@ -5174,32 +5178,13 @@ int hg_scanner::get_scan_is_sleep(SANE_Bool& data)
{
return SCANNER_ERR_DEVICE_NOT_SUPPORT;
}
void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id)
{
if (!buffer.get() || !ImagePrc_pHandle_)
{
return;
}
int ret = SCANNER_ERR_OK;
hg_imgproc::IMGPRCPARAM param = get_image_process_object(pid_);
hg_imgproc::IMGHEAD ih;
int err = SCANNER_ERR_OK,
index = 0;
std::string fimg(final_path_ + "failedimgs" + PATH_SEPARATOR);
void* buf = NULL;
hg_imgproc::load_buffer(ImagePrc_pHandle_, buffer);
int imgStatus = buffer->get_image_statu();
err = hg_imgproc::decode(ImagePrc_pHandle_, pid_, &img_conf_, &param, correction_image_map_);
(this->*dump_img_)(ImagePrc_pHandle_, "decode");
if (err != SCANNER_ERR_OK)
void hg_scanner::save_exception_image(std::shared_ptr<tiny_buffer>& buffer, int sn, const char* desc)
{
char name[128] = { 0 };
FILE* dst = nullptr;
sprintf(name, "%04d-decode.jpg", id);
dst = fopen((fimg + name).c_str(), "wb");
sprintf(name, "%04d-%s.jpg", sn, desc);
dst = fopen((final_path_ + "failedimgs" + PATH_SEPARATOR + name).c_str(), "wb");
if (dst)
{
unsigned int off = 0,
@ -5220,9 +5205,34 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
}
fclose(dst);
}
}
bool hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id)
{
if (!buffer.get() || !ImagePrc_pHandle_)
{
VLOG_MINI_1(LOG_LEVEL_FATAL, "process image %d fatal: data is empty or image-process object is not initialized, stop scanning ...\n", id);
do_stop();
return false;
}
int ret = SCANNER_ERR_OK;
hg_imgproc::IMGPRCPARAM param = get_image_process_object(pid_);
hg_imgproc::IMGHEAD ih;
int err = SCANNER_ERR_OK,
index = 0;
std::string fimg(final_path_ + "failedimgs" + PATH_SEPARATOR);
void* buf = NULL;
hg_imgproc::load_buffer(ImagePrc_pHandle_, buffer);
int imgStatus = buffer->get_image_statu();
err = hg_imgproc::decode(ImagePrc_pHandle_, pid_, &img_conf_, &param, correction_image_map_);
(this->*dump_img_)(ImagePrc_pHandle_, "decode");
if (err != SCANNER_ERR_OK)
{
save_exception_image(buffer, id, "decode");
VLOG_MINI_2(LOG_LEVEL_FATAL, "Decode image(%d) with bytes %u failed, stop scanning.\n", id, buffer->size());
do_stop();
return;
return false;
}
if (is_dpi_color_check)
@ -5460,7 +5470,9 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
if (bmpdata.empty())
{
status_ = SCANNER_ERR_NO_DATA;
return;
VLOG_MINI_1(LOG_LEVEL_FATAL, "process image %d fatal: image data is empty! stop scanning ...\n", id);
do_stop();
return false;
}
buf = bmpdata.data();
ih.total_bytes = bmpdata.size();
@ -5473,6 +5485,8 @@ void hg_scanner::image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id
save_final_image(&ih, buf, id);
id = -1;
}
return true;
}
int hg_scanner::image_configuration(SCANCONF& ic)
{

View File

@ -130,7 +130,8 @@ class hg_scanner
void working_begin(void*);
void working_done(void*);
void image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id);
void save_exception_image(std::shared_ptr<tiny_buffer>& buffer, int sn, const char* desc);
bool image_process(std::shared_ptr<tiny_buffer>& buffer, uint32_t id); // 返回true - continue; false - exit and invoked do_stop
void reset_custom_area_range(int paper);
float reset_custom_area_jsn_value(const char* name, double& var, float range_l, float range_u, float value_l, float value_u); // return cur value
int set_color_change(void);

View File

@ -839,6 +839,7 @@ scanner_err hg_scanner_mgr::hg_scanner_close(scanner_handle h, bool force)
}
}
SCAN_PTR(h)->stop();
SCAN_PTR(h)->close(force);
delete SCAN_PTR(h);