From d0093d6199d179b9493cda1c8e6b03b1523445b6 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Thu, 7 Dec 2023 16:56:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E5=83=8F=E5=A4=84=E7=90=86=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=97=B6=EF=BC=8C=E4=BF=9D=E5=AD=98=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=9B=BE=E7=89=87=EF=BC=9B=E5=85=B3=E9=97=AD=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=EF=BC=8C=E8=B0=83=E7=94=A8=E4=B8=80=E6=AC=A1?= =?UTF-8?q?stop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hgdriver/hgdev/hg_scanner.cpp | 76 ++++++++++++++++++------------ hgdriver/hgdev/hg_scanner.h | 3 +- hgdriver/hgdev/scanner_manager.cpp | 1 + 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/hgdriver/hgdev/hg_scanner.cpp b/hgdriver/hgdev/hg_scanner.cpp index 38f41c9..be193f6 100644 --- a/hgdriver/hgdev/hg_scanner.cpp +++ b/hgdriver/hgdev/hg_scanner.cpp @@ -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,11 +5178,41 @@ int hg_scanner::get_scan_is_sleep(SANE_Bool& data) { return SCANNER_ERR_DEVICE_NOT_SUPPORT; } -void hg_scanner::image_process(std::shared_ptr& buffer, uint32_t id) +void hg_scanner::save_exception_image(std::shared_ptr& buffer, int sn, const char* desc) +{ + char name[128] = { 0 }; + FILE* dst = nullptr; + + sprintf(name, "%04d-%s.jpg", sn, desc); + dst = fopen((final_path_ + "failedimgs" + PATH_SEPARATOR + name).c_str(), "wb"); + if (dst) + { + unsigned int off = 0, + len = buffer->size(), + size = len; + unsigned char* mem = buffer->data(off, &size); + while (mem) + { + fwrite(mem, 1, size, dst); + off += size; + if (off >= len) + break; + else + { + size = len - off; + mem = buffer->data(off, &size); + } + } + fclose(dst); + } +} +bool hg_scanner::image_process(std::shared_ptr& buffer, uint32_t id) { if (!buffer.get() || !ImagePrc_pHandle_) { - return; + 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_); @@ -5195,34 +5229,10 @@ void hg_scanner::image_process(std::shared_ptr& buffer, uint32_t id (this->*dump_img_)(ImagePrc_pHandle_, "decode"); if (err != SCANNER_ERR_OK) { - char name[128] = { 0 }; - FILE *dst = nullptr; - - sprintf(name, "%04d-decode.jpg", id); - dst = fopen((fimg + name).c_str(), "wb"); - if (dst) - { - unsigned int off = 0, - len = buffer->size(), - size = len; - unsigned char *mem = buffer->data(off, &size); - while (mem) - { - fwrite(mem, 1, size, dst); - off += size; - if (off >= len) - break; - else - { - size = len - off; - mem = buffer->data(off, &size); - } - } - fclose(dst); - } + 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& 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& buffer, uint32_t id save_final_image(&ih, buf, id); id = -1; } + + return true; } int hg_scanner::image_configuration(SCANCONF& ic) { diff --git a/hgdriver/hgdev/hg_scanner.h b/hgdriver/hgdev/hg_scanner.h index eb514a4..a73a77f 100644 --- a/hgdriver/hgdev/hg_scanner.h +++ b/hgdriver/hgdev/hg_scanner.h @@ -130,7 +130,8 @@ class hg_scanner void working_begin(void*); void working_done(void*); - void image_process(std::shared_ptr& buffer, uint32_t id); + void save_exception_image(std::shared_ptr& buffer, int sn, const char* desc); + bool image_process(std::shared_ptr& 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); diff --git a/hgdriver/hgdev/scanner_manager.cpp b/hgdriver/hgdev/scanner_manager.cpp index 3fad6fb..af8a320 100644 --- a/hgdriver/hgdev/scanner_manager.cpp +++ b/hgdriver/hgdev/scanner_manager.cpp @@ -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);