#include "Manager.h" #include "MsgLoop.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 "../../utility/HGString.h" extern "C" { #include "zip.h" }; #include #include DevParam::DevParam() { Reset(); } DevParam::~DevParam() { } void DevParam::Reset() { autofeeder = true; pixel = 1; white = false; discardBlankThre = 5; single = false; format = "jpg"; resolution = 200; orentation = 0; paperType = "Auto"; splitImage = 0; noiseDetachEnable = true; noiseDetach = 15; uploadMode = 2; ftpPort = 21; ftpMode = 2; } void DevParam::Load(const std::string& cfgPath) { HGInt intValue; HGChar strValue[256]; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "device", "", strValue, 256); device = strValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", 1, &intValue); autofeeder = (bool)intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "pixel", 1, &intValue); pixel = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "white", 0, &intValue); white = (bool)intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", 5, &intValue); discardBlankThre = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "single", 0, &intValue); single = (bool)intValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "format", "jpg", strValue, 256); format = strValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "resolution", 200, &intValue); resolution = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "orentation", 0, &intValue); orentation = intValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "paperType", "Auto", strValue, 256); paperType = strValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "splitImage", 0, &intValue); splitImage = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable”", 1, &intValue); noiseDetachEnable = (bool)intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", 15, &intValue); noiseDetach = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", 2, &intValue); uploadMode = intValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpUrl", "", strValue, 256); httpUrl = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "fileName", "", strValue, 256); fileName = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpMethod", "", strValue, 256); httpMethod = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "header", "", strValue, 256); header = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "param", "", strValue, 256); param = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", "", strValue, 256); ftpUrl = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPath", "", strValue, 256); ftpPath = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUser", "", strValue, 256); ftpUser = strValue; HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", "", strValue, 256); ftpPassword = strValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", 21, &intValue); ftpPort = intValue; HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", 2, &intValue); ftpMode = intValue; } void DevParam::Save(const std::string& cfgPath) { HGBase_SetProfileString(cfgPath.c_str(), "devParam", "device", device.c_str()); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", autofeeder); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "pixel", pixel); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "white", white); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", discardBlankThre); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "single", single); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "format", format.c_str()); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "resolution", resolution); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "orentation", orentation); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "paperType", paperType.c_str()); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "splitImage", splitImage); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable", noiseDetachEnable); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", noiseDetach); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", uploadMode); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpUrl", httpUrl.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "fileName", fileName.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpMethod", httpMethod.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "header", header.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "param", param.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", ftpUrl.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPath", ftpPath.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUser", ftpUser.c_str()); HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", ftpPassword.c_str()); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", ftpPort); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", ftpMode); } Manager::Manager(class MsgLoop* loop) { m_loop = loop; HGBase_CreateLock(&m_lock); m_devName.clear(); m_devHandle = NULL; m_scanEvent = NULL; m_scanParam = NULL; m_scanInsertImgName.clear(); m_scanIsInsert = false; m_scanning = false; SANE_Int version_code = 0; SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this); assert(SANE_STATUS_GOOD == status); } Manager::~Manager() { if (NULL != m_devHandle) { StopScan(); m_devParam.Reset(); sane_close(m_devHandle); m_devHandle = NULL; m_devName.clear(); } sane_exit(); HGBase_DestroyLock(m_lock); m_lock = NULL; } void Manager::OpenDev(const OpenDevParam* param) { if (!m_devName.empty() || NULL != m_devHandle) { return; } SANE_Handle devHandle = NULL; SANE_Status status = sane_open(param->devName.c_str(), &devHandle); if (SANE_STATUS_GOOD == status) { assert(NULL != devHandle); m_devName = param->devName; m_devHandle = devHandle; std::string filePath = GetFilePath(m_devName); std::string cfgPath = filePath + "config.ini"; m_devParam.Load(cfgPath); SetDevParam(m_devName, m_devParam); } } void Manager::CloseDev(const CloseDevParam* param) { if (m_devName.empty() || NULL == m_devHandle || m_devName != param->devName) { return; } assert(NULL != m_devHandle); StopScan(); m_devParam.Reset(); sane_close(m_devHandle); m_devHandle = NULL; m_devName.clear(); } void Manager::SetScanEvent(ScanEvent event, void* param) { assert(NULL != event && NULL != param); HGBase_EnterLock(m_lock); m_scanEvent = event; m_scanParam = param; HGBase_LeaveLock(m_lock); } void Manager::ResetScanEvent() { HGBase_EnterLock(m_lock); m_scanEvent = NULL; m_scanParam = NULL; HGBase_LeaveLock(m_lock); } bool Manager::Scan(const std::string& insertImgName, bool isInsert) { if (m_scanning) { return false; } bool ret = false; if (NULL != m_devHandle) { m_scanInsertImgName = insertImgName; m_scanIsInsert = isInsert; if (SANE_STATUS_GOOD == sane_start(m_devHandle)) { m_scanning = true; ret = true; } else { m_scanInsertImgName.clear(); m_scanIsInsert = false; } } return ret; } bool Manager::StopScan() { if (m_scanning) { assert(NULL != m_devHandle); sane_cancel(m_devHandle); m_scanning = false; m_scanInsertImgName.clear(); m_scanIsInsert = false; } return true; } bool Manager::GetCurDevId(std::string& devId) { devId.clear(); if (m_scanning) { return false; } devId = m_devName; return true; } bool Manager::GetDevNames(std::vector& devNameList) { devNameList.clear(); if (m_scanning) { return false; } const SANE_Device** device_list; if (SANE_STATUS_GOOD == sane_get_devices(&device_list, SANE_TRUE)) { const SANE_Device** p; for (p = device_list; *p != NULL; ++p) { devNameList.push_back((*p)->name); } } return true; } bool Manager::GetImageByDevId(const std::string& devId, std::vector& imgNameList, std::vector& imgBase64List) { imgNameList.clear(); imgBase64List.clear(); if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); HGBase_WriteInfo(HGBASE_INFOTYPE_DEBUG, "filePath=%s", filePath.c_str()); for (int i = 0; i < (int)fileNameList.size(); ++i) { std::string fileName = filePath + fileNameList[i]; HGImage img = NULL; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { std::string imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(img, 0); imgNameList.push_back(fileNameList[i]); imgBase64List.push_back(imgBase64); HGBase_DestroyImage(img); } } SaveFileNameList(devId, imgNameList); return true; } bool Manager::GetDevParam(const std::string& devId, DevParam& devParam) { devParam.Reset(); if (m_scanning) { return false; } bool ret = false; if (NULL != m_devHandle) { m_devParam.device = m_devName; // 从设备获取到m_devParam SANE_Int num_dev_options = 0; sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); for (int i = 1; i < num_dev_options; ++i) { const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); if (NULL == desp) continue; std::string title = Utf8ToStdString(desp->title); TrimString(title); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "title=%s", title.c_str()); if (SANE_TYPE_BOOL == desp->type) { SANE_Bool value = 0; sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BOOL, value=%s", value ? "TRUE" : "FALSE"); if (0 == strcmp(title.c_str(), "图像拆分")) m_devParam.splitImage = value ? 1 : 0; else if (0 == strcmp(title.c_str(), "黑白图像噪点优化")) m_devParam.noiseDetachEnable = (bool)value; } else if (SANE_TYPE_INT == desp->type) { SANE_Int value = 0; sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=INT, value=%d", value); if (0 == strcmp(title.c_str(), "跳过空白页灵敏度")) m_devParam.discardBlankThre = value; else if (0 == strcmp(title.c_str(), "分辨率")) m_devParam.resolution = value; } else if (SANE_TYPE_FIXED == desp->type) { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=FIXED"); } else if (SANE_TYPE_STRING == desp->type) { char value[256] = {0}; sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL); std::string stdValue = Utf8ToStdString(value); TrimString(stdValue); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=STRING, value=%s", stdValue.c_str()); if (0 == strcmp("扫描张数", title.c_str()) && 0 == strcmp("连续扫描", stdValue.c_str())) m_devParam.autofeeder = true; else if (0 == strcmp("扫描张数", title.c_str()) && 0 == strcmp("扫描指定张数", stdValue.c_str())) m_devParam.autofeeder = false; else if (0 == strcmp("颜色模式", title.c_str()) && 0 == strcmp("24位彩色", stdValue.c_str())) m_devParam.pixel = 2; else if (0 == strcmp("颜色模式", title.c_str()) && 0 == strcmp("256级灰度", stdValue.c_str())) m_devParam.pixel = 1; else if (0 == strcmp("颜色模式", title.c_str()) && 0 == strcmp("黑白", stdValue.c_str())) m_devParam.pixel = 0; else if (0 == strcmp("扫描页面", title.c_str()) && 0 == strcmp("双面", stdValue.c_str())) { m_devParam.white = false; m_devParam.single = false; } else if (0 == strcmp("扫描页面", title.c_str()) && 0 == strcmp("单面", stdValue.c_str())) { m_devParam.white = false; m_devParam.single = true; } else if (0 == strcmp("扫描页面", title.c_str()) && NULL != strstr(stdValue.c_str(), "跳过空白页")) { m_devParam.white = true; m_devParam.single = false; } else if (0 == strcmp("文稿方向", title.c_str()) && 0 == strcmp("0°", stdValue.c_str())) m_devParam.orentation = 0; else if (0 == strcmp("文稿方向", title.c_str()) && 0 == strcmp("90°", stdValue.c_str())) m_devParam.orentation = 90; else if (0 == strcmp("文稿方向", title.c_str()) && 0 == strcmp("180°", stdValue.c_str())) m_devParam.orentation = 180; else if (0 == strcmp("文稿方向", title.c_str()) && 0 == strcmp("-90°", stdValue.c_str())) m_devParam.orentation = 270; else if (0 == strcmp("纸张尺寸", title.c_str()) && 0 == strcmp("匹配原始尺寸", stdValue.c_str())) m_devParam.paperType = "Auto"; else if (0 == strcmp("纸张尺寸", title.c_str()) && 0 == strcmp("A3", stdValue.c_str())) m_devParam.paperType = "A3"; else if (0 == strcmp("纸张尺寸", title.c_str()) && 0 == strcmp("A4", stdValue.c_str())) m_devParam.paperType = "A4"; } else if (SANE_TYPE_BUTTON == desp->type) { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BUTTON"); } else if (SANE_TYPE_GROUP == desp->type) { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=GROUP"); } } std::string filePath = GetFilePath(m_devName); std::string cfgPath = filePath + "config.ini"; m_devParam.Save(cfgPath); ret = true; } devParam = m_devParam; return ret; } bool Manager::SetDevParam(const std::string& devId, const DevParam& devParam) { if (m_scanning) { return false; } m_devParam = devParam; std::string filePath = GetFilePath(m_devName); std::string cfgPath = filePath + "config.ini"; m_devParam.Save(cfgPath); bool ret = false; if (NULL != m_devHandle) { // 设置m_devParam到设备 SANE_Int num_dev_options = 0; sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); for (int i = 1; i < num_dev_options; ++i) { const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); if (NULL == desp) continue; std::string title = Utf8ToStdString(desp->title); TrimString(title); if (SANE_TYPE_BOOL == desp->type) { if (0 == strcmp(title.c_str(), "图像拆分")) { SANE_Bool value = SANE_FALSE; if (0 != m_devParam.splitImage) value = SANE_TRUE; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } else if (0 == strcmp(title.c_str(), "黑白图像噪点优化")) { SANE_Bool value = SANE_FALSE; if (m_devParam.noiseDetachEnable) value = SANE_TRUE; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } } else if (SANE_TYPE_INT == desp->type) { if (0 == strcmp(title.c_str(), "跳过空白页灵敏度")) { SANE_Int value = m_devParam.discardBlankThre; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } else if (0 == strcmp(title.c_str(), "分辨率")) { SANE_Int value = m_devParam.resolution; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } else if (0 == strcmp(title.c_str(), "扫描数量")) { SANE_Int value = 1; //if (!m_devParam.autofeeder) // value = 1; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } } else if (SANE_TYPE_FIXED == desp->type) { } else if (SANE_TYPE_STRING == desp->type) { if (0 == strcmp("扫描张数", title.c_str())) { char value[256] = { 0 }; strcpy(value, "连续扫描"); if (!m_devParam.autofeeder) strcpy(value, "扫描指定张数"); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)(StdStringToUtf8(value).c_str()), NULL); } else if (0 == strcmp("颜色模式", title.c_str())) { char value[256] = { 0 }; strcpy(value, "24位彩色"); if (1 == m_devParam.pixel) strcpy(value, "256级灰度"); else if (0 == m_devParam.pixel) strcpy(value, "黑白"); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void *)(StdStringToUtf8(value).c_str()), NULL); } else if (0 == strcmp("扫描页面", title.c_str())) { char value[256] = { 0 }; strcpy(value, "双面"); if (m_devParam.single) strcpy(value, "单面"); else if (m_devParam.white) strcpy(value, "跳过空白页(通用)"); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)(StdStringToUtf8(value).c_str()), NULL); } else if (0 == strcmp("文稿方向", title.c_str())) { char value[256] = { 0 }; strcpy(value, "0°"); if (90 == m_devParam.orentation) strcpy(value, "90°"); else if (180 == m_devParam.orentation) strcpy(value, "180°"); else if (270 == m_devParam.orentation) strcpy(value, "-90°"); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)(StdStringToUtf8(value).c_str()), NULL); } else if (0 == strcmp("纸张尺寸", title.c_str())) { char value[256] = { 0 }; strcpy(value, "匹配原始尺寸"); if ("A3" == m_devParam.paperType) strcpy(value, "A3"); else if ("A4" == m_devParam.paperType) strcpy(value, "A4"); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)(StdStringToUtf8(value).c_str()), NULL); } } else if (SANE_TYPE_BUTTON == desp->type) { } else if (SANE_TYPE_GROUP == desp->type) { } } ret = true; } return ret; } bool Manager::ExportOfd(const std::string& devId, bool isAuto, std::string& imgBase64) { imgBase64.clear(); if (m_scanning) { return false; } HGByte* data = NULL; HGUInt size = 0; ExportOfdFile(devId, isAuto, &data, &size); if (NULL != data) { imgBase64 = GetBase64(data, size); delete[] data; } return !imgBase64.empty(); } bool Manager::ExportOfdFile(const std::string& devId, bool isAuto, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(tmpFileName, 512); HGOfdImageWriter writer = NULL; HGImgFmt_OpenOfdImageWriter(tmpFileName, &writer); if (NULL == writer) { return false; } bool ret = false; for (int i = 0; i < (int)fileNameList.size(); ++i) { HGImage img = NULL; std::string fileName = filePath + fileNameList[i]; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToOfdImageWriter(writer, img, NULL, 0)) ret = true; HGBase_DestroyImage(img); } } HGImgFmt_CloseOfdImageWriter(writer); if (!ret) { HGBase_DeleteFile(tmpFileName); return false; } *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool Manager::ExportPdf(const std::string& devId, std::string& imgBase64) { imgBase64.clear(); if (m_scanning) { return false; } HGByte* data = NULL; HGUInt size = 0; ExportPdfFile(devId, &data, &size); if (NULL != data) { imgBase64 = GetBase64(data, size); delete[] data; } return !imgBase64.empty(); } bool Manager::ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(tmpFileName, 512); HGPdfImageWriter writer = NULL; HGImgFmt_OpenPdfImageWriter(tmpFileName, &writer); if (NULL == writer) { return false; } bool ret = false; for (int i = 0; i < (int)fileNameList.size(); ++i) { HGImage img = NULL; std::string fileName = filePath + fileNameList[i]; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToPdfImageWriter(writer, img, NULL, 0)) ret = true; HGBase_DestroyImage(img); } } HGImgFmt_ClosePdfImageWriter(writer); if (!ret) { HGBase_DeleteFile(tmpFileName); return false; } *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool Manager::ExportTiff(const std::string& devId, std::string& imgBase64) { imgBase64.clear(); if (m_scanning) { return false; } HGByte* data = NULL; HGUInt size = 0; ExportTiffFile(devId, &data, &size); if (NULL != data) { imgBase64 = GetBase64(data, size); delete[] data; } return !imgBase64.empty(); } bool Manager::ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(tmpFileName, 512); HGTiffWriter writer = NULL; HGImgFmt_OpenTiffWriter(tmpFileName, &writer); if (NULL == writer) { return false; } bool ret = false; for (int i = 0; i < (int)fileNameList.size(); ++i) { HGImage img = NULL; std::string fileName = filePath + fileNameList[i]; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { if (HGBASE_ERR_OK == HGImgFmt_SaveImageToTiffWriter(writer, img, NULL)) ret = true; HGBase_DestroyImage(img); } } HGImgFmt_CloseTiffWriter(writer); if (!ret) { HGBase_DeleteFile(tmpFileName); return false; } *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool Manager::ExportZip(const std::string& devId, std::string& imgBase64) { imgBase64.clear(); if (m_scanning) { return false; } HGByte* data = NULL; HGUInt size = 0; ExportZipFile(devId, &data, &size); if (NULL != data) { imgBase64 = GetBase64(data, size); delete[] data; } return !imgBase64.empty(); } bool Manager::ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(tmpFileName, 512); int error = 0; zip* z = zip_open(StdStringToUtf8(tmpFileName).c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error); if (NULL == z) { return false; } bool ret = false; for (int i = 0; i < (int)fileNameList.size(); ++i) { std::string fileName = filePath + fileNameList[i]; zip_source_t* s = zip_source_file(z, StdStringToUtf8(fileName.c_str()).c_str(), 0, 0); if (NULL != s) { if (zip_file_add(z, StdStringToUtf8(fileNameList[i].c_str()).c_str(), s, ZIP_FL_OVERWRITE) >= 0) { ret = true; } else { zip_source_free(s); } } } zip_close(z); z = NULL; if (!ret) { HGBase_DeleteFile(tmpFileName); return false; } *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool Manager::UploadImage(const UploadParam& uploadParam) { if (m_scanning) { return false; } // 打包并上传 return true; } bool Manager::SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64) { if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::string fileName = filePath + imgName; size_t pos = imgBase64.find(","); if (std::string::npos != pos) return SaveBase64(fileName, imgBase64.c_str() + pos + 1); else return SaveBase64(fileName, imgBase64.c_str()); } bool Manager::DeleteImage(const std::string& devId, const std::string& imgName) { if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::string fileName = filePath + imgName; bool ret = false; if (HGBASE_ERR_OK == HGBase_DeleteFile(fileName.c_str())) { std::vector fileNameList = GetFileNameList(devId); for (int i = 0; i < (int)fileNameList.size(); ++i) { if (imgName == fileNameList[i]) { fileNameList.erase(fileNameList.begin() + i); break; } } SaveFileNameList(devId, fileNameList); ret = true; } return ret; } bool Manager::DeleteAllImage(const std::string& devId) { if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); for (int i = 0; i < (int)fileNameList.size(); ++i) { std::string fileName = filePath + fileNameList[i]; HGBase_DeleteFile(fileName.c_str()); } fileNameList.clear(); SaveFileNameList(devId, fileNameList); return true; } bool Manager::MergeImage(const std::string& devId, bool isHorizontal, const std::vector& imgIndexList, std::string& imgName, std::string& imgBase64) { imgName.clear(); imgBase64.clear(); if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); std::vector imgList; for (int i = 0; i < (int)imgIndexList.size(); ++i) { int index = imgIndexList[i]; if (index >= 0 && index < (int)fileNameList.size()) { std::string fileName = filePath + fileNameList[index]; HGImage img = NULL; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) imgList.push_back(img); } } HGUInt width = 0, height = 0; for (int i = 0; i < (int)imgList.size(); ++i) { HGImageInfo imgInfo; HGBase_GetImageInfo(imgList[i], &imgInfo); if (isHorizontal) { width += imgInfo.width; if (imgInfo.height > height) height = imgInfo.height; } else { height += imgInfo.height; if (imgInfo.width > width) width = imgInfo.width; } } if (width > 0 && height > 0) { HGImage img = NULL; HGBase_CreateImage(width, height, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { HGUInt x = 0, y = 0; for (int i = 0; i < (int)imgList.size(); ++i) { HGImageInfo imgInfo; HGBase_GetImageInfo(imgList[i], &imgInfo); HGImageRoi roi = {x, y, x + imgInfo.width, y + imgInfo.height}; HGBase_SetImageROI(img, &roi); HGBase_CopyImage(imgList[i], img); HGBase_ResetImageROI(img); if (isHorizontal) x += imgInfo.width; else y += imgInfo.height; } int maxIndex = 0; for (int i = 0; i < (int)fileNameList.size(); ++i) { int index = atoi(fileNameList[i].c_str()); if (index > maxIndex) maxIndex = index; } HGChar name[256]; sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); imgName = name; HGBase_CreateDir(filePath.c_str()); std::string fileName = filePath + imgName; HGImgFmt_SaveImage(img, 0, NULL, 0, fileName.c_str()); fileNameList.push_back(imgName); SaveFileNameList(devId, fileNameList); imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(img, 0); HGBase_DestroyImage(img); } } for (int i = 0; i < (int)imgList.size(); ++i) { HGBase_DestroyImage(imgList[i]); } return true; } static bool LowerSort(const std::string &str1, const std::string& str2) { return atoi(str1.c_str()) < atoi(str2.c_str()); } bool Manager::BookSort(const std::string& devId, std::vector& imgNameList, std::vector& imgBase64List) { imgNameList.clear(); imgBase64List.clear(); if (m_scanning) { return false; } std::vector fileNameList = GetFileNameList(devId); std::sort(fileNameList.begin(), fileNameList.end(), LowerSort); SaveFileNameList(devId, fileNameList); return GetImageByDevId(devId, imgNameList, imgBase64List); } bool Manager::ExchangeImage(const std::string& devId, int index1, int index2) { if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (index1 < 0 || index1 >= (int)fileNameList.size() || index2 < 0 || index2 >= (int)fileNameList.size()) return false; std::string imgName1 = fileNameList[index1]; std::string imgName2 = fileNameList[index2]; fileNameList[index1] = imgName2; fileNameList[index2] = imgName1; SaveFileNameList(devId, fileNameList); return true; } bool Manager::GetLastBetch(std::string& devId) { devId.clear(); if (m_scanning) { return false; } devId = m_devName; return true; } bool Manager::ResetPatchIndex() { if (m_scanning) { return false; } return true; } bool Manager::SplitImage(const std::string& devId, const std::string& imgName, bool isHorizontal, int x1, int y1, int x2, int y2, std::string& imgName1, std::string& imgBase64_1, std::string& imgName2, std::string& imgBase64_2) { imgName1.clear(); imgBase64_1.clear(); imgName2.clear(); imgBase64_2.clear(); if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::string fileName = filePath + imgName; HGImage img = NULL; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { HGImageInfo imgInfo; HGBase_GetImageInfo(img, &imgInfo); for (int i = 0; i < 2; ++i) { HGUInt newWidth = isHorizontal ? x1 : imgInfo.width; if (1 == i) newWidth = isHorizontal ? (imgInfo.width - x1) : imgInfo.width; HGUInt newHeight = isHorizontal ? imgInfo.height : y1; if (1 == i) newHeight = isHorizontal ? imgInfo.height : (imgInfo.height - y1); HGImage newImg = NULL; HGBase_CreateImage(newWidth, newHeight, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &newImg); if (NULL != newImg) { HGImageRoi roi; if (isHorizontal) { roi.left = 0; roi.top = 0; roi.right = x1; roi.bottom = imgInfo.height; if (1 == i) { roi.left = x1; roi.right = imgInfo.width; } } else { roi.left = 0; roi.top = 0; roi.right = imgInfo.width; roi.bottom = y1; if (1 == i) { roi.top = y1; roi.bottom = imgInfo.height; } } if (1 == i) { roi.left = x1; roi.right = imgInfo.width; } HGBase_SetImageROI(img, &roi); HGBase_CopyImage(img, newImg); HGBase_ResetImageROI(img); std::vector fileNameList = GetFileNameList(devId); int maxIndex = 0; for (int i = 0; i < (int)fileNameList.size(); ++i) { int index = atoi(fileNameList[i].c_str()); if (index > maxIndex) maxIndex = index; } HGChar name[256]; sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); std::string imgName = name; HGBase_CreateDir(filePath.c_str()); std::string fileName = filePath + imgName; HGImgFmt_SaveImage(newImg, 0, NULL, 0, fileName.c_str()); fileNameList.push_back(imgName); SaveFileNameList(devId, fileNameList); std::string imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(newImg, 0); if (0 == i) { imgName1 = imgName; imgBase64_1 = imgBase64; } else { imgName2 = imgName; imgBase64_2 = imgBase64; } HGBase_DestroyImage(newImg); } } HGBase_DestroyImage(img); } return true; } bool Manager::GetDevSerialNo(const std::string& devId, std::string& serialNo) { serialNo.clear(); if (m_scanning) { return false; } serialNo = devId; return true; } bool Manager::GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64) { imgBase64.clear(); if (m_scanning) { return false; } std::string filePath = GetFilePath(devId); std::string fileName = filePath + imgName; HGImage img = NULL; HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(img, 0); HGBase_DestroyImage(img); } return true; } std::string Manager::GetFilePath(const std::string& devId) { HGChar docsPath[512]; HGBase_GetDocumentsPath(docsPath, 512); HGChar imgPath[512]; sprintf(imgPath, "%sHuago/WebService/%s/", docsPath, Utf8ToStdString(devId.c_str()).c_str()); HGChar stdImgPath[512]; HGBase_StandardiseFileName(imgPath, stdImgPath, 512); return stdImgPath; } std::vector Manager::GetFileNameList(const std::string& devId) { std::vector fileNameList; std::string filePath = GetFilePath(devId); std::string cfgName = filePath + "images.txt"; FILE* file = fopen(cfgName.c_str(), "r"); if (NULL != file) { while (feof(file) == 0) { char lineContent[256] = { 0 }; if (NULL == fgets(lineContent, 256, file) || '\n' == *lineContent) { continue; } if (lineContent[strlen(lineContent) - 1] == '\n') lineContent[strlen(lineContent) - 1] = 0; fileNameList.push_back(lineContent); } fclose(file); } return fileNameList; } bool Manager::SaveFileNameList(const std::string& devId, const std::vector& fileNameList) { bool ret = false; std::string filePath = GetFilePath(devId); std::string cfgName = filePath + "images.txt"; FILE* file = fopen(cfgName.c_str(), "w"); if (NULL != file) { for (int i = 0; i < (int)fileNameList.size(); ++i) { fwrite(fileNameList[i].c_str(), 1, fileNameList[i].size(), file); fwrite("\n", 1, strlen("\n"), file); } fclose(file); ret = true; } return ret; } std::string Manager::GetBase64(HGImage image, HGUInt quality) { std::string strBase64; if (NULL != image) { HGBuffer buffer = NULL; HGImgFmt_SaveJpegImageToBuffer(image, NULL, quality, &buffer); if (NULL != buffer) { HGByte* data; HGBase_GetBufferData(buffer, &data); HGUSize size; HGBase_GetBufferSize(buffer, &size); HGSize base64Size = 0; HGBase_Base64Encode(data, size, NULL, &base64Size); uint8_t* base64 = new uint8_t[base64Size + 1]; HGBase_Base64Encode(data, size, base64, &base64Size); base64[base64Size] = 0; strBase64 = (const char*)base64; delete[] base64; HGBase_DestroyBuffer(buffer); } } return strBase64; } std::string Manager::GetBase64(const HGByte* data, HGUInt size) { std::string strBase64; if (NULL != data && 0 != size) { HGSize base64Size = 0; HGBase_Base64Encode(data, size, NULL, &base64Size); uint8_t* base64 = new uint8_t[base64Size + 1]; HGBase_Base64Encode(data, size, base64, &base64Size); base64[base64Size] = 0; strBase64 = (const char*)base64; delete[] base64; } return strBase64; } std::string Manager::GetBase64(const std::string& fileName) { std::string strBase64; 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* base64 = new uint8_t[base64Size + 1]; HGBase_Base64Encode(data, size, base64, &base64Size); base64[base64Size] = 0; strBase64 = (const char*)base64; delete[] base64; } delete[] data; } fclose(file); } return strBase64; } HGByte* Manager::GetBuffer(const std::string& fileName, HGUInt* size) { HGByte* data = NULL; *size = 0; FILE* file = fopen(fileName.c_str(), "rb"); if (NULL != file) { fseek(file, 0, SEEK_END); *size = ftell(file); fseek(file, 0, SEEK_SET); if (*size > 0) { data = new HGByte[*size]; HGUInt readLen = (HGUInt)fread(data, 1, *size, file); if (readLen != *size) { delete[] data; data = NULL; *size = 0; } } fclose(file); } return data; } bool Manager::SaveBase64(const std::string& fileName, const char* base64) { if (NULL == base64 || 0 == *base64) return false; HGUInt base64Size = (HGUInt)strlen(base64); bool ret = false; FILE* file = fopen(fileName.c_str(), "wb"); if (NULL != file) { HGSize size = 0; HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, NULL, &size); uint8_t* data = new HGByte[size]; HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, data, &size); size_t writeLen = fwrite(data, 1, size, file); if (writeLen == size) ret = true; delete[] data; fclose(file); } return ret; } int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param) { (void)hdev; (void)len; Manager* p = (Manager*)param; switch (code) { case SANE_EVENT_DEVICE_ARRIVED: { SANE_Device* sane_dev = (SANE_Device*)data; HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_ARRIVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); OpenDevParam* openDevParam = new OpenDevParam; openDevParam->devName = sane_dev->name; WebMsg msg; msg.msgId = WEB_MSGID_OPENDEV; msg.svrId = 0; msg.usrId = 0; msg.param = openDevParam; bool b = p->m_loop->Send(&msg); if (!b) { delete openDevParam; } } break; case SANE_EVENT_DEVICE_LEFT: { SANE_Device* sane_dev = (SANE_Device*)data; HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_REMOVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); CloseDevParam* closeDevParam = new CloseDevParam; closeDevParam->devName = sane_dev->name; WebMsg msg; msg.msgId = WEB_MSGID_CLOSEDEV; msg.svrId = 0; msg.usrId = 0; msg.param = closeDevParam; bool b = p->m_loop->Send(&msg); if (!b) { delete closeDevParam; } } break; case SANE_EVENT_STATUS: { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char *)data).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_STATUS, (void*)data, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); } break; case SANE_EVENT_ERROR: { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_ERROR, (void *)data, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); } break; case SANE_EVENT_WORKING: { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_WORKING, (void*)data, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); } break; case SANE_EVENT_IMAGE_OK: { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_IMAGE_OK"); SANE_Image* sane_img = (SANE_Image*)data; HGUInt imgType = 0; if (sane_img->header.format == SANE_FRAME_GRAY) imgType = HGBASE_IMGTYPE_GRAY; else if (sane_img->header.format == SANE_FRAME_RGB) imgType = HGBASE_IMGTYPE_RGB; HGByte* data = sane_img->data; HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines, imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP }; HGImage img = NULL; HGBase_CreateImageFromData(data, &imgInfo, nullptr, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { std::string filePath = GetFilePath(p->m_devName); std::vector fileNameList = GetFileNameList(p->m_devName); int maxIndex = 0; for (int i = 0; i < (int)fileNameList.size(); ++i) { int index = atoi(fileNameList[i].c_str()); if (index > maxIndex) maxIndex = index; } HGChar name[256]; sprintf(name, "%d.%s", maxIndex + 1, p->m_devParam.format.c_str()); std::string imgName = name; HGBase_CreateDir(filePath.c_str()); std::string fileName = filePath + imgName; HGImgFmt_SaveImage(img, 0, NULL, 0, fileName.c_str()); if (p->m_scanIsInsert) { int index = -1; for (int i = 0; i < (int)fileNameList.size(); ++i) { if (fileNameList[i] == p->m_scanInsertImgName) { index = i; break; } } if (-1 != index) { fileNameList.insert(fileNameList.begin() + index, imgName); } else { fileNameList.push_back(imgName); } } else { fileNameList.push_back(imgName); } SaveFileNameList(p->m_devName, fileNameList); std::string imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(img, 0); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_IMAGE, (void*)imgName.c_str(), (void *)imgBase64.c_str(), p->m_scanParam); HGBase_LeaveLock(p->m_lock); HGBase_DestroyImage(img); } } break; case SANE_EVENT_SCAN_FINISHED: { HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_EnterLock(p->m_lock); if (NULL != p->m_scanEvent) p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam); HGBase_LeaveLock(p->m_lock); WebMsg msg; msg.msgId = WEB_MSGID_STOPSCAN; msg.svrId = 0; msg.usrId = 0; msg.param = NULL; p->m_loop->Send(&msg); } break; } return 0; }