调整对比时使用的地址对比导致匹配失败 调整加载ocr动态库时会找不到依赖库导致打开失败问题

This commit is contained in:
13038267101 2022-11-16 18:20:10 +08:00
parent 92dc63bbb5
commit 41c73125d0
11 changed files with 166 additions and 73 deletions

View File

@ -611,11 +611,16 @@ int hg_scanner::restore(const char* name)
setting_jsn_.at(name).at("size").get_to(size); setting_jsn_.at(name).at("size").get_to(size);
buf = (char*)malloc(size + 4); buf = (char*)malloc(size + 4);
if (buf)
{
bzero(buf, size + 4); bzero(buf, size + 4);
strcpy(buf, val.c_str()); strcpy(buf, val.c_str());
ret = set_setting(name, buf, val.length()); ret = set_setting(name, buf, val.length());
free(buf); free(buf);
} }
else
return SCANNER_ERR_INSUFFICIENT_MEMORY;
}
else if (val == "int") else if (val == "int")
{ {
int v = 0; int v = 0;

View File

@ -12,11 +12,11 @@
#include "hg_scanner.h" #include "hg_scanner.h"
#include "../wrapper/hg_log.h" #include "../wrapper/hg_log.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_200 hw_scanner_200 //#define hg_scanner_200 hw_scanner_200
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_200 lsc_scanner_200 //#define hg_scanner_200 lsc_scanner_200
#endif //#endif
class hg_scanner_200 : public hg_scanner class hg_scanner_200 : public hg_scanner

View File

@ -12,11 +12,11 @@
#include "hg_scanner.h" #include "hg_scanner.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_239 hw_scanner_239 //#define hg_scanner_239 hw_scanner_239
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_239 lsc_scanner_239 //#define hg_scanner_239 lsc_scanner_239
#endif //#endif
class hg_scanner_239 : public hg_scanner class hg_scanner_239 : public hg_scanner
{ {

View File

@ -13,11 +13,11 @@
#include "../wrapper/hg_log.h" #include "../wrapper/hg_log.h"
#include "PaperSize.h" #include "PaperSize.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_300 hw_scanner_300 //#define hg_scanner_300 hw_scanner_300
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_300 lsc_scanner_300 //#define hg_scanner_300 lsc_scanner_300
#endif //#endif
class hg_scanner_300 : public hg_scanner class hg_scanner_300 : public hg_scanner

View File

@ -12,11 +12,11 @@
#include "hg_scanner.h" #include "hg_scanner.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_302 hw_scanner_302 //#define hg_scanner_302 hw_scanner_302
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_302 lsc_scanner_302 //#define hg_scanner_302 lsc_scanner_302
#endif //#endif
class hg_scanner_302 : public hg_scanner class hg_scanner_302 : public hg_scanner
{ {

View File

@ -112,13 +112,10 @@ void hg_scanner_400::thread_handle_usb_read(void)
if (ret == SCANNER_ERR_DEVICE_STOPPED) if (ret == SCANNER_ERR_DEVICE_STOPPED)
{ {
if (!savestatus_.empty()) if (!savestatus_.empty())
{
status_ = savestatus_[0]; status_ = savestatus_[0];
}
else else
{
status_ = SCANNER_ERR_OK; status_ = SCANNER_ERR_OK;
}
savestatus_.clear(); savestatus_.clear();
break; break;
} }

View File

@ -14,11 +14,11 @@
#include "PaperSize.h" #include "PaperSize.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_400 hw_scanner_400 //#define hg_scanner_400 hw_scanner_400
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_400 lsc_scanner_400 //#define hg_scanner_400 lsc_scanner_400
#endif //#endif
class hg_scanner_400 : public hg_scanner class hg_scanner_400 : public hg_scanner
{ {

View File

@ -12,11 +12,11 @@
#include "hg_scanner.h" #include "hg_scanner.h"
#ifdef OEM_HANWANG //#ifdef OEM_HANWANG
#define hg_scanner_402 hw_scanner_402 //#define hg_scanner_402 hw_scanner_402
#elif defined(OEM_LISICHENG) //#elif defined(OEM_LISICHENG)
#define hg_scanner_402 lsc_scanner_402 //#define hg_scanner_402 lsc_scanner_402
#endif //#endif
class hg_scanner_402 : public hg_scanner class hg_scanner_402 : public hg_scanner
{ {

View File

@ -73,32 +73,32 @@ using namespace std;
//动态打开库的命名 //动态打开库的命名
#ifdef OEM_HANWANG #ifdef OEM_HANWANG
#ifdef WIN32 #ifdef WIN32
#define IMGPRC_LIBNANE L"HwImgProc.dll" #define IMGPRC_LIBNANE "HWImgProc.dll"
#define HGBASE_LIBNAME L"HwBase.dll" #define HGBASE_LIBNAME "HWBase.dll"
#else #else
#define IMGPRC_LIBNANE "libHwImgProc.so" #define IMGPRC_LIBNANE "libHwImgProc.so"
#define HGBASE_LIBNAME "libHwBase.so" #define HGBASE_LIBNAME "libHwBase.so"
#endif #endif
#elif defined(OEM_LISICHENG) #elif defined(OEM_LISICHENG)
#ifdef WIN32 #ifdef WIN32
#define IMGPRC_LIBNANE L"LscImgProc.dll" #define IMGPRC_LIBNANE "LSCImgProc.dll"
#define HGBASE_LIBNAME L"LscBase.dll" #define HGBASE_LIBNAME "LSCBase.dll"
#else #else
#define IMGPRC_LIBNANE "libLscImgProc.so" #define IMGPRC_LIBNANE "libLscImgProc.so"
#define HGBASE_LIBNAME "libLscBase.so" #define HGBASE_LIBNAME "libLscBase.so"
#endif #endif
#elif defined(OEM_CANGTIAN) #elif defined(OEM_CANGTIAN)
#ifdef WIN32 #ifdef WIN32
#define IMGPRC_LIBNANE L"CtsImgProc.dll" #define IMGPRC_LIBNANE "CtsImgProc.dll"
#define HGBASE_LIBNAME L"CtsBase.dll" #define HGBASE_LIBNAME "CtsBase.dll"
#else #else
#define IMGPRC_LIBNANE "libCtsImgProc.so" #define IMGPRC_LIBNANE "libCtsImgProc.so"
#define HGBASE_LIBNAME "libCtsBase.so" #define HGBASE_LIBNAME "libCtsBase.so"
#endif #endif
#else #else
#ifdef WIN32 #ifdef WIN32
#define IMGPRC_LIBNANE L"HGImgProc.dll" #define IMGPRC_LIBNANE "HGImgProc.dll"
#define HGBASE_LIBNAME L"HGBase.dll" #define HGBASE_LIBNAME "HGBase.dll"
#else #else
#define IMGPRC_LIBNANE "libHGImgProc.so" #define IMGPRC_LIBNANE "libHGImgProc.so"
#define HGBASE_LIBNAME "libHGBase.so" #define HGBASE_LIBNAME "libHGBase.so"
@ -108,8 +108,66 @@ using namespace std;
// functional ... // functional ...
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// //
static int num=0; #ifndef WIN32
#define __stdcall
#endif
static int num=0;
#define SAFE_DELETE(p){ \
if (NULL != (p)) {delete []p; \
(p) = NULL; \
}\
}
static int load_dll(const wchar_t* path_dll, HMODULE* dll)
{
HMODULE h = LoadLibraryW(path_dll);
int ret = GetLastError();
wchar_t info[128] = { 0 };
swprintf_s(info, _countof(info) - 1, L" = %d\r\n", ret);
OutputDebugStringW((L"[TWAIN]Load: " + std::wstring(path_dll) + info).c_str());
if (!h && ret == ERROR_MOD_NOT_FOUND)
{
std::wstring dir(path_dll);
size_t pos = dir.rfind(L'\\');
wchar_t path[MAX_PATH] = { 0 };
GetDllDirectoryW(_countof(path) - 1, path);
if (pos != std::wstring::npos)
dir.erase(pos);
OutputDebugStringW((L"[TWAIN]Load: try change directory to " + dir + L"\r\n").c_str());
SetDllDirectoryW(dir.c_str());
h = LoadLibraryW(path_dll);
ret = GetLastError();
swprintf_s(info, _countof(info) - 1, L" = %d\r\n", ret);
OutputDebugStringW((L"[TWAIN]Load: " + std::wstring(path_dll) + info).c_str());
OutputDebugStringW((L"[TWAIN]Load: restore directory to " + std::wstring(path) + L"\r\n").c_str());
SetDllDirectoryW(path);
}
if (dll)
*dll = h;
return ret;
}
static int strToWchar(string str, wchar_t* s)
{
if (str.empty() || !s)
{
return -1;
}
size_t size = str.length();
wchar_t* buffer = new wchar_t[size + 1];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), size, buffer, size * sizeof(wchar_t));
buffer[size] = 0;
wcscpy(s, buffer);
if (!buffer)
return -1;
delete []buffer;
buffer = NULL;
return 0;
}
namespace hg_imgproc namespace hg_imgproc
{ {
typedef union typedef union
@ -160,12 +218,12 @@ namespace hg_imgproc
} }
}; };
typedef unsigned int (*SDKHGImgProc_InitOCR_)(unsigned int ocrtype, void** pstOcrHandle); typedef unsigned int (__stdcall *SDKHGImgProc_InitOCR_)(unsigned int ocrtype, void** pstOcrHandle);
typedef unsigned int (*SDKHGImgProc_FreeImage_)(void* pstOcrHandle); typedef unsigned int (__stdcall *SDKHGImgProc_FreeImage_)(void* pstOcrHandle);
typedef unsigned int (*SDKHGImgProc_GetTextDir_)(void* pstOcrHandle, void* image, unsigned int* pDirect); typedef unsigned int (__stdcall *SDKHGImgProc_GetTextDir_)(void* pstOcrHandle, void* image, unsigned int* pDirect);
typedef unsigned int (*SDKHGBase_CreateImage_)(void* data, void* imageinfo, void* image); typedef unsigned int (__stdcall *SDKHGBase_CreateImage_)(void* data, void* imageinfo, void* image);
typedef unsigned int (*SDKHGBase_FreeImage_)(void* image); typedef unsigned int (__stdcall *SDKHGBase_FreeImage_)(void* image);
class imgproc class imgproc
{ {
std::string my_path_; std::string my_path_;
@ -324,9 +382,10 @@ namespace hg_imgproc
Dynamicopen_HGImageprc_pHandle_ = dlopen(HGImagePrclib_path.c_str(), RTLD_LAZY); Dynamicopen_HGImageprc_pHandle_ = dlopen(HGImagePrclib_path.c_str(), RTLD_LAZY);
Dynamicopen_HGBase_pHandle_ = dlopen(HGBaselib_path.c_str(), RTLD_LAZY); Dynamicopen_HGBase_pHandle_ = dlopen(HGBaselib_path.c_str(), RTLD_LAZY);
if (!Dynamicopen_HGBase_pHandle_ && !Dynamicopen_HGImageprc_pHandle_) if (!Dynamicopen_HGBase_pHandle_ || !Dynamicopen_HGImageprc_pHandle_)
{ {
return SCANNER_ERR_OUT_OF_RANGE; VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "auto txt open dll is error: %s\n", hg_scanner_err_name(SCANNER_ERR_INSUFFICIENT_MEMORY));
return SCANNER_ERR_INSUFFICIENT_MEMORY;
} }
ocrinit_ = (SDKHGImgProc_InitOCR_)dlsym(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_CreateOCRMgr"); ocrinit_ = (SDKHGImgProc_InitOCR_)dlsym(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_CreateOCRMgr");
@ -337,22 +396,50 @@ namespace hg_imgproc
HGBase_FreeImg = (SDKHGBase_FreeImage_)dlsym(Dynamicopen_HGBase_pHandle_,"HGBase_DestroyImage"); HGBase_FreeImg = (SDKHGBase_FreeImage_)dlsym(Dynamicopen_HGBase_pHandle_,"HGBase_DestroyImage");
#else #else
Dynamicopen_HGImageprc_pHandle_ = LoadLibrary(IMGPRC_LIBNANE); #define SCAN "scanner.dll"
Dynamicopen_HGBase_pHandle_ = LoadLibrary(HGBASE_LIBNAME); string scanner_path = hg_log::get_module_full_path(SCAN);
if (Dynamicopen_HGImageprc_pHandle_ && Dynamicopen_HGBase_pHandle_) scanner_path = scanner_path.substr(0, scanner_path.size() - strlen(SCAN));
string HGImagePrclib_path = scanner_path + IMGPRC_LIBNANE;
string HGBaselib_path = scanner_path + HGBASE_LIBNAME;
wchar_t* Prclibbuffer = new wchar_t[HGImagePrclib_path.length() + 1];
wchar_t* Baselibbuffer = new wchar_t[HGBaselib_path.length() + 1];
ret = strToWchar(HGImagePrclib_path, Prclibbuffer);
if (ret == -1 )
return SCANNER_ERR_INSUFFICIENT_MEMORY;
ret = strToWchar(HGBaselib_path, Baselibbuffer);
if (ret == -1)
return SCANNER_ERR_INSUFFICIENT_MEMORY;
load_dll(Baselibbuffer, &Dynamicopen_HGBase_pHandle_); //先加载后释放
load_dll(Prclibbuffer, &Dynamicopen_HGImageprc_pHandle_); //windows下可能会找不到他所在的依赖库导致直接打开动态库的时候找不到模块
SAFE_DELETE(Prclibbuffer);
SAFE_DELETE(Baselibbuffer);
//int l = GetLastError();
if (!Dynamicopen_HGBase_pHandle_ || !Dynamicopen_HGImageprc_pHandle_)
{ {
VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "auto txt open dll is error: %s\n", hg_scanner_err_name(SCANNER_ERR_INSUFFICIENT_MEMORY));
return SCANNER_ERR_INSUFFICIENT_MEMORY;
}
ocrinit_ = (SDKHGImgProc_InitOCR_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_CreateOCRMgr"); ocrinit_ = (SDKHGImgProc_InitOCR_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_CreateOCRMgr");
ocrgetdirectimage_ = (SDKHGImgProc_GetTextDir_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_ImageTextDirectOCR"); ocrgetdirectimage_ = (SDKHGImgProc_GetTextDir_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_ImageTextDirectOCR");
ocrexit_ = (SDKHGImgProc_FreeImage_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_DestroyOCRMgr"); ocrexit_ = (SDKHGImgProc_FreeImage_)GetProcAddress(Dynamicopen_HGImageprc_pHandle_, "HGImgProc_DestroyOCRMgr");
HGBase_CreatImg = (SDKHGBase_CreateImage_)GetProcAddress(Dynamicopen_HGBase_pHandle_, "HGBase_CreateImageWithData"); HGBase_CreatImg = (SDKHGBase_CreateImage_)GetProcAddress(Dynamicopen_HGBase_pHandle_, "HGBase_CreateImageWithData");
HGBase_FreeImg = (SDKHGBase_FreeImage_)GetProcAddress(Dynamicopen_HGBase_pHandle_, "HGBase_DestroyImage"); HGBase_FreeImg = (SDKHGBase_FreeImage_)GetProcAddress(Dynamicopen_HGBase_pHandle_, "HGBase_DestroyImage");
}
#endif #endif
ret = ocrinit_(HGIMGPROC_OCRALGO_TESSERACT, &Auto_Txt_pHanld); ocrinit_(HGIMGPROC_OCRALGO_TESSERACT, &Auto_Txt_pHanld);
return ret; return ret;
} }
int free_auto_txt_hanld() int free_auto_txt_hanld()
{ {
int ret = 0; int ret = 0;
@ -371,7 +458,7 @@ namespace hg_imgproc
#if (!defined WIN32) #if (!defined WIN32)
ret = dlclose(Dynamicopen_HGBase_pHandle_); ret = dlclose(Dynamicopen_HGBase_pHandle_);
#else #else
ret = FreeLibrary(Dynamicopen_HGBase_pHandle_); ret = FreeLibrary(Dynamicopen_HGBase_pHandle_); //貌似加载顺序不对,会导致这个地方的死锁???
#endif #endif
Dynamicopen_HGBase_pHandle_ = NULL; Dynamicopen_HGBase_pHandle_ = NULL;
} }
@ -1203,6 +1290,10 @@ namespace hg_imgproc
} }
int tesseract_auto_txtdirect() int tesseract_auto_txtdirect()
{ {
if (!Auto_Txt_pHanld)
{
return SCANNER_ERR_NO_DATA;
}
std::vector<cv::Mat> mats(mats_); std::vector<cv::Mat> mats(mats_);
mats_.clear(); mats_.clear();

View File

@ -1034,7 +1034,7 @@ std::string hg_sane_middleware::get_option_json(scanner_handle handle, void *opt
return ret; return ret;
} }
SANE_Option_Descriptor* hg_sane_middleware::find_stored_descriptor(const char* name, unsigned long option) SANE_Option_Descriptor* hg_sane_middleware::find_stored_descriptor(const char* name, unsigned long long option)
{ {
if (IS_PTR_NUMBER(option)) if (IS_PTR_NUMBER(option))
@ -1049,14 +1049,14 @@ SANE_Option_Descriptor* hg_sane_middleware::find_stored_descriptor(const char* n
{ {
for (const auto& v : opts_) for (const auto& v : opts_)
{ {
if (v.dev_name == name && v.opt_name.c_str() == (const char*)option) if (v.dev_name == name && v.opt_name == (const char*)option)
return v.desc; return v.desc;
} }
} }
return NULL; return NULL;
} }
SANE_Option_Descriptor* hg_sane_middleware::find_stored_descriptor(SANE_Handle handle, unsigned long option) SANE_Option_Descriptor* hg_sane_middleware::find_stored_descriptor(SANE_Handle handle, unsigned long long option)
{ {
OPENDEV dev; OPENDEV dev;
scanner_handle h = find_openning_device(handle, false, &dev); scanner_handle h = find_openning_device(handle, false, &dev);
@ -1819,7 +1819,7 @@ bool hg_sane_middleware::is_associatived(const SLAVEOP& slave, const char* maste
return result; return result;
} }
bool hg_sane_middleware::set_stored_option_enabled(const char* dev_name, unsigned long option, bool enable, int* size) bool hg_sane_middleware::set_stored_option_enabled(const char* dev_name, unsigned long long option, bool enable, int* size)
{ {
SANE_Option_Descriptor* opt = find_stored_descriptor(dev_name, option); SANE_Option_Descriptor* opt = find_stored_descriptor(dev_name, option);
bool ret = false; bool ret = false;
@ -1868,7 +1868,7 @@ int hg_sane_middleware::something_after_do(scanner_handle h, const char* dev_nam
continue; continue;
slave_options_[slave].enable_now = enable; slave_options_[slave].enable_now = enable;
if (!set_stored_option_enabled(dev_name, (unsigned long)slave_options_[slave].name.c_str(), enable, &bytes)) if (!set_stored_option_enabled(dev_name, (unsigned long long )slave_options_[slave].name.c_str(), enable, &bytes))
continue; continue;
OPTEN* op = get_control_enalbe_data(slave_options_[slave]); OPTEN* op = get_control_enalbe_data(slave_options_[slave]);

View File

@ -78,8 +78,8 @@ class hg_sane_middleware
SANE_Option_Descriptor* from_json(scanner_handle h, const std::string& name, json* jsn, int opt_no); SANE_Option_Descriptor* from_json(scanner_handle h, const std::string& name, json* jsn, int opt_no);
scanner_handle find_openning_device(SANE_Handle h, bool rmv = false, OPENDEV* dev = NULL); scanner_handle find_openning_device(SANE_Handle h, bool rmv = false, OPENDEV* dev = NULL);
std::string get_option_json(scanner_handle handle, void* opt, std::string* key = nullptr); std::string get_option_json(scanner_handle handle, void* opt, std::string* key = nullptr);
SANE_Option_Descriptor* find_stored_descriptor(const char* name, unsigned long option); SANE_Option_Descriptor* find_stored_descriptor(const char* name, unsigned long long option);
SANE_Option_Descriptor* find_stored_descriptor(SANE_Handle handle, unsigned long option); SANE_Option_Descriptor* find_stored_descriptor(SANE_Handle handle, unsigned long long option);
void reload_current_value(scanner_handle handle, std::vector<std::string>* changed = NULL); void reload_current_value(scanner_handle handle, std::vector<std::string>* changed = NULL);
bool get_current_value(scanner_handle handle, int option, void* value, SANE_Value_Type* type = NULL); bool get_current_value(scanner_handle handle, int option, void* value, SANE_Value_Type* type = NULL);
@ -149,7 +149,7 @@ class hg_sane_middleware
} }
}OPTENABLE; }OPTENABLE;
bool is_associatived(const SLAVEOP& slave, const char* master_name); bool is_associatived(const SLAVEOP& slave, const char* master_name);
bool set_stored_option_enabled(const char* dev_name, unsigned long option, bool enable, int* size = NULL); bool set_stored_option_enabled(const char* dev_name, unsigned long long option, bool enable, int* size = NULL);
int something_after_do(scanner_handle h, const char* dev_name, const char* master_name, const char* cur_val); int something_after_do(scanner_handle h, const char* dev_name, const char* master_name, const char* cur_val);
OPTEN* get_control_enalbe_data(const SLAVEOP& slave); OPTEN* get_control_enalbe_data(const SLAVEOP& slave);
void free_control_enable_data(OPTEN* opt); void free_control_enable_data(OPTEN* opt);