From eda3ac49071d96da262bbfbb637fbfd27dec7995 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 10 May 2022 08:27:31 +0800 Subject: [PATCH] add temporary path for saving received images from USB in windows --- hgdriver/hgdev/hg_scanner.cpp | 46 ++++++++++++++++++++++++++--------- hgdriver/hgdev/hg_scanner.h | 3 ++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/hgdriver/hgdev/hg_scanner.cpp b/hgdriver/hgdev/hg_scanner.cpp index 19316de..b660fee 100644 --- a/hgdriver/hgdev/hg_scanner.cpp +++ b/hgdriver/hgdev/hg_scanner.cpp @@ -4,6 +4,7 @@ #ifdef WIN32 #include "scanner_manager.h" +#include #endif @@ -164,30 +165,49 @@ std::string hg_scanner::error_description(hg_err err) } std::string hg_scanner::temporary_file(char* tail, char* head) { - char buf[128]; - FILE* src = NULL; + std::string path("/tmp/"); + char buf[128]; + FILE* src = NULL; + unsigned int ind = 1; if (!head || *head == 0) head = (char*)"scan"; if (!tail) tail = (char*)""; +#ifdef WIN32 + char me[MAX_PATH] = { 0 }, * n = NULL; + + GetModuleFileNameA(NULL, me, _countof(me) - 1); + n = strrchr(me, '\\'); + if (n++ == NULL) + n = me; + *n = 0; + path = me; + path += "img_tmp\\"; + mkdir(path.c_str()); +#endif + srand(time(NULL)); - sprintf(buf, "/tmp/%s_%04x%s", head, rand(), tail); - while ((src = fopen(buf, "rb"))) + sprintf(buf, "%s%s", head, tail); + while ((src = fopen((path + buf).c_str(), "rb"))) { fclose(src); - sprintf(buf, "/tmp/%s_%04x%s", head, rand(), tail); + sprintf(buf, "%s(%u)%s", head, ind++, tail); } - return buf; + return path + buf; } -int hg_scanner::save_2_tempory_file(std::shared_ptr> data, std::string* path_file) +int hg_scanner::save_2_tempory_file(std::shared_ptr> data, std::string* path_file, unsigned int index) { - std::string file(hg_scanner::temporary_file((char*)".jpg")); - FILE* dst = fopen(file.c_str(), "wb+"); + char head[40] = { 0 }; + std::string file(""); + FILE* dst = nullptr; int ret = HG_ERR_OK; + sprintf(head, "scan_%06u", index); + file = hg_scanner::temporary_file((char*)".jpg", (char*)head); + dst = fopen(file.c_str(), "wb+"); if (dst) { ENOSPC; EROFS; strerror(errno); @@ -356,6 +376,7 @@ void hg_scanner::thread_handle_usb(void) } scan_life_ = new do_when_born_and_dead(this, &hg_scanner::working_begin, &hg_scanner::working_done, NULL); + usb_img_index_ = 0; thread_handle_usb_read(); if (scan_life_->release() == 0) scan_life_ = NULL; @@ -1539,7 +1560,7 @@ int hg_scanner::setting_img_quality(void *data) , (char*)data, hg_scanner::strerr((hg_err)ret).c_str()); is_quality_ = match_best_img_quality(str,NULL); - + return HG_ERR_OK; } @@ -1741,12 +1762,13 @@ int hg_scanner::save_usb_data(std::shared_ptr> data) { int ret = HG_ERR_OK; + usb_img_index_++; HG_VLOG_MINI_1(HG_LOG_LEVEL_DEBUG_INFO, "USB read one picture with %u bytes\n", data->size()); if (is_to_file()) { std::string file(""); - ret = hg_scanner::save_2_tempory_file(data, &file); + ret = hg_scanner::save_2_tempory_file(data, &file, usb_img_index_); if (ret == HG_ERR_OK) paths_.Put(file); } @@ -1755,7 +1777,7 @@ int hg_scanner::save_usb_data(std::shared_ptr> data) imgs_.Put(data); #ifdef SAVE_TO_FILE - hg_scanner::save_2_tempory_file(data, NULL); + hg_scanner::save_2_tempory_file(data, NULL, usb_img_index_); #endif } if (wait_img_.is_waiting()) diff --git a/hgdriver/hgdev/hg_scanner.h b/hgdriver/hgdev/hg_scanner.h index e297c41..3206f9d 100644 --- a/hgdriver/hgdev/hg_scanner.h +++ b/hgdriver/hgdev/hg_scanner.h @@ -240,6 +240,7 @@ protected: std::string img_type_; image_data final_imgs_; // JPG ... + unsigned int usb_img_index_; unsigned int final_img_index_; BlockingQueue>> imgs_; BlockingQueue paths_; @@ -298,7 +299,7 @@ public: static std::string strerr(hg_err err); static std::string error_description(hg_err err); static std::string temporary_file(char* tail = NULL, char* head = NULL); - static int save_2_tempory_file(std::shared_ptr> data, std::string* path_file); + static int save_2_tempory_file(std::shared_ptr> data, std::string* path_file, unsigned int index = 0); public: ///////////////////////////////新增 20220425//////////////////////////