add temporary path for saving received images from USB in windows

This commit is contained in:
gb 2022-05-10 08:27:31 +08:00
parent 8c94b2878d
commit eda3ac4907
2 changed files with 36 additions and 13 deletions

View File

@ -4,6 +4,7 @@
#ifdef WIN32 #ifdef WIN32
#include "scanner_manager.h" #include "scanner_manager.h"
#include <direct.h>
#endif #endif
@ -164,30 +165,49 @@ std::string hg_scanner::error_description(hg_err err)
} }
std::string hg_scanner::temporary_file(char* tail, char* head) std::string hg_scanner::temporary_file(char* tail, char* head)
{ {
char buf[128]; std::string path("/tmp/");
FILE* src = NULL; char buf[128];
FILE* src = NULL;
unsigned int ind = 1;
if (!head || *head == 0) if (!head || *head == 0)
head = (char*)"scan"; head = (char*)"scan";
if (!tail) if (!tail)
tail = (char*)""; 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)); srand(time(NULL));
sprintf(buf, "/tmp/%s_%04x%s", head, rand(), tail); sprintf(buf, "%s%s", head, tail);
while ((src = fopen(buf, "rb"))) while ((src = fopen((path + buf).c_str(), "rb")))
{ {
fclose(src); 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<std::vector<char>> data, std::string* path_file) int hg_scanner::save_2_tempory_file(std::shared_ptr<std::vector<char>> data, std::string* path_file, unsigned int index)
{ {
std::string file(hg_scanner::temporary_file((char*)".jpg")); char head[40] = { 0 };
FILE* dst = fopen(file.c_str(), "wb+"); std::string file("");
FILE* dst = nullptr;
int ret = HG_ERR_OK; 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) if (dst)
{ {
ENOSPC; EROFS; strerror(errno); ENOSPC; EROFS; strerror(errno);
@ -356,6 +376,7 @@ void hg_scanner::thread_handle_usb(void)
} }
scan_life_ = new do_when_born_and_dead<hg_scanner>(this, &hg_scanner::working_begin, &hg_scanner::working_done, NULL); scan_life_ = new do_when_born_and_dead<hg_scanner>(this, &hg_scanner::working_begin, &hg_scanner::working_done, NULL);
usb_img_index_ = 0;
thread_handle_usb_read(); thread_handle_usb_read();
if (scan_life_->release() == 0) if (scan_life_->release() == 0)
scan_life_ = NULL; scan_life_ = NULL;
@ -1741,12 +1762,13 @@ int hg_scanner::save_usb_data(std::shared_ptr<std::vector<char>> data)
{ {
int ret = HG_ERR_OK; 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()); HG_VLOG_MINI_1(HG_LOG_LEVEL_DEBUG_INFO, "USB read one picture with %u bytes\n", data->size());
if (is_to_file()) if (is_to_file())
{ {
std::string 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) if (ret == HG_ERR_OK)
paths_.Put(file); paths_.Put(file);
} }
@ -1755,7 +1777,7 @@ int hg_scanner::save_usb_data(std::shared_ptr<std::vector<char>> data)
imgs_.Put(data); imgs_.Put(data);
#ifdef SAVE_TO_FILE #ifdef SAVE_TO_FILE
hg_scanner::save_2_tempory_file(data, NULL); hg_scanner::save_2_tempory_file(data, NULL, usb_img_index_);
#endif #endif
} }
if (wait_img_.is_waiting()) if (wait_img_.is_waiting())

View File

@ -240,6 +240,7 @@ protected:
std::string img_type_; std::string img_type_;
image_data final_imgs_; // JPG ... image_data final_imgs_; // JPG ...
unsigned int usb_img_index_;
unsigned int final_img_index_; unsigned int final_img_index_;
BlockingQueue<std::shared_ptr<std::vector<char>>> imgs_; BlockingQueue<std::shared_ptr<std::vector<char>>> imgs_;
BlockingQueue<std::string> paths_; BlockingQueue<std::string> paths_;
@ -298,7 +299,7 @@ public:
static std::string strerr(hg_err err); static std::string strerr(hg_err err);
static std::string error_description(hg_err err); static std::string error_description(hg_err err);
static std::string temporary_file(char* tail = NULL, char* head = NULL); static std::string temporary_file(char* tail = NULL, char* head = NULL);
static int save_2_tempory_file(std::shared_ptr<std::vector<char>> data, std::string* path_file); static int save_2_tempory_file(std::shared_ptr<std::vector<char>> data, std::string* path_file, unsigned int index = 0);
public: public:
///////////////////////////////新增 20220425////////////////////////// ///////////////////////////////新增 20220425//////////////////////////