实现缩略图管理中的功能

This commit is contained in:
luoliangyi 2022-06-09 17:30:04 +08:00
parent e749a6691b
commit 3658b0e682
6 changed files with 1343 additions and 217 deletions

View File

@ -33,6 +33,11 @@ namespace ver_2
return pr1.second < pr2.second;
}
static bool ImageThumbSort(const ImageThumbInfo& info1, const ImageThumbInfo& info2)
{
return info1.idx < info2.idx;
}
ManagerV2::ManagerV2(HGMsgPump msgPump)
: Manager(msgPump)
{
@ -76,16 +81,23 @@ namespace ver_2
m_openDevice = false;
m_devName.clear();
m_devHandle = NULL;
m_devParamValid = false;
m_devParam.Reset();
m_scanning = false;
m_scanEvent = NULL;
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
strcat(cfgPath, "imageMgr.db");
char oldDbPath[256];
sprintf(oldDbPath, "%s%s", cfgPath, "imageMgr.db");
HGBase_DeleteFile(oldDbPath);
char newDbPath[256];
sprintf(newDbPath, "%s%s", cfgPath, "imageList.db");
m_sqlite = NULL;
sqlite3_open_v2(cfgPath, &m_sqlite, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
sqlite3_open_v2(newDbPath, &m_sqlite, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE, NULL);
if (NULL != m_sqlite)
{
@ -94,7 +106,7 @@ namespace ver_2
int ret = sqlite3_exec(m_sqlite, "create table table_ (id integer primary key autoincrement, name text)", NULL, NULL, NULL);
if (0 == ret)
{
ret = sqlite3_exec(m_sqlite, "create table 'table_default' (id integer primary key autoincrement, idx integer, format text, tag text, image blob)", NULL, NULL, NULL);
ret = sqlite3_exec(m_sqlite, "create table 'table_default' (id integer primary key autoincrement, idx integer, format text, tag text, image blob, thumb blob)", NULL, NULL, NULL);
assert(0 == ret);
ret = sqlite3_exec(m_sqlite, "insert into table_ (name) values ('default')", NULL, NULL, NULL);
assert(0 == ret);
@ -899,9 +911,12 @@ namespace ver_2
// 设置devParam到设备
SetParamToDevice(dev, devParam, 0xFFFFFFFF);
// 从设备获取并设置m_devParam
GetParamFromDevice(dev, m_devParam);
// 保存到配置文件
RestoreDeviceParam(deviceName, m_devParam);
if (0 == GetParamFromDevice(dev, m_devParam))
{
m_devParamValid = true;
// 保存到配置文件
RestoreDeviceParam(deviceName, m_devParam);
}
m_devHandle = dev;
m_devName = deviceName;
@ -924,6 +939,7 @@ namespace ver_2
m_devHandle = NULL;
m_devName.clear();
m_devParam.Reset();
m_devParamValid = false;
m_openDevice = false;
errInfo.clear();
@ -940,9 +956,12 @@ namespace ver_2
// 设置devParam到设备
int ret = SetParamToDevice(m_devHandle, param, mask);
// 从设备获取并设置m_devParam
GetParamFromDevice(m_devHandle, m_devParam);
// 保存到配置文件
RestoreDeviceParam(m_devName, m_devParam);
if (0 == GetParamFromDevice(m_devHandle, m_devParam))
{
m_devParamValid = true;
// 保存到配置文件
RestoreDeviceParam(m_devName, m_devParam);
}
if (0 == ret)
errInfo.clear();
@ -954,7 +973,7 @@ namespace ver_2
param.Reset();
errInfo = "错误";
if (!m_openDevice)
if (!m_openDevice || !m_devParamValid)
return -1;
param = m_devParam;
@ -1124,7 +1143,7 @@ namespace ver_2
return -1;
char sql[256];
sprintf(sql, "create table 'table_%s' (id integer primary key autoincrement, idx integer, format text, tag text, image blob)", batchId.c_str());
sprintf(sql, "create table 'table_%s' (id integer primary key autoincrement, idx integer, format text, tag text, image blob, thumb blob)", batchId.c_str());
int ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
return -1;
@ -1176,6 +1195,68 @@ namespace ver_2
return 0;
}
int ManagerV2::GetImageThumbnailList(std::vector<ImageThumbInfo>& imageThumbList, std::string& errInfo)
{
imageThumbList.clear();
errInfo = "错误";
if (NULL == m_sqlite)
return -1;
int rc = -1;
sqlite3_stmt* stmt = NULL;
char sql[256];
sprintf(sql, "select * from 'table_%s'", m_currBatchId.c_str());
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_step(stmt);
while (SQLITE_ROW == ret)
{
int idx = sqlite3_column_int(stmt, 1);
std::string imgFmt = (const char*)sqlite3_column_text(stmt, 2);
std::string imgTag = (const char*)sqlite3_column_text(stmt, 3);
const void* thumbData = sqlite3_column_blob(stmt, 5);
int thumbSize = sqlite3_column_bytes(stmt, 5);
std::string thumbBase64;
SaveToBase64((const HGByte*)thumbData, thumbSize, thumbBase64);
assert(!thumbBase64.empty());
std::string prefix;
if ("jpg" == imgFmt)
prefix = "data:image/jpeg;base64,";
else if ("bmp" == imgFmt)
prefix = "data:image/bmp;base64,";
else if ("png" == imgFmt)
prefix = "data:image/png;base64,";
else if ("tif" == imgFmt)
prefix = "data:image/tiff;base64,";
else if ("pdf" == imgFmt)
prefix = "data:image/pdf;base64,";
else if ("ofd" == imgFmt)
prefix = "data:image/ofd;base64,";
thumbBase64.insert(0, prefix);
ImageThumbInfo thumbInfo;
thumbInfo.idx = idx;
thumbInfo.tag = imgTag;
thumbInfo.thumbBase64 = thumbBase64;
imageThumbList.push_back(thumbInfo);
ret = sqlite3_step(stmt);
}
ret = sqlite3_finalize(stmt);
assert(0 == ret);
std::sort(imageThumbList.begin(), imageThumbList.end(), ImageThumbSort);
errInfo.clear();
return 0;
}
int ManagerV2::GetImageCount(int& imageCount, std::string& errInfo)
{
imageCount = 0;
@ -1222,30 +1303,33 @@ namespace ver_2
if (idx == imageIndex)
{
std::string imgFmt = (const char*)sqlite3_column_text(stmt, 2);
imageTag = (const char*)sqlite3_column_text(stmt, 3);
std::string imgTag = (const char*)sqlite3_column_text(stmt, 3);
const void* imgData = sqlite3_column_blob(stmt, 4);
int imgSize = sqlite3_column_bytes(stmt, 4);
GetBase64((const HGByte *)imgData, imgSize, imageBase64);
assert(!imageBase64.empty());
if (SaveToBase64((const HGByte*)imgData, imgSize, imageBase64))
{
imageTag = imgTag;
std::string prefix;
if ("jpg" == imgFmt)
prefix = "data:image/jpeg;base64,";
else if ("bmp" == imgFmt)
prefix = "data:image/bmp;base64,";
else if ("png" == imgFmt)
prefix = "data:image/png;base64,";
else if ("tif" == imgFmt)
prefix = "data:image/tiff;base64,";
else if ("pdf" == imgFmt)
prefix = "data:image/pdf;base64,";
else if ("ofd" == imgFmt)
prefix = "data:image/ofd;base64,";
imageBase64.insert(0, prefix);
std::string prefix;
if ("jpg" == imgFmt)
prefix = "data:image/jpeg;base64,";
else if ("bmp" == imgFmt)
prefix = "data:image/bmp;base64,";
else if ("png" == imgFmt)
prefix = "data:image/png;base64,";
else if ("tif" == imgFmt)
prefix = "data:image/tiff;base64,";
else if ("pdf" == imgFmt)
prefix = "data:image/pdf;base64,";
else if ("ofd" == imgFmt)
prefix = "data:image/ofd;base64,";
imageBase64.insert(0, prefix);
errInfo.clear();
rc = 0;
}
errInfo.clear();
rc = 0;
break;
}
@ -1283,22 +1367,8 @@ namespace ver_2
const void* imgData = sqlite3_column_blob(stmt, 4);
int imgSize = sqlite3_column_bytes(stmt, 4);
bool saveRet = false;
std::string imagePath2 = GetFilePath(imgFmt.c_str());
FILE* file = fopen(imagePath2.c_str(), "wb");
if (NULL != file)
{
size_t writeLen = fwrite(imgData, 1, imgSize, file);
if (writeLen == (size_t)imgSize)
saveRet = true;
fclose(file);
}
if (!saveRet)
{
HGBase_DeleteFile(imagePath2.c_str());
}
else
if (SaveToFile((const HGByte*)imgData, imgSize, imagePath2))
{
imagePath = imagePath2;
HGBase_EnterLock(m_lock);
@ -1328,56 +1398,20 @@ namespace ver_2
if (NULL == m_sqlite || imagePath.empty() || insertPos < -1)
return -1;
HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType);
if (0 == imgType)
return -1;
std::string format;
if (HGIMGFMT_TYPE_JPEG == imgType)
format = "jpg";
else if (HGIMGFMT_TYPE_BMP == imgType)
format = "bmp";
else if (HGIMGFMT_TYPE_PNG == imgType)
format = "png";
else if (HGIMGFMT_TYPE_TIFF == imgType)
format = "tif";
else if (HGIMGFMT_TYPE_PDF == imgType)
format = "pdf";
else if (HGIMGFMT_TYPE_OFD == imgType)
format = "ofd";
if (format.empty())
return - 1;
HGByte* imgData = NULL;
HGUInt imgSize = 0;
FILE* file = fopen(imagePath.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
imgSize = ftell(file);
fseek(file, 0, SEEK_SET);
if (imgSize > 0)
{
imgData = new HGByte[imgSize];
HGUInt readLen = (HGUInt)fread(imgData, 1, imgSize, file);
if (readLen != imgSize)
{
delete[] imgData;
imgData = NULL;
imgSize = 0;
}
}
fclose(file);
}
std::string imgFormat;
HGByte* imgData = LoadImageFromPath(imagePath.c_str(), imgSize, imgFormat);
if (NULL == imgData)
return -1;
HGUInt thumbSize = 0;
HGByte* thumbData = LoadThumbFromPath(imagePath.c_str(), thumbSize);
if (NULL == thumbData)
{
delete[] imgData;
return -1;
}
std::vector<std::pair<int, int>> tables;
char** result = NULL;
@ -1414,6 +1448,7 @@ namespace ver_2
insertPos = 0;
else if (0 != insertPos)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
@ -1424,6 +1459,7 @@ namespace ver_2
insertPos = tables[tables.size() - 1].second + 1;
else if (insertPos > tables[tables.size() - 1].second + 1)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
@ -1440,17 +1476,20 @@ namespace ver_2
}
}
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image) values ('%d', '%s', '%s', ?)", m_currBatchId.c_str(),
insertPos, format.c_str(), imageTag.c_str());
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image, thumb) values ('%d', '%s', '%s', ?, ?)", m_currBatchId.c_str(),
insertPos, imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
assert(0 == ret);
delete[] thumbData;
delete[] imgData;
errInfo.clear();
return 0;
@ -1463,43 +1502,20 @@ namespace ver_2
if (NULL == m_sqlite || imageBase64.empty() || insertPos < -1)
return -1;
size_t pos = imageBase64.find(',');
if (std::string::npos == pos)
return -1;
std::string format;
std::string prefix = imageBase64.substr(0, pos + 1);
if ("data:image/jpeg;base64," == prefix)
format = "jpg";
else if ("data:image/bmp;base64," == prefix)
format = "bmp";
else if ("data:image/png;base64," == prefix)
format = "png";
else if ("data:image/tiff;base64," == prefix)
format = "tif";
else if ("data:image/pdf;base64," == prefix)
format = "pdf";
else if ("data:image/ofd;base64," == prefix)
format = "ofd";
if (format.empty())
return -1;
HGByte* imgData = NULL;
HGSize imgSize = 0;
const HGChar *base64Data = imageBase64.c_str() + pos + 1;
HGSize base64Size = (HGSize)strlen(base64Data);
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &imgSize);
if (0 != imgSize)
{
imgData = new HGByte[imgSize];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, imgData, &imgSize);
}
HGUInt imgSize = 0;
std::string imgFormat;
HGByte* imgData = LoadImageFromBase64(imageBase64.c_str(), imgSize, imgFormat);
if (NULL == imgData)
return -1;
HGUInt thumbSize = 0;
HGByte* thumbData = LoadThumbFromBase64(imageBase64.c_str(), thumbSize);
if (NULL == thumbData)
{
delete[] imgData;
return -1;
}
std::vector<std::pair<int, int>> tables;
char** result = NULL;
@ -1536,6 +1552,7 @@ namespace ver_2
insertPos = 0;
else if (0 != insertPos)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
@ -1546,6 +1563,7 @@ namespace ver_2
insertPos = tables[tables.size() - 1].second + 1;
else if (insertPos > tables[tables.size() - 1].second + 1)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
@ -1562,17 +1580,20 @@ namespace ver_2
}
}
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image) values ('%d', '%s', '%s', ?)", m_currBatchId.c_str(),
insertPos, format.c_str(), imageTag.c_str());
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image, thumb) values ('%d', '%s', '%s', ?, ?)", m_currBatchId.c_str(),
insertPos, imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
assert(0 == ret);
delete[] thumbData;
delete[] imgData;
errInfo.clear();
return 0;
@ -1774,43 +1795,20 @@ namespace ver_2
if (NULL == m_sqlite || imageIndex < 0 || imageBase64.empty())
return -1;
size_t pos = imageBase64.find(',');
if (std::string::npos == pos)
return -1;
std::string format;
std::string prefix = imageBase64.substr(0, pos + 1);
if ("data:image/jpeg;base64," == prefix)
format = "jpg";
else if ("data:image/bmp;base64," == prefix)
format = "bmp";
else if ("data:image/png;base64," == prefix)
format = "png";
else if ("data:image/tiff;base64," == prefix)
format = "tif";
else if ("data:image/pdf;base64," == prefix)
format = "pdf";
else if ("data:image/ofd;base64," == prefix)
format = "ofd";
if (format.empty())
return -1;
HGByte* imgData = NULL;
HGSize imgSize = 0;
const HGChar* base64Data = imageBase64.c_str() + pos + 1;
HGSize base64Size = (HGSize)strlen(base64Data);
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &imgSize);
if (0 != imgSize)
{
imgData = new HGByte[imgSize];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, imgData, &imgSize);
}
HGUInt imgSize = 0;
std::string imgFormat;
HGByte* imgData = LoadImageFromBase64(imageBase64.c_str(), imgSize, imgFormat);
if (NULL == imgData)
return -1;
HGUInt thumbSize = 0;
HGByte* thumbData = LoadThumbFromBase64(imageBase64.c_str(), thumbSize);
if (NULL == thumbData)
{
delete[] imgData;
return -1;
}
std::vector<std::pair<int, int>> tables;
char** result = NULL;
@ -1853,21 +1851,25 @@ namespace ver_2
if (!find)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
sprintf(sql, "update 'table_%s' set format = '%s', image = ? where idx = '%d'",
m_currBatchId.c_str(), format.c_str(), imageIndex);
sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where idx = '%d'",
m_currBatchId.c_str(), imgFormat.c_str(), imageIndex);
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
assert(0 == ret);
delete[] thumbData;
delete[] imgData;
errInfo.clear();
return 0;
@ -1880,56 +1882,20 @@ namespace ver_2
if (NULL == m_sqlite || imageIndex < 0 || imagePath.empty())
return -1;
HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType);
if (0 == imgType)
return -1;
std::string format;
if (HGIMGFMT_TYPE_JPEG == imgType)
format = "jpg";
else if (HGIMGFMT_TYPE_BMP == imgType)
format = "bmp";
else if (HGIMGFMT_TYPE_PNG == imgType)
format = "png";
else if (HGIMGFMT_TYPE_TIFF == imgType)
format = "tif";
else if (HGIMGFMT_TYPE_PDF == imgType)
format = "pdf";
else if (HGIMGFMT_TYPE_OFD == imgType)
format = "ofd";
if (format.empty())
return -1;
HGByte* imgData = NULL;
HGUInt imgSize = 0;
FILE* file = fopen(imagePath.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
imgSize = ftell(file);
fseek(file, 0, SEEK_SET);
if (imgSize > 0)
{
imgData = new HGByte[imgSize];
HGUInt readLen = (HGUInt)fread(imgData, 1, imgSize, file);
if (readLen != imgSize)
{
delete[] imgData;
imgData = NULL;
imgSize = 0;
}
}
fclose(file);
}
std::string imgFormat;
HGByte* imgData = LoadImageFromPath(imagePath.c_str(), imgSize, imgFormat);
if (NULL == imgData)
return -1;
HGUInt thumbSize = 0;
HGByte* thumbData = LoadThumbFromPath(imagePath.c_str(), thumbSize);
if (NULL == thumbData)
{
delete[] imgData;
return -1;
}
std::vector<std::pair<int, int>> tables;
char** result = NULL;
@ -1972,21 +1938,25 @@ namespace ver_2
if (!find)
{
delete[] thumbData;
delete[] imgData;
return -1;
}
sprintf(sql, "update 'table_%s' set format = '%s', image = ? where idx = '%d'",
m_currBatchId.c_str(), format.c_str(), imageIndex);
sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where idx = '%d'",
m_currBatchId.c_str(), imgFormat.c_str(), imageIndex);
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
assert(0 == ret);
delete[] thumbData;
delete[] imgData;
errInfo.clear();
return 0;
@ -2787,7 +2757,7 @@ namespace ver_2
assert(NULL != hdev);
devParam.Reset();
int ret = 0;
HGUInt mask = 0;
SANE_Int num_dev_options = 0;
sane_control_option(hdev, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
@ -2815,6 +2785,7 @@ namespace ver_2
}
devParam.colorMode = value;
mask |= DeviceParam::colorModeMask;
}
else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title))
{
@ -2829,6 +2800,7 @@ namespace ver_2
}
devParam.pageMode = value;
mask |= DeviceParam::pageModeMask;
}
else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title))
{
@ -2843,6 +2815,7 @@ namespace ver_2
}
devParam.paperSize = value;
mask |= DeviceParam::paperSizeMask;
}
}
else if (SANE_TYPE_INT == desp->type)
@ -2862,6 +2835,7 @@ namespace ver_2
}
devParam.resolution = value;
mask |= DeviceParam::resolutionMask;
}
else if (0 == strcmp(OPTION_TITLE_LDZ, desp->title))
{
@ -2872,6 +2846,7 @@ namespace ver_2
}
devParam.brightness = value;
mask |= DeviceParam::brightnessMask;
}
else if (0 == strcmp(OPTION_TITLE_DBD, desp->title))
{
@ -2882,6 +2857,7 @@ namespace ver_2
}
devParam.contrast = value;
mask |= DeviceParam::contrastMask;
}
}
else if (SANE_TYPE_FIXED == desp->type)
@ -2898,10 +2874,12 @@ namespace ver_2
}
devParam.gamma = SANE_UNFIX(value);
mask |= DeviceParam::gammaMask;
}
else if (0 == strcmp(OPTION_TITLE_SMQYZCmm, desp->title))
{
devParam.paperCutLeft = SANE_UNFIX(value);
mask |= DeviceParam::paperCutLeftMask;
}
else if (0 == strcmp(OPTION_TITLE_SMQYYCmm, desp->title))
{
@ -2911,10 +2889,12 @@ namespace ver_2
}
devParam.paperCutRight = SANE_UNFIX(value);
mask |= DeviceParam::paperCutRightMask;
}
else if (0 == strcmp(OPTION_TITLE_SMQYSCmm, desp->title))
{
devParam.paperCutTop = SANE_UNFIX(value);
mask |= DeviceParam::paperCutTopMask;
}
else if (0 == strcmp(OPTION_TITLE_SMQYXCmm, desp->title))
{
@ -2924,6 +2904,7 @@ namespace ver_2
}
devParam.paperCutBottom = SANE_UNFIX(value);
mask |= DeviceParam::paperCutBottomMask;
}
}
else if (SANE_TYPE_BOOL == desp->type)
@ -2934,18 +2915,241 @@ namespace ver_2
if (0 == strcmp(OPTION_TITLE_ZDJP, desp->title))
{
devParam.autoCrop = (bool)value;
mask |= DeviceParam::autoCropMask;
}
else if (0 == strcmp(OPTION_TITLE_ZDYSMQY, desp->title))
{
devParam.paperCutEnabled = (bool)value;
mask |= DeviceParam::paperCutEnabledMask;
}
}
}
return ret;
HGUInt stdMask = DeviceParam::colorModeMask | DeviceParam::pageModeMask | DeviceParam::resolutionMask | DeviceParam::brightnessMask
| DeviceParam::contrastMask | DeviceParam::gammaMask | DeviceParam::paperSizeMask | DeviceParam::paperCutEnabledMask
| DeviceParam::paperCutLeftMask | DeviceParam::paperCutTopMask | DeviceParam::paperCutRightMask | DeviceParam::paperCutBottomMask
| DeviceParam::autoCropMask;
if (mask != stdMask)
{
return -1;
}
return 0;
}
void ManagerV2::GetBase64(const HGByte* data, HGUInt size, std::string& base64)
HGByte* ManagerV2::LoadImageFromPath(const std::string& imagePath, HGUInt& size, std::string& format)
{
size = 0;
format.clear();
HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType);
if (0 == imgType)
return NULL;
std::string imgFormat;
if (HGIMGFMT_TYPE_JPEG == imgType)
imgFormat = "jpg";
else if (HGIMGFMT_TYPE_BMP == imgType)
imgFormat = "bmp";
else if (HGIMGFMT_TYPE_PNG == imgType)
imgFormat = "png";
else if (HGIMGFMT_TYPE_TIFF == imgType)
imgFormat = "tif";
else if (HGIMGFMT_TYPE_PDF == imgType)
imgFormat = "pdf";
else if (HGIMGFMT_TYPE_OFD == imgType)
imgFormat = "ofd";
if (imgFormat.empty())
return NULL;
HGByte* imgData = NULL;
HGUInt imgSize = 0;
FILE* file = fopen(imagePath.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
imgSize = ftell(file);
fseek(file, 0, SEEK_SET);
if (imgSize > 0)
{
imgData = new HGByte[imgSize];
HGUInt readLen = (HGUInt)fread(imgData, 1, imgSize, file);
if (readLen != imgSize)
{
delete[] imgData;
imgData = NULL;
imgSize = 0;
}
}
fclose(file);
}
if (NULL == imgData)
return NULL;
size = imgSize;
format = imgFormat;
return imgData;
}
HGByte* ManagerV2::LoadImageFromBase64(const std::string& imageBase64, HGUInt& size, std::string& format)
{
size = 0;
format.clear();
size_t pos = imageBase64.find(',');
if (std::string::npos == pos)
return NULL;
std::string imgFormat;
std::string prefix = imageBase64.substr(0, pos + 1);
if ("data:image/jpeg;base64," == prefix)
imgFormat = "jpg";
else if ("data:image/bmp;base64," == prefix)
imgFormat = "bmp";
else if ("data:image/png;base64," == prefix)
imgFormat = "png";
else if ("data:image/tiff;base64," == prefix)
imgFormat = "tif";
else if ("data:image/pdf;base64," == prefix)
imgFormat = "pdf";
else if ("data:image/ofd;base64," == prefix)
imgFormat = "ofd";
if (imgFormat.empty())
return NULL;
HGByte* imgData = NULL;
HGSize imgSize = 0;
const HGChar* base64Data = imageBase64.c_str() + pos + 1;
HGSize base64Size = (HGSize)strlen(base64Data);
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &imgSize);
if (0 != imgSize)
{
imgData = new HGByte[imgSize];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, imgData, &imgSize);
}
if (NULL == imgData)
return NULL;
size = imgSize;
format = imgFormat;
return imgData;
}
HGByte* ManagerV2::LoadThumbFromPath(const std::string& imagePath, HGUInt& size)
{
size = 0;
HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType);
if (0 == imgType)
return NULL;
HGImage img = NULL;
HGImgFmt_LoadImage(imagePath.c_str(), imgType, NULL, 0, HGBASE_IMGORIGIN_TOP, &img);
if (NULL == img)
return NULL;
HGImageInfo imgInfo;
HGBase_GetImageInfo(img, &imgInfo);
HGUInt width = imgInfo.width;
HGUInt height = imgInfo.height;
if (width >= height)
{
if (width > 240)
{
width = 240;
height = HGMAX((HGUInt)(240.0 * imgInfo.height / imgInfo.width + 0.5), 1);
}
}
else
{
if (height > 240)
{
height = 240;
width = HGMAX((HGUInt)(240.0 * imgInfo.width / imgInfo.height + 0.5), 1);
}
}
HGImage img2 = NULL;
HGBase_CreateImage(width, height, imgInfo.type, HGBASE_IMGORIGIN_TOP, &img2);
if (NULL == img2)
{
HGBase_DestroyImage(img);
return NULL;
}
HGImgProc_ResizeImage(img, img2, HGIMGPROC_INTERPOTYPE_NN);
HGBase_DestroyImage(img);
HGChar tmpName[256];
HGBase_GetTmpFileName(NULL, tmpName, 256);
HGResult ret = HGImgFmt_SaveImage(img2, imgType, NULL, tmpName);
HGBase_DestroyImage(img2);
if (HGBASE_ERR_OK != ret)
{
return NULL;
}
HGByte* imgData = NULL;
HGUInt imgSize = 0;
FILE* file = fopen(tmpName, "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
imgSize = ftell(file);
fseek(file, 0, SEEK_SET);
if (imgSize > 0)
{
imgData = new HGByte[imgSize];
HGUInt readLen = (HGUInt)fread(imgData, 1, imgSize, file);
if (readLen != imgSize)
{
delete[] imgData;
imgData = NULL;
imgSize = 0;
}
}
fclose(file);
}
HGBase_DeleteFile(tmpName);
if (NULL == imgData)
return NULL;
size = imgSize;
return imgData;
}
HGByte* ManagerV2::LoadThumbFromBase64(const std::string& imageBase64, HGUInt& size)
{
size = 0;
size_t pos = imageBase64.find(',');
if (std::string::npos == pos)
return NULL;
HGChar tmpName[256];
HGBase_GetTmpFileName(NULL, tmpName, 256);
if (0 != SaveBase64(imageBase64.c_str() + pos + 1, tmpName))
return NULL;
HGByte* data = LoadThumbFromPath(tmpName, size);
HGBase_DeleteFile(tmpName);
return data;
}
bool ManagerV2::SaveToBase64(const HGByte* data, HGUInt size, std::string& base64)
{
assert(NULL != data && 0 != size);
base64.clear();
@ -2957,6 +3161,26 @@ namespace ver_2
p[base64Size] = 0;
base64 = (const char*)p;
delete[] p;
return true;
}
bool ManagerV2::SaveToFile(const HGByte* data, HGUInt size, const std::string& filePath)
{
assert(NULL != data && 0 != size);
bool ret = false;
FILE* file = fopen(filePath.c_str(), "wb");
if (NULL != file)
{
size_t writeLen = fwrite(data, 1, size, file);
if (writeLen == (size_t)size)
ret = true;
fclose(file);
}
if (!ret)
HGBase_DeleteFile(filePath.c_str());
return ret;
}
int ManagerV2::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)

View File

@ -162,6 +162,13 @@ namespace ver_2
bool autoCrop;
};
struct ImageThumbInfo
{
int idx;
std::string tag;
std::string thumbBase64;
};
typedef void (*SaneEvent)(int code, const char *str, bool err, void* param);
typedef void (*SaneImageCallback)(const char* path, void* param);
@ -248,6 +255,8 @@ namespace ver_2
int GetCurrBatchId(std::string& batchId, std::string& errInfo);
// 修改批次号
int ModifyBatchId(const std::string& batchId, const std::string& newBatchId, std::string& errInfo);
// 加载图像缩略图列表
int GetImageThumbnailList(std::vector<ImageThumbInfo>& imageThumbList, std::string& errInfo);
// 获取图像数量
int GetImageCount(int& imageCount, std::string& errInfo);
// 加载图像
@ -310,7 +319,13 @@ namespace ver_2
static void RestoreDeviceParam(const std::string& devName, const DeviceParam& devParam);
static int SetParamToDevice(SANE_Handle hdev, const DeviceParam& devParam, HGUInt mask);
static int GetParamFromDevice(SANE_Handle hdev, DeviceParam& devParam);
static void GetBase64(const HGByte* data, HGUInt size, std::string &base64);
static HGByte* LoadImageFromPath(const std::string& imagePath, HGUInt& size, std::string& format);
static HGByte* LoadImageFromBase64(const std::string& imageBase64, HGUInt& size, std::string& format);
static HGByte* LoadThumbFromPath(const std::string& imagePath, HGUInt& size);
static HGByte* LoadThumbFromBase64(const std::string& imageBase64, HGUInt& size);
static bool SaveToBase64(const HGByte* data, HGUInt size, std::string& base64);
static bool SaveToFile(const HGByte* data, HGUInt size, const std::string &filePath);
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
@ -328,6 +343,7 @@ namespace ver_2
bool m_openDevice;
SANE_Handle m_devHandle;
std::string m_devName;
bool m_devParamValid;
DeviceParam m_devParam;
bool m_scanning;
HGEvent m_scanEvent;

View File

@ -152,6 +152,41 @@ namespace ver_2
return ret;
}
static std::vector<int> GetJsonIntListValue(cJSON* json, const std::string& key, bool* find = NULL)
{
std::vector<int> ret;
if (NULL != find)
*find = false;
cJSON* p = json->child;
while (NULL != p)
{
if (0 == strcmp(p->string, key.c_str()))
{
if (p->type == cJSON_Array)
{
cJSON* pEx = p->child;
while (NULL != pEx)
{
if (pEx->type == cJSON_Number)
ret.push_back(pEx->valueint);
pEx = pEx->next;
}
if (NULL != find)
*find = true;
}
break;
}
p = p->next;
}
return ret;
}
#if defined(HG_CMP_MSC)
WSUser::WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn)
#else
@ -305,6 +340,58 @@ namespace ver_2
{
ModifyBatchId(json);
}
else if ("get_image_thumbnail_list" == func)
{
GetImageThumbnailList(json);
}
else if ("get_image_count" == func)
{
GetImageCount(json);
}
else if ("load_image" == func)
{
LoadImage(json);
}
else if ("save_image" == func)
{
SaveImage(json);
}
else if ("insert_local_image" == func)
{
InsertLocalImage(json);
}
else if ("insert_image" == func)
{
InsertImage(json);
}
else if ("modify_image_tag" == func)
{
ModifyImageTag(json);
}
else if ("delete_image" == func)
{
DeleteImage(json);
}
else if ("clear_image_list" == func)
{
ClearImageList(json);
}
else if ("modify_image" == func)
{
ModifyImage(json);
}
else if ("modify_image_by_local" == func)
{
ModifyImageByLocal(json);
}
else if ("move_image" == func)
{
MoveImage(json);
}
else if ("image_book_sort" == func)
{
ImageBookSort(json);
}
cJSON_Delete(json);
}
@ -2674,4 +2761,632 @@ namespace ver_2
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::GetImageThumbnailList(cJSON* json)
{
assert(NULL != json);
std::vector<ImageThumbInfo> imageThumbList;
std::string errInfo;
int ret = GetManager()->GetImageThumbnailList(imageThumbList, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d, ";
fmt += "\"image_thumbnail_list\":%s}";
}
std::string thumbList = "[";
for (int i = 0; i < (int)imageThumbList.size(); ++i)
{
thumbList += "{\"image_tag\":\"";
thumbList += imageThumbList[i].tag;
thumbList += "\", \"image_base64\":\"";
thumbList += imageThumbList[i].thumbBase64;
thumbList += "\"}";
if (i != (int)imageThumbList.size() - 1)
{
thumbList += ", ";
}
}
thumbList += "]";
char* resp = new char[1024 + thumbList.size()];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", iden.c_str(), ret, thumbList.c_str());
else
sprintf(resp, fmt.c_str(), "get_image_thumbnail_list", ret, thumbList.c_str());
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
delete[] resp;
}
void WSUser::GetImageCount(cJSON* json)
{
assert(NULL != json);
int imageCount = 0;
std::string errInfo;
int ret = GetManager()->GetImageCount(imageCount, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d, ";
fmt += "\"image_count\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "get_image_count", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "get_image_count", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "get_image_count", iden.c_str(), ret, imageCount);
else
sprintf(resp, fmt.c_str(), "get_image_count", ret, imageCount);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::LoadImage(cJSON* json)
{
assert(NULL != json);
int imageIndex = GetJsonIntValue(json, "image_index");
std::string imageTag, imageBase64;
std::string errInfo;
int ret = GetManager()->LoadImage(imageIndex, imageTag, imageBase64, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d, ";
fmt += "\"image_tag\":\"%s\", ";
fmt += "\"image_base64\":\"%s\"}";
}
char *resp = new char [1024 + imageTag.size() + imageBase64.size()];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "load_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "load_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "load_image", iden.c_str(), ret, imageTag.c_str(), imageBase64.c_str());
else
sprintf(resp, fmt.c_str(), "load_image", ret, imageTag.c_str(), imageBase64.c_str());
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
delete[] resp;
}
void WSUser::SaveImage(cJSON* json)
{
assert(NULL != json);
int imageIndex = GetJsonIntValue(json, "image_index");
std::string imagePath;
std::string errInfo;
int ret = GetManager()->SaveImage(imageIndex, imagePath, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d, ";
fmt += "\"image_path\":\"%s\"}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "save_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "save_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "save_image", iden.c_str(), ret, StdStringToUtf8(strToJson(imagePath)).c_str());
else
sprintf(resp, fmt.c_str(), "save_image", ret, StdStringToUtf8(strToJson(imagePath)).c_str());
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::InsertLocalImage(cJSON* json)
{
assert(NULL != json);
std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path"));
bool find = false;
int insertPos = GetJsonIntValue(json, "insert_pos", &find);
if (!find)
insertPos = -1;
std::string imageTag = GetJsonStringValue(json, "image_tag");
std::string errInfo;
int ret = GetManager()->InsertLocalImage(imagePath, insertPos, imageTag, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "insert_local_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "insert_local_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "insert_local_image", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "insert_local_image", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::InsertImage(cJSON* json)
{
assert(NULL != json);
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
bool find = false;
int insertPos = GetJsonIntValue(json, "insert_pos", &find);
if (!find)
insertPos = -1;
std::string imageTag = GetJsonStringValue(json, "image_tag");
std::string errInfo;
int ret = GetManager()->InsertImage(imageBase64, insertPos, imageTag, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "insert_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "insert_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "insert_image", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "insert_image", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::ModifyImageTag(cJSON* json)
{
assert(NULL != json);
std::vector<int> imageIndexList = GetJsonIntListValue(json, "image_index_list");
std::vector<std::string> imageTagList = GetJsonStringListValue(json, "image_tag_list");
std::string errInfo;
int ret = GetManager()->ModifyImageTag(imageIndexList, imageTagList, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image_tag", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "modify_image_tag", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image_tag", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "modify_image_tag", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::DeleteImage(cJSON* json)
{
assert(NULL != json);
std::vector<int> imageIndexList = GetJsonIntListValue(json, "image_index_list");
std::string errInfo;
int ret = GetManager()->DeleteImage(imageIndexList, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "delete_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "delete_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "delete_image", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "delete_image", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::ClearImageList(cJSON* json)
{
assert(NULL != json);
std::string errInfo;
int ret = GetManager()->ClearImageList(errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "clear_image_list", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "clear_image_list", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "clear_image_list", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "clear_image_list", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::ModifyImage(cJSON* json)
{
assert(NULL != json);
int imageIndex = GetJsonIntValue(json, "image_index");
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
std::string errInfo;
int ret = GetManager()->ModifyImage(imageIndex, imageBase64, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "modify_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "modify_image", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::ModifyImageByLocal(cJSON* json)
{
assert(NULL != json);
int imageIndex = GetJsonIntValue(json, "image_index");
std::string imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path"));
std::string errInfo;
int ret = GetManager()->ModifyImageByLocal(imageIndex, imagePath, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image_by_local", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "modify_image_by_local", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "modify_image_by_local", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "modify_image_by_local", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::MoveImage(cJSON* json)
{
assert(NULL != json);
std::vector<int> imageIndexList = GetJsonIntListValue(json, "image_index_list");
bool find = false;
std::string mode = GetJsonStringValue(json, "mode", &find);
if (!find)
mode = "index";
int target = GetJsonIntValue(json, "target");
std::string errInfo;
int ret = GetManager()->MoveImage(imageIndexList, mode, target, errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "move_image", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "move_image", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "move_image", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "move_image", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
void WSUser::ImageBookSort(cJSON* json)
{
assert(NULL != json);
std::string errInfo;
int ret = GetManager()->ImageBookSort(errInfo);
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
std::string fmt;
fmt += "{\"func\":\"%s\", ";
if (findIden)
fmt += "\"iden\":\"%s\", ";
if (0 != ret)
{
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
}
else
{
fmt += "\"ret\":%d}";
}
char resp[1024];
if (0 != ret)
{
if (findIden)
sprintf(resp, fmt.c_str(), "image_book_sort", iden.c_str(), ret, StdStringToUtf8(errInfo).c_str());
else
sprintf(resp, fmt.c_str(), "image_book_sort", ret, StdStringToUtf8(errInfo).c_str());
}
else
{
if (findIden)
sprintf(resp, fmt.c_str(), "image_book_sort", iden.c_str(), ret);
else
sprintf(resp, fmt.c_str(), "image_book_sort", ret);
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
}

View File

@ -63,6 +63,19 @@ namespace ver_2
void NewBatch(cJSON* json);
void GetCurrBatchId(cJSON* json);
void ModifyBatchId(cJSON* json);
void GetImageThumbnailList(cJSON* json);
void GetImageCount(cJSON* json);
void LoadImage(cJSON* json);
void SaveImage(cJSON* json);
void InsertLocalImage(cJSON* json);
void InsertImage(cJSON* json);
void ModifyImageTag(cJSON* json);
void DeleteImage(cJSON* json);
void ClearImageList(cJSON* json);
void ModifyImage(cJSON* json);
void ModifyImageByLocal(cJSON* json);
void MoveImage(cJSON* json);
void ImageBookSort(cJSON* json);
private:
std::string m_initDeviceIden;

View File

@ -140,6 +140,14 @@
{
var myCanvas = document.getElementById("myCanvas");
myCanvas.src = message['image_base64'];
var req1 = {'func':'insert_image', 'image_base64':''};
req1.image_base64 = message['image_base64'];
socket.send(JSON.stringify(req1));
var req2 = {'func':'delete_local_file', 'file_path':''};
req2.file_path = message['image_path'];
socket.send(JSON.stringify(req2));
}
else if ("stop_scan" == message['func'])
{
@ -169,6 +177,58 @@
{
alert(msg.data);
}
else if ("get_image_thumbnail_list" == message['func'])
{
alert(msg.data);
}
else if ("get_image_count" == message['func'])
{
alert(msg.data);
}
else if ("load_image" == message['func'])
{
alert(msg.data);
}
else if ("save_image" == message['func'])
{
alert(msg.data);
}
else if ("insert_local_image" == message['func'])
{
alert(msg.data);
}
else if ("insert_image" == message['func'])
{
//alert(msg.data);
}
else if ("modify_image_tag" == message['func'])
{
alert(msg.data);
}
else if ("delete_image" == message['func'])
{
alert(msg.data);
}
else if ("clear_image_list" == message['func'])
{
alert(msg.data);
}
else if ("modify_image" == message['func'])
{
alert(msg.data);
}
else if ("modify_image_by_local" == message['func'])
{
alert(msg.data);
}
else if ("move_image" == message['func'])
{
alert(msg.data);
}
else if ("image_book_sort" == message['func'])
{
alert(msg.data);
}
}
}
}
@ -386,6 +446,93 @@
}));
}
function GetImageThumbnailList()
{
socket.send(JSON.stringify({
'func':'get_image_thumbnail_list'
}));
}
function GetImageCount()
{
socket.send(JSON.stringify({
'func':'get_image_count'
}));
}
function LoadImage()
{
socket.send(JSON.stringify({
'func':'load_image',
'image_index':0
}));
}
function SaveImage()
{
socket.send(JSON.stringify({
'func':'save_image',
'image_index':0
}));
}
function InsertLocalImage()
{
socket.send(JSON.stringify({
'func':'insert_local_image',
'image_path':'D:\\1.jpg'
}));
}
function ModifyImageTag()
{
socket.send(JSON.stringify({
'func':'modify_image_tag',
'image_index_list':[0],
'image_tag_list':['测试TAG值'],
}));
}
function DeleteImage()
{
socket.send(JSON.stringify({
'func':'delete_image',
'image_index_list':[0]
}));
}
function ClearImageList()
{
socket.send(JSON.stringify({
'func':'clear_image_list'
}));
}
function ModifyImageByLocal()
{
socket.send(JSON.stringify({
'func':'modify_image_by_local',
'image_index':0,
'image_path':'D:\\1.jpg'
}));
}
function MoveImage()
{
socket.send(JSON.stringify({
'func':'move_image',
'image_index_list':[1],
'target':0
}));
}
function ImageBookSort()
{
socket.send(JSON.stringify({
'func':'image_book_sort'
}));
}
window.onload = function()
{
var myimg = document.getElementById("myCanvas");
@ -441,6 +588,17 @@
<input type="button" value="新建批次" onclick="NewBatch()" />
<input type="button" value="获取当前批次名称" onclick="GetCurrBatchId()" />
<input type="button" value="修改批次名称" onclick="ModifyBatchId()" />
<input type="button" value="获取图像缩略图列表" onclick="GetImageThumbnailList()" />
<input type="button" value="获取图像数量" onclick="GetImageCount()" />
<input type="button" value="加载图像" onclick="LoadImage()" />
<input type="button" value="保存图像" onclick="SaveImage()" />
<input type="button" value="插入本地图像" onclick="InsertLocalImage()" />
<input type="button" value="修改图像标签" onclick="ModifyImageTag()" />
<input type="button" value="删除图像" onclick="DeleteImage()" />
<input type="button" value="清理图像列表" onclick="ClearImageList()" />
<input type="button" value="用本地图像修改图像" onclick="ModifyImageByLocal()" />
<input type="button" value="移动图像" onclick="MoveImage()" />
<input type="button" value="图像书籍排序" onclick="ImageBookSort()" />
<br />
<br />
<img id="myCanvas" width='640' height='480' style="background-color: black; float: left;"/>