#include "ManagerV2.h" #include "base/HGBuffer.h" #include "base/HGBase64.h" #include "base/HGUtility.h" #include "base/HGInfo.h" #include "imgfmt/HGJpeg.h" #include "imgfmt/HGOfd.h" #include "imgfmt/HGPdf.h" #include "imgfmt/HGTiff.h" #include "imgfmt/HGImgFmt.h" #include "HGString.h" namespace ver_2 { ManagerV2::ManagerV2(HGMsgPump msgPump) : Manager(msgPump) { } ManagerV2::~ManagerV2() { } void ManagerV2::ScanFinish(const ScanFinishParam* param) { assert(NULL != param && this == param->mgr); } int ManagerV2::SetGlobalConfig(const GlobalConfig& cfg) { if ("date_time" != cfg.fileNameMode && "random" != cfg.fileNameMode) return -1; if ("jpg" != cfg.imageFormat && "bmp" != cfg.imageFormat && "png" != cfg.imageFormat && "tif" != cfg.imageFormat && "pdf" != cfg.imageFormat && "ofd" != cfg.imageFormat && "ocr-pdf" != cfg.imageFormat && "ocr-ofd" != cfg.imageFormat) return -1; if (cfg.imageJpegQuality < 0 || cfg.imageJpegQuality > 100) return -1; if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression && "jpeg" != cfg.imageTiffCompression) return -1; if (cfg.imageTiffJpegQuality < 0 || cfg.imageTiffJpegQuality > 100) return -1; SetCfgStringValue("global", "fileSavePath", Utf8ToStdString(cfg.fileSavePath)); SetCfgStringValue("global", "fileNamePrefix", Utf8ToStdString(cfg.fileNamePrefix)); SetCfgStringValue("global", "fileNameMode", cfg.fileNameMode); SetCfgStringValue("global", "imageFormat", cfg.imageFormat); SetCfgIntValue("global", "imageJpegQuality", cfg.imageJpegQuality); SetCfgStringValue("global", "imageTiffCompression", cfg.imageTiffCompression); SetCfgIntValue("global", "imageTiffJpegQuality", cfg.imageTiffJpegQuality); SetCfgStringValue("global", "uploadHttpHost", cfg.uploadHttpHost); SetCfgIntValue("global", "uploadHttpPort", cfg.uploadHttpPort); SetCfgStringValue("global", "uploadHttpPath", cfg.uploadHttpPath); SetCfgStringValue("global", "uploadFtpUser", cfg.uploadFtpUser); SetCfgStringValue("global", "uploadFtpPassword", cfg.uploadFtpPassword); SetCfgStringValue("global", "uploadFtpHost", cfg.uploadFtpHost); SetCfgIntValue("global", "uploadFtpPort", cfg.uploadFtpPort); return 0; } int ManagerV2::GetGlobalConfig(GlobalConfig& cfg) { HGChar docPath[256]; HGBase_GetDocumentsPath(docPath, 256); cfg.fileSavePath = StdStringToUtf8(GetCfgStringValue("global", "fileSavePath", docPath)); cfg.fileNamePrefix = StdStringToUtf8(GetCfgStringValue("global", "fileNamePrefix", "Huago")); cfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time"); cfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); cfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80); cfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw"); cfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80); cfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", ""); cfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80); cfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi"); cfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", ""); cfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", ""); cfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", ""); cfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21); return 0; } int ManagerV2::LoadLocalImage(const std::string& imagePath, std::string& imgBase64) { imgBase64.clear(); HGUInt imgType = 0; HGImgFmt_GetImgFmtType(Utf8ToStdString(imagePath).c_str(), &imgType); if (0 == imgType) return -1; int ret = LoadBase64(Utf8ToStdString(imagePath), imgBase64); if (0 != ret) return ret; std::string prefix = "data:image/jpeg;base64,"; if (HGIMGFMT_TYPE_BMP == imgType) prefix = "data:image/bmp;base64,"; else if (HGIMGFMT_TYPE_PNG == imgType) prefix = "data:image/png;base64,"; else if (HGIMGFMT_TYPE_TIFF == imgType) prefix = "data:image/tiff;base64,"; else if (HGIMGFMT_TYPE_PDF == imgType) prefix = "data:image/pdf;base64,"; else if (HGIMGFMT_TYPE_OFD == imgType) prefix = "data:image/ofd;base64,"; imgBase64.insert(0, prefix); return 0; } int ManagerV2::SaveLocalImage(const std::string& imgBase64, std::string& imagePath) { imagePath.clear(); size_t pos = imgBase64.find(','); if (std::string::npos == pos) return -1; std::string prefix = imgBase64.substr(0, pos + 1); std::string suffix = "jpg"; if ("data:image/bmp;base64," == prefix) suffix = "bmp"; else if ("data:image/png;base64," == prefix) suffix = "png"; else if ("data:image/tiff;base64," == prefix) suffix = "tif"; else if ("data:image/pdf;base64," == prefix) suffix = "pdf"; else if ("data:image/ofd;base64," == prefix) suffix = "ofd"; std::string imagePath2 = GetFilePath(suffix); int ret = SaveBase64(imgBase64.c_str() + pos + 1, imagePath2); if (0 != ret) return ret; imagePath = StdStringToUtf8(imagePath2); return 0; } int ManagerV2::DeleteLocalFile(const std::string& filePath) { return 0; } int ManagerV2::ClearGlobalFileSavePath() { return 0; } int ManagerV2::MergeLocalImage(const std::vector& imagePathList, const std::string& mode, const std::string& align, int interval, std::string& outImagePath) { return 0; } int ManagerV2::LocalMakeMultiImage(const std::vector& imagePathList, const std::string& format, const std::string& tiffCompression, int tiffJpegQuality, std::string& outImagePath) { return 0; } int ManagerV2::SplitLocalImage(const std::string& imagePath, const std::string& mode, int location, std::vector& outImagePathList) { return 0; } int ManagerV2::LocalMakeZipFile(const std::vector& filePathList, std::string& outZipPath) { return 0; } int ManagerV2::LocalImageDeskew(const std::string& imagePath, std::string& outImagePath) { return 0; } int ManagerV2::UploadLocalFile(const std::string& filePath, const std::string& mode, const std::string& remoteFilePath) { return 0; } std::string ManagerV2::GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); strcat(cfgPath, "config2.ini"); HGChar val[256] = { 0 }; HGBase_GetProfileString(cfgPath, app.c_str(), key.c_str(), def.c_str(), val, 256); return val; } int ManagerV2::GetCfgIntValue(const std::string& app, const std::string& key, int def) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); strcat(cfgPath, "config2.ini"); HGInt val = 0; HGBase_GetProfileInt(cfgPath, app.c_str(), key.c_str(), def, &val); return val; } bool ManagerV2::GetCfgBoolValue(const std::string& app, const std::string& key, bool def) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); strcat(cfgPath, "config2.ini"); HGInt val = 0; HGBase_GetProfileInt(cfgPath, app.c_str(), key.c_str(), (HGInt)def, &val); return (bool)val; } bool ManagerV2::SetCfgStringValue(const std::string& app, const std::string& key, const std::string& val) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGBase_CreateDir(cfgPath); strcat(cfgPath, "config2.ini"); return (HGBASE_ERR_OK == HGBase_SetProfileString(cfgPath, app.c_str(), key.c_str(), val.c_str())); } bool ManagerV2::SetCfgIntValue(const std::string& app, const std::string& key, int val) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGBase_CreateDir(cfgPath); strcat(cfgPath, "config2.ini"); return (HGBASE_ERR_OK == HGBase_SetProfileInt(cfgPath, app.c_str(), key.c_str(), val)); } bool ManagerV2::SetCfgBoolValue(const std::string& app, const std::string& key, bool val) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGBase_CreateDir(cfgPath); strcat(cfgPath, "config2.ini"); return (HGBASE_ERR_OK == HGBase_SetProfileInt(cfgPath, app.c_str(), key.c_str(), (HGInt)val)); } int ManagerV2::LoadBase64(const std::string& fileName, std::string& base64) { base64.clear(); int ret = -1; FILE* file = fopen(fileName.c_str(), "rb"); if (NULL != file) { fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); if (size > 0) { HGByte* data = new HGByte[size]; long readLen = (long)fread(data, 1, size, file); if (readLen == size) { HGSize base64Size = 0; HGBase_Base64Encode(data, size, NULL, &base64Size); uint8_t* base64Data = new uint8_t[base64Size + 1]; HGBase_Base64Encode(data, size, base64Data, &base64Size); base64Data[base64Size] = 0; base64 = (const char*)base64Data; delete[] base64Data; ret = 0; } delete[] data; } fclose(file); } return ret; } int ManagerV2::SaveBase64(const std::string& base64, const std::string& fileName) { if (base64.empty()) return -1; const char* base64Data = base64.c_str(); size_t base64Size = base64.size(); int ret = -1; FILE* file = fopen(fileName.c_str(), "wb"); if (NULL != file) { HGSize size = 0; HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &size); uint8_t* data = new HGByte[size]; HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, data, &size); size_t writeLen = fwrite(data, 1, size, file); if (writeLen == (size_t)size) ret = 0; delete[] data; fclose(file); } return ret; } std::string ManagerV2::GetFilePath(const std::string& suffix) { HGChar docPath[256]; HGBase_GetDocumentsPath(docPath, 256); std::string fileSavePath = GetCfgStringValue("global", "fileSavePath", docPath); std::string fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago"); std::string fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time"); char filePath[256] = { 0 }; if ("random" == fileNameMode) { HGChar uuid[256]; HGBase_GetUuid(uuid, 256); sprintf(filePath, "%s%s%s.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), uuid, suffix.c_str()); } else { timeb tb; ftime(&tb); struct tm* p = localtime(&tb.time); sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, suffix.c_str()); } return filePath; } std::string ManagerV2::GetImagePath() { std::string imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); return GetFilePath(imageFormat); } }