#include "ManagerV1.h" #include "base/HGBuffer.h" #include "base/HGBase64.h" #include "base/HGUtility.h" #include "base/HGIni.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" extern "C" { #include "zip.h" }; #include #include #include namespace ver_1 { 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; ftpPath = "/images"; ftpPort = 21; ftpMode = 2; } void DevParam::Load(const std::string& devName) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); strcat(cfgPath, "config1.ini"); HGInt intValue; HGChar strValue[256]; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "device", "", strValue, 256); device = strValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "autofeeder", 1, &intValue); autofeeder = (bool)intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "pixel", 1, &intValue); pixel = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "white", 0, &intValue); white = (bool)intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "discardBlankThre", 5, &intValue); discardBlankThre = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "single", 0, &intValue); single = (bool)intValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "format", "jpg", strValue, 256); format = strValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "resolution", 200, &intValue); resolution = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "orentation", 0, &intValue); orentation = intValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "paperType", "Auto", strValue, 256); paperType = strValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "splitImage", 0, &intValue); splitImage = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "noiseDetachEnable”", 1, &intValue); noiseDetachEnable = (bool)intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "noiseDetach", 15, &intValue); noiseDetach = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "uploadMode", 2, &intValue); uploadMode = intValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "httpUrl", "", strValue, 256); httpUrl = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "fileName", "", strValue, 256); fileName = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "httpMethod", "", strValue, 256); httpMethod = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "header", "", strValue, 256); header = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "param", "", strValue, 256); param = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "ftpUrl", "", strValue, 256); ftpUrl = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "ftpPath", "", strValue, 256); ftpPath = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "ftpUser", "", strValue, 256); ftpUser = strValue; HGBase_GetProfileString(cfgPath, Utf8ToStdString(devName).c_str(), "ftpPassword", "", strValue, 256); ftpPassword = strValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "ftpPort", 21, &intValue); ftpPort = intValue; HGBase_GetProfileInt(cfgPath, Utf8ToStdString(devName).c_str(), "ftpMode", 2, &intValue); ftpMode = intValue; } void DevParam::Save(const std::string& devName) { HGChar cfgPath[256]; HGBase_GetConfigPath(cfgPath, 256); HGBase_CreateDir(cfgPath); strcat(cfgPath, "config1.ini"); HGBase_SetProfileString(cfgPath, devName.c_str(), "device", device.c_str()); HGBase_SetProfileInt(cfgPath, devName.c_str(), "autofeeder", autofeeder); HGBase_SetProfileInt(cfgPath, devName.c_str(), "pixel", pixel); HGBase_SetProfileInt(cfgPath, devName.c_str(), "white", white); HGBase_SetProfileInt(cfgPath, devName.c_str(), "discardBlankThre", discardBlankThre); HGBase_SetProfileInt(cfgPath, devName.c_str(), "single", single); HGBase_SetProfileString(cfgPath, devName.c_str(), "format", format.c_str()); HGBase_SetProfileInt(cfgPath, devName.c_str(), "resolution", resolution); HGBase_SetProfileInt(cfgPath, devName.c_str(), "orentation", orentation); HGBase_SetProfileString(cfgPath, devName.c_str(), "paperType", paperType.c_str()); HGBase_SetProfileInt(cfgPath, devName.c_str(), "splitImage", splitImage); HGBase_SetProfileInt(cfgPath, devName.c_str(), "noiseDetachEnable", noiseDetachEnable); HGBase_SetProfileInt(cfgPath, devName.c_str(), "noiseDetach", noiseDetach); HGBase_SetProfileInt(cfgPath, devName.c_str(), "uploadMode", uploadMode); HGBase_SetProfileString(cfgPath, devName.c_str(), "httpUrl", httpUrl.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "fileName", fileName.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "httpMethod", httpMethod.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "header", header.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "param", param.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "ftpUrl", ftpUrl.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "ftpPath", ftpPath.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "ftpUser", ftpUser.c_str()); HGBase_SetProfileString(cfgPath, devName.c_str(), "ftpPassword", ftpPassword.c_str()); HGBase_SetProfileInt(cfgPath, devName.c_str(), "ftpPort", ftpPort); HGBase_SetProfileInt(cfgPath, devName.c_str(), "ftpMode", ftpMode); } ManagerV1::ManagerV1(HGMsgPump msgPump) : Manager(msgPump) { HGBase_CreateLock(&m_lock); m_devName.clear(); m_devHandle = NULL; m_saneEvent = NULL; m_saneParam = NULL; m_saneImageCallback = NULL; m_saneImageParam = NULL; m_scanInsertImgName.clear(); m_scanIsInsert = false; m_scanning = false; m_scanEvent = NULL; SANE_Int version_code = 0; SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this); assert(SANE_STATUS_GOOD == status); } ManagerV1::~ManagerV1() { 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 ManagerV1::OpenDev(const OpenDevParam* param) { assert(NULL != param && this == param->mgr); 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; m_devParam.Load(m_devName); SetDevParam(m_devName, m_devParam); } } void ManagerV1::CloseDev(const CloseDevParam* param) { assert(NULL != param && this == param->mgr); 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 ManagerV1::ScanFinish(const ScanFinishParam* param) { assert(NULL != param && this == param->mgr); StopScan(); } void ManagerV1::SetSaneEvent(SaneEvent event, void* param) { assert(NULL != event && NULL != param); HGBase_EnterLock(m_lock); m_saneEvent = event; m_saneParam = param; HGBase_LeaveLock(m_lock); } void ManagerV1::SetSaneImageCallback(SaneImageCallback func, void* param) { assert(NULL != func && NULL != param); HGBase_EnterLock(m_lock); m_saneImageCallback = func; m_saneImageParam = param; HGBase_LeaveLock(m_lock); } void ManagerV1::ResetSaneEvent() { HGBase_EnterLock(m_lock); m_saneEvent = NULL; m_saneParam = NULL; HGBase_LeaveLock(m_lock); } void ManagerV1::ResetSaneImageCallback() { HGBase_EnterLock(m_lock); m_saneImageCallback = NULL; m_saneImageParam = NULL; HGBase_LeaveLock(m_lock); } bool ManagerV1::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; HGBase_CreateEvent(HGFALSE, HGFALSE, &m_scanEvent); assert(NULL != m_scanEvent); if (SANE_STATUS_GOOD == sane_start(m_devHandle)) { m_scanning = true; ret = true; } else { HGBase_DestroyEvent(m_scanEvent); m_scanEvent = NULL; m_scanInsertImgName.clear(); m_scanIsInsert = false; } } return ret; } bool ManagerV1::StopScan() { if (m_scanning) { assert(NULL != m_devHandle); sane_cancel(m_devHandle); assert(NULL != m_scanEvent); HGBase_WaitEvent(m_scanEvent); HGBase_DestroyEvent(m_scanEvent); m_scanEvent = NULL; m_scanInsertImgName.clear(); m_scanIsInsert = false; m_scanning = false; } return true; } bool ManagerV1::GetCurDevId(std::string& devId) { devId.clear(); if (m_scanning) { return false; } devId = m_devName; return true; } bool ManagerV1::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 ManagerV1::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); imgNameList.push_back(fileNameList[i]); imgBase64List.push_back(imgBase64); HGBase_DestroyImage(img); } } SaveFileNameList(devId, imgNameList); return true; } bool ManagerV1::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; HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "title=%s", Utf8ToStdString(desp->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(desp->title, OPTION_TITLE_TXCF)) m_devParam.splitImage = value ? 1 : 0; else if (0 == strcmp(desp->title, OPTION_TITLE_HBTXZDYH)) 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(desp->title, OPTION_TITLE_TGKBYLMD)) m_devParam.discardBlankThre = value; else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) 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); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=STRING, value=%s", Utf8ToStdString(value).c_str()); if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_LXSM, value)) m_devParam.autofeeder = true; else if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_SMZDZS, value)) m_devParam.autofeeder = false; else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && (0 == strcmp(OPTION_VALUE_YSMS_24WCS, value) || 0 == strcmp(OPTION_VALUE_YSMS_YSZDSB, value))) m_devParam.pixel = 2; else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_256JHD, value)) m_devParam.pixel = 1; else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_HB, value)) m_devParam.pixel = 0; else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_SM, value)) { m_devParam.white = false; m_devParam.single = false; } else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_DM, value)) { m_devParam.white = false; m_devParam.single = true; } else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && (0 == strcmp(OPTION_VALUE_SMYM_TGKBYTY, value) || 0 == strcmp(OPTION_VALUE_SMYM_TGKBYFPZ, value))) { m_devParam.white = true; m_devParam.single = false; } else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_0, value)) m_devParam.orentation = 0; else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_90, value)) m_devParam.orentation = 90; else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_180, value)) m_devParam.orentation = 180; else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX__90, value)) m_devParam.orentation = 270; else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_PPYSCC, value)) m_devParam.paperType = "Auto"; else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A3, value)) m_devParam.paperType = "A3"; else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A4, value)) 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"); } } m_devParam.Save(m_devName); ret = true; } devParam = m_devParam; return ret; } bool ManagerV1::SetDevParam(const std::string& devId, const DevParam& devParam) { if (m_scanning) { return false; } m_devParam = devParam; m_devParam.Save(m_devName); 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; if (SANE_TYPE_BOOL == desp->type) { if (0 == strcmp(desp->title, OPTION_TITLE_TXCF)) { 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(desp->title, OPTION_TITLE_HBTXZDYH)) { 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(desp->title, OPTION_TITLE_TGKBYLMD)) { SANE_Int value = m_devParam.discardBlankThre; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) { SANE_Int value = m_devParam.resolution; sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); } else if (0 == strcmp(desp->title, OPTION_TITLE_SMSL)) { SANE_Int 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(OPTION_TITLE_SMZS, desp->title)) { char value[256] = { 0 }; strcpy(value, OPTION_VALUE_SMZS_LXSM); if (!m_devParam.autofeeder) strcpy(value, OPTION_VALUE_SMZS_SMZDZS); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); } else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title)) { char value[256] = { 0 }; strcpy(value, OPTION_VALUE_YSMS_24WCS); if (1 == m_devParam.pixel) strcpy(value, OPTION_VALUE_YSMS_256JHD); else if (0 == m_devParam.pixel) strcpy(value, OPTION_VALUE_YSMS_HB); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); } else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title)) { char value[256] = { 0 }; strcpy(value, OPTION_VALUE_SMYM_SM); if (m_devParam.single) strcpy(value, OPTION_VALUE_SMYM_DM); else if (m_devParam.white) strcpy(value, OPTION_VALUE_SMYM_TGKBYTY); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); } else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title)) { char value[256] = { 0 }; strcpy(value, OPTION_VALUE_WGFX_0); if (90 == m_devParam.orentation) strcpy(value, OPTION_VALUE_WGFX_90); else if (180 == m_devParam.orentation) strcpy(value, OPTION_VALUE_WGFX_180); else if (270 == m_devParam.orentation) strcpy(value, OPTION_VALUE_WGFX__90); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); } else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title)) { char value[256] = { 0 }; strcpy(value, OPTION_VALUE_ZZCC_PPYSCC); if ("A3" == m_devParam.paperType) strcpy(value, OPTION_VALUE_ZZCC_A3); else if ("A4" == m_devParam.paperType) strcpy(value, OPTION_VALUE_ZZCC_A4); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); } } else if (SANE_TYPE_BUTTON == desp->type) { } else if (SANE_TYPE_GROUP == desp->type) { } } ret = true; } return ret; } bool ManagerV1::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 ManagerV1::ExportOfdFile(const std::string& devId, bool isAuto, const std::string& fileName) { std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGOfdImageWriter writer = NULL; HGImgFmt_OpenOfdImageWriter(fileName.c_str(), &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)) ret = true; HGBase_DestroyImage(img); } } HGImgFmt_CloseOfdImageWriter(writer); if (!ret) { HGBase_DeleteFile(fileName.c_str()); return false; } return true; } bool ManagerV1::ExportOfdFile(const std::string& devId, bool isAuto, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportOfdFile(devId, isAuto, tmpFileName); *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool ManagerV1::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 ManagerV1::ExportPdfFile(const std::string& devId, const std::string& fileName) { std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGPdfImageWriter writer = NULL; HGImgFmt_OpenPdfImageWriter(fileName.c_str(), &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)) ret = true; HGBase_DestroyImage(img); } } HGImgFmt_ClosePdfImageWriter(writer); if (!ret) { HGBase_DeleteFile(fileName.c_str()); return false; } return true; } bool ManagerV1::ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportPdfFile(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool ManagerV1::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 ManagerV1::ExportTiff(const std::string& devId, const std::string& fileName) { std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } HGTiffWriter writer = NULL; HGImgFmt_OpenTiffWriter(fileName.c_str(), &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(fileName.c_str()); return false; } return true; } bool ManagerV1::ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportTiff(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool ManagerV1::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 ManagerV1::ExportZipFile(const std::string& devId, const std::string& fileName) { std::string filePath = GetFilePath(devId); std::vector fileNameList = GetFileNameList(devId); if (fileNameList.empty()) { return false; } int error = 0; zip* z = zip_open(StdStringToUtf8(fileName).c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error); if (NULL == z) { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "ManagerV1::ExportZipFile Create File Fail %s", fileName.c_str()); 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(), 0, 0); if (NULL != s) { if (zip_file_add(z, StdStringToUtf8(fileNameList[i]).c_str(), s, ZIP_FL_OVERWRITE) >= 0) { ret = true; } else { zip_source_free(s); } } } zip_close(z); z = NULL; if (!ret) { HGBase_DeleteFile(fileName.c_str()); return false; } return true; } bool ManagerV1::ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size) { *data = NULL; *size = 0; if (m_scanning) { return false; } HGChar tmpFileName[512]; HGBase_GetTmpFileName(NULL, tmpFileName, 512); ExportZipFile(devId, tmpFileName); *data = GetBuffer(tmpFileName, size); HGBase_DeleteFile(tmpFileName); return (NULL != *data); } bool ManagerV1::UploadImage(const UploadParam& uploadParam) { if (m_scanning) { return false; } std::string devId; GetCurDevId(devId); HGChar tmpFileName[512]; HGBase_GetTmpFileName(NULL, tmpFileName, 512); if (0 == uploadParam.format) { strcat(tmpFileName, ".ofd"); ExportOfdFile(devId, true, tmpFileName); } else if (1 == uploadParam.format) { strcat(tmpFileName, ".pdf"); ExportPdfFile(devId, tmpFileName); } else { strcat(tmpFileName, ".zip"); ExportZipFile(devId, tmpFileName); } bool ret = false; if (0 == uploadParam.uploadMode) // HTTP { ret = HTTPUpload(tmpFileName, uploadParam.httpUrl, uploadParam.fileName, uploadParam.httpMethod, uploadParam.header, uploadParam.param); } else if (1 == uploadParam.uploadMode) // FTP { ret = FTPUpload(tmpFileName, uploadParam.ftpUrl, uploadParam.ftpPort, uploadParam.ftpPath, uploadParam.ftpUser, uploadParam.ftpPassword, uploadParam.ftpMode); } HGBase_DeleteFile(tmpFileName); return ret; } bool ManagerV1::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 ManagerV1::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 ManagerV1::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 ManagerV1::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, fileName.c_str()); fileNameList.push_back(imgName); SaveFileNameList(devId, fileNameList); imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(img); 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 ManagerV1::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 ManagerV1::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 ManagerV1::GetLastBetch(std::string& devId) { devId.clear(); if (m_scanning) { return false; } devId = m_devName; return true; } bool ManagerV1::ResetPatchIndex() { if (m_scanning) { return false; } return true; } bool ManagerV1::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, fileName.c_str()); fileNameList.push_back(imgName); SaveFileNameList(devId, fileNameList); std::string imgBase64 = "data:image/jpeg;base64,"; imgBase64 += GetBase64(newImg); if (0 == i) { imgName1 = imgName; imgBase64_1 = imgBase64; } else { imgName2 = imgName; imgBase64_2 = imgBase64; } HGBase_DestroyImage(newImg); } } HGBase_DestroyImage(img); } return true; } bool ManagerV1::GetDevSerialNo(const std::string& devId, std::string& serialNo) { serialNo.clear(); if (m_scanning) { return false; } serialNo = devId; return true; } bool ManagerV1::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); HGBase_DestroyImage(img); } return true; } std::string ManagerV1::GetFilePath(const std::string& devId) { HGChar docsPath[256]; HGBase_GetDocumentsPath(docsPath, 256); HGChar procName[256]; HGBase_GetProcessName(procName, 256); HGChar imgPath[512]; sprintf(imgPath, "%s%s/%s/", docsPath, procName, Utf8ToStdString(devId).c_str()); HGChar stdImgPath[512]; HGBase_StandardiseFileName(imgPath, stdImgPath, 512); return stdImgPath; } std::vector ManagerV1::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 ManagerV1::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; } else { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "ManagerV1::SaveFileNameList Create File Fail %s", cfgName.c_str()); } return ret; } std::string ManagerV1::GetBase64(HGImage image) { std::string strBase64; if (NULL != image) { HGBuffer buffer = NULL; HGImgFmt_SaveJpegImageToBuffer(image, NULL, &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 ManagerV1::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 ManagerV1::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* ManagerV1::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 ManagerV1::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_t)size) ret = true; delete[] data; fclose(file); } else { HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "ManagerV1::SaveBase64 Create File Fail %s", fileName.c_str()); } return ret; } bool ManagerV1::HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName, const std::string& httpMethod, const std::string& header, const std::string& param) { bool ret = false; CURL* curl = curl_easy_init(); if (NULL != curl) { struct curl_httppost* formpost = NULL; struct curl_httppost* lastptr = NULL; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_FILE, localFileName.c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "remote_filename", CURLFORM_COPYCONTENTS, remoteFileName.c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2); curl_easy_setopt(curl, CURLOPT_URL, httpUrl.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); /* Perform the request, res will get the return code */ CURLcode res = curl_easy_perform(curl); /* Check for errors */ if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res)); else ret = true; /* then cleanup the formpost chain */ curl_formfree(formpost); /* always cleanup */ curl_easy_cleanup(curl); } return ret; } static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* stream) { unsigned long nread; /* in real-world cases, this would probably get this data differently as this fread() stuff is exactly what the library already would do by default internally */ size_t retcode = fread(ptr, size, nmemb, (FILE*)stream); if (retcode > 0) { nread = (unsigned long)retcode; //fprintf(stderr, "*** We read %lu bytes from file\n", nread); } return retcode; } bool ManagerV1::FTPUpload(const std::string& localFileName, const std::string& ftpUrl, int ftpPort, const std::string& ftpPath, const std::string& ftpUser, const std::string& ftpPassword, int ftpMode) { FILE* file = fopen(localFileName.c_str(), "rb"); if (NULL == file) { return false; } bool ret = false; fseek(file, 0, SEEK_END); long fsize = ftell(file); fseek(file, 0, SEEK_SET); /* get a curl handle */ CURL* curl = curl_easy_init(); if (NULL != curl) { char remoteName[256]; HGBase_GetFileName(localFileName.c_str(), remoteName, 256); /* we want to use our own read function */ curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); char url[512]; if (!ftpUser.empty() && !ftpPassword.empty()) { sprintf(url, "ftp://%s:%s@%s:%d%s/%s", ftpUser.c_str(), ftpPassword.c_str(), ftpUrl.c_str(), ftpPort, ftpPath.c_str(), remoteName); } else { sprintf(url, "ftp://%s:%d%s/%s", ftpUrl.c_str(), ftpPort, ftpPath.c_str(), remoteName); } curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); /* now specify which file to upload */ curl_easy_setopt(curl, CURLOPT_READDATA, file); /* Set the size of the file to upload (optional). If you give a *_LARGE option you MUST make sure that the type of the passed-in argument is a curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must make sure that to pass in a type 'long' argument. */ curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); /* Now run off and do what you have been told! */ CURLcode res = curl_easy_perform(curl); /* Check for errors */ if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); else ret = true; /* always cleanup */ curl_easy_cleanup(curl); } fclose(file); /* close the local file */ return ret; } int ManagerV1::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param) { (void)hdev; (void)len; ManagerV1* p = (ManagerV1*)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()); OpenDevParam* openDevParam = new OpenDevParam; openDevParam->mgr = p; openDevParam->devName = sane_dev->name; HGMsg msg; msg.id = MSGID_OPEN_DEVICE; msg.data = openDevParam; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) { delete openDevParam; } HGBase_EnterLock(p->m_lock); if (NULL != p->m_saneEvent) p->m_saneEvent(SANEEVENT_ARRIVE, sane_dev->name, false, p->m_saneParam); HGBase_LeaveLock(p->m_lock); } 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()); CloseDevParam* closeDevParam = new CloseDevParam; closeDevParam->mgr = p; closeDevParam->devName = sane_dev->name; HGMsg msg; msg.id = MSGID_CLOSE_DEVICE; msg.data = closeDevParam; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) { delete closeDevParam; } HGBase_EnterLock(p->m_lock); if (NULL != p->m_saneEvent) p->m_saneEvent(SANEEVENT_REMOVE, sane_dev->name, false, p->m_saneParam); HGBase_LeaveLock(p->m_lock); } 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_saneEvent) p->m_saneEvent(SANEEVENT_STATUS, (const char*)data, false, p->m_saneParam); 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_saneEvent) p->m_saneEvent(SANEEVENT_ERROR, (const char*)data, (0 != *len), p->m_saneParam); 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_saneEvent) p->m_saneEvent(SANEEVENT_WORKING, (const char*)data, false, p->m_saneParam); 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, NULL, 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, fileName.c_str()); if (0 == p->m_devParam.uploadMode) // HTTP { HTTPUpload(fileName, p->m_devParam.httpUrl, p->m_devParam.fileName, p->m_devParam.httpMethod, p->m_devParam.header, p->m_devParam.param); } else if (1 == p->m_devParam.uploadMode) // FTP { FTPUpload(fileName, p->m_devParam.ftpUrl, p->m_devParam.ftpPort, p->m_devParam.ftpPath, p->m_devParam.ftpUser, p->m_devParam.ftpPassword, p->m_devParam.ftpMode); } 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); HGBase_EnterLock(p->m_lock); if (NULL != p->m_saneImageCallback) p->m_saneImageCallback(imgName.c_str(), imgBase64.c_str(), p->m_saneImageParam); 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_SetEvent(p->m_scanEvent); ScanFinishParam* scanFinishParam = new ScanFinishParam; scanFinishParam->mgr = p; HGMsg msg; msg.id = MSGID_SCAN_FINISH; msg.data = scanFinishParam; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) { delete scanFinishParam; } HGBase_EnterLock(p->m_lock); if (NULL != p->m_saneEvent) p->m_saneEvent(SANEEVENT_FINISH, (const char*)data, (0 != *len), p->m_saneParam); HGBase_LeaveLock(p->m_lock); } break; } return 0; } }