websdk增加去底色、色彩调整和二值化的接口

This commit is contained in:
luoliangyi 2024-03-27 14:08:21 +08:00
parent cb43da2559
commit 193d9811ae
6 changed files with 368 additions and 0 deletions

View File

@ -1224,6 +1224,115 @@ namespace ver_2
return ret; return ret;
} }
int ManagerV2::LocalImageFadeBkColor(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo)
{
outImagePath.clear();
errInfo = "错误";
if (imagePath.empty())
return -1;
int ret = -1;
HGImage image = NULL;
HGImgFmt_LoadImage(imagePath.c_str(), 0, NULL, 0, HGBASE_IMGORIGIN_TOP, &image);
if (NULL != image)
{
HGImgFaceBkColorParam param;
param.threshold = 100;
param.offset = 0;
param.range = 40;
if (HGBASE_ERR_OK == HGImgProc_ImageFadeBkColor(image, image, &param))
{
if (0 == SaveImage(image, temp, outImagePath))
{
if (!temp)
{
m_saveFilePathList.push_back(outImagePath);
RestoreSaveFilePathList(m_saveFilePathList);
}
errInfo.clear();
ret = 0;
}
}
HGBase_DestroyImage(image);
}
return ret;
}
int ManagerV2::LocalImageAdjustColors(const std::string& imagePath, int brightness, int contrast, double gamma,
bool temp, std::string& outImagePath, std::string& errInfo)
{
outImagePath.clear();
errInfo = "错误";
if (imagePath.empty() || brightness < -255 || brightness > 255 || contrast < -127 || contrast > 127
|| gamma < 0.1 || gamma > 5.0)
return -1;
int ret = -1;
HGImage image = NULL;
HGImgFmt_LoadImage(imagePath.c_str(), 0, NULL, 0, HGBASE_IMGORIGIN_TOP, &image);
if (NULL != image)
{
if (HGBASE_ERR_OK == HGImgProc_ImageAdjustColors(image, image, brightness, contrast, (HGFloat)gamma))
{
if (0 == SaveImage(image, temp, outImagePath))
{
if (!temp)
{
m_saveFilePathList.push_back(outImagePath);
RestoreSaveFilePathList(m_saveFilePathList);
}
errInfo.clear();
ret = 0;
}
}
HGBase_DestroyImage(image);
}
return ret;
}
int ManagerV2::LocalImageBinarization(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo)
{
outImagePath.clear();
errInfo = "错误";
if (imagePath.empty())
return -1;
int ret = -1;
HGImage image = NULL;
HGImgFmt_LoadImage(imagePath.c_str(), 0, NULL, 0, HGBASE_IMGORIGIN_TOP, &image);
if (NULL != image)
{
if (HGBASE_ERR_OK == HGImgProc_ImageBinarization(image, image, HGIMGPROC_THRESHTYPE_ADAPTIVE_MEAN, 120, 51, 41))
{
if (0 == SaveImage(image, temp, outImagePath))
{
if (!temp)
{
m_saveFilePathList.push_back(outImagePath);
RestoreSaveFilePathList(m_saveFilePathList);
}
errInfo.clear();
ret = 0;
}
}
HGBase_DestroyImage(image);
}
return ret;
}
int ManagerV2::InitDevice(std::string& errInfo) int ManagerV2::InitDevice(std::string& errInfo)
{ {
errInfo = "错误"; errInfo = "错误";

View File

@ -176,6 +176,13 @@ namespace ver_2
// 裁剪图像 // 裁剪图像
int LocalImageClip(const std::string& imagePath, int x, int y, int width, int height, int LocalImageClip(const std::string& imagePath, int x, int y, int width, int height,
bool temp, std::string& outImagePath, std::string& errInfo); bool temp, std::string& outImagePath, std::string& errInfo);
// 图像去背景色
int LocalImageFadeBkColor(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo);
// 图像色彩调整
int LocalImageAdjustColors(const std::string& imagePath, int brightness, int contrast, double gamma,
bool temp, std::string& outImagePath, std::string& errInfo);
// 图像二值化
int LocalImageBinarization(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo);
// 设备初始化 // 设备初始化
int InitDevice(std::string& errInfo); int InitDevice(std::string& errInfo);

View File

@ -286,6 +286,18 @@ namespace ver_2
{ {
ImageClip(json, func); ImageClip(json, func);
} }
else if ("local_image_fade_bkcolor" == func || "base64_image_fade_bkcolor" == func || "image_fade_bkcolor" == func)
{
ImageFadeBkColor(json, func);
}
else if ("local_image_adjust_colors" == func || "base64_image_adjust_colors" == func || "image_adjust_colors" == func)
{
ImageAdjustColors(json, func);
}
else if ("local_image_binarization" == func || "base64_image_binarization" == func || "image_binarization" == func)
{
ImageBinarization(json, func);
}
else if ("upload_image" == func || "upload_local_file" == func || "upload_image_base64" == func) else if ("upload_image" == func || "upload_local_file" == func || "upload_image_base64" == func)
{ {
UploadFile(json, func); UploadFile(json, func);
@ -2223,6 +2235,243 @@ namespace ver_2
} }
} }
void WSUser::ImageFadeBkColor(cJSON* json, const std::string& func)
{
assert(NULL != json);
bool find = false;
bool localSave = GetJsonBoolValue(json, "local_save", &find);
if (!find)
localSave = true;
std::string imagePath;
if ("image_fade_bkcolor" == func)
{
int imageIndex = GetJsonIntValue(json, "image_index");
std::string errInfo2;
GetManager()->SaveImage(imageIndex, true, imagePath, errInfo2);
}
else if ("local_image_fade_bkcolor" == func)
{
imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path"));
}
else if ("base64_image_fade_bkcolor" == func)
{
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
std::string errInfo2;
GetManager()->SaveLocalImage(imageBase64, true, imagePath, errInfo2);
}
std::string outImagePath;
std::string errInfo;
int ret = GetManager()->LocalImageFadeBkColor(imagePath, !localSave, outImagePath, errInfo);
if ("image_fade_bkcolor" == func || "base64_image_fade_bkcolor" == func)
{
HGBase_DeleteFile(imagePath.c_str());
}
std::string outImageBase64;
bool getBase64 = GetJsonBoolValue(json, "get_base64");
if (0 == ret && getBase64)
{
std::string errInfo2;
GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2);
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
cJSON* retJson = cJSON_CreateObject();
if (NULL != retJson)
{
cJSON_AddItemToObject(retJson, "func", cJSON_CreateString(func.c_str()));
if (findIden)
cJSON_AddItemToObject(retJson, "iden", cJSON_CreateString(iden.c_str()));
cJSON_AddItemToObject(retJson, "ret", cJSON_CreateNumber(ret));
if (0 != ret)
cJSON_AddItemToObject(retJson, "err_info", cJSON_CreateString(StdStringToUtf8(errInfo).c_str()));
else
{
if (localSave)
cJSON_AddItemToObject(retJson, "image_path", cJSON_CreateString(StdStringToUtf8(outImagePath).c_str()));
else
HGBase_DeleteFile(outImagePath.c_str());
if (getBase64)
cJSON_AddItemToObject(retJson, "image_base64", cJSON_CreateString(outImageBase64.c_str()));
}
char* resp = cJSON_Print(retJson);
if (NULL != resp)
{
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
free(resp);
}
cJSON_Delete(retJson);
}
}
void WSUser::ImageAdjustColors(cJSON* json, const std::string& func)
{
assert(NULL != json);
int brightness = GetJsonIntValue(json, "brightness");
int contrast = GetJsonIntValue(json, "contrast");
double gamma = GetJsonDoubleValue(json, "gamma");
bool find = false;
bool localSave = GetJsonBoolValue(json, "local_save", &find);
if (!find)
localSave = true;
std::string imagePath;
if ("image_adjust_colors" == func)
{
int imageIndex = GetJsonIntValue(json, "image_index");
std::string errInfo2;
GetManager()->SaveImage(imageIndex, true, imagePath, errInfo2);
}
else if ("local_image_adjust_colors" == func)
{
imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path"));
}
else if ("base64_image_adjust_colors" == func)
{
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
std::string errInfo2;
GetManager()->SaveLocalImage(imageBase64, true, imagePath, errInfo2);
}
std::string outImagePath;
std::string errInfo;
int ret = GetManager()->LocalImageAdjustColors(imagePath, brightness, contrast, gamma, !localSave, outImagePath, errInfo);
if ("image_adjust_colors" == func || "base64_image_adjust_colors" == func)
{
HGBase_DeleteFile(imagePath.c_str());
}
std::string outImageBase64;
bool getBase64 = GetJsonBoolValue(json, "get_base64");
if (0 == ret && getBase64)
{
std::string errInfo2;
GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2);
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
cJSON* retJson = cJSON_CreateObject();
if (NULL != retJson)
{
cJSON_AddItemToObject(retJson, "func", cJSON_CreateString(func.c_str()));
if (findIden)
cJSON_AddItemToObject(retJson, "iden", cJSON_CreateString(iden.c_str()));
cJSON_AddItemToObject(retJson, "ret", cJSON_CreateNumber(ret));
if (0 != ret)
cJSON_AddItemToObject(retJson, "err_info", cJSON_CreateString(StdStringToUtf8(errInfo).c_str()));
else
{
if (localSave)
cJSON_AddItemToObject(retJson, "image_path", cJSON_CreateString(StdStringToUtf8(outImagePath).c_str()));
else
HGBase_DeleteFile(outImagePath.c_str());
if (getBase64)
cJSON_AddItemToObject(retJson, "image_base64", cJSON_CreateString(outImageBase64.c_str()));
}
char* resp = cJSON_Print(retJson);
if (NULL != resp)
{
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
free(resp);
}
cJSON_Delete(retJson);
}
}
void WSUser::ImageBinarization(cJSON* json, const std::string& func)
{
assert(NULL != json);
bool find = false;
bool localSave = GetJsonBoolValue(json, "local_save", &find);
if (!find)
localSave = true;
std::string imagePath;
if ("image_binarization" == func)
{
int imageIndex = GetJsonIntValue(json, "image_index");
std::string errInfo2;
GetManager()->SaveImage(imageIndex, true, imagePath, errInfo2);
}
else if ("local_image_binarization" == func)
{
imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path"));
}
else if ("base64_image_binarization" == func)
{
std::string imageBase64 = GetJsonStringValue(json, "image_base64");
std::string errInfo2;
GetManager()->SaveLocalImage(imageBase64, true, imagePath, errInfo2);
}
std::string outImagePath;
std::string errInfo;
int ret = GetManager()->LocalImageBinarization(imagePath, !localSave, outImagePath, errInfo);
if ("image_binarization" == func || "base64_image_binarization" == func)
{
HGBase_DeleteFile(imagePath.c_str());
}
std::string outImageBase64;
bool getBase64 = GetJsonBoolValue(json, "get_base64");
if (0 == ret && getBase64)
{
std::string errInfo2;
GetManager()->LoadLocalImage(outImagePath, outImageBase64, errInfo2);
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
cJSON* retJson = cJSON_CreateObject();
if (NULL != retJson)
{
cJSON_AddItemToObject(retJson, "func", cJSON_CreateString(func.c_str()));
if (findIden)
cJSON_AddItemToObject(retJson, "iden", cJSON_CreateString(iden.c_str()));
cJSON_AddItemToObject(retJson, "ret", cJSON_CreateNumber(ret));
if (0 != ret)
cJSON_AddItemToObject(retJson, "err_info", cJSON_CreateString(StdStringToUtf8(errInfo).c_str()));
else
{
if (localSave)
cJSON_AddItemToObject(retJson, "image_path", cJSON_CreateString(StdStringToUtf8(outImagePath).c_str()));
else
HGBase_DeleteFile(outImagePath.c_str());
if (getBase64)
cJSON_AddItemToObject(retJson, "image_base64", cJSON_CreateString(outImageBase64.c_str()));
}
char* resp = cJSON_Print(retJson);
if (NULL != resp)
{
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
free(resp);
}
cJSON_Delete(retJson);
}
}
void WSUser::InitDevice(cJSON* json) void WSUser::InitDevice(cJSON* json)
{ {
assert(NULL != json); assert(NULL != json);

View File

@ -50,6 +50,9 @@ namespace ver_2
void ImageDecontamination(cJSON* json, const std::string& func); void ImageDecontamination(cJSON* json, const std::string& func);
void ImageDirectionCorrect(cJSON* json, const std::string& func); void ImageDirectionCorrect(cJSON* json, const std::string& func);
void ImageClip(cJSON* json, const std::string& func); void ImageClip(cJSON* json, const std::string& func);
void ImageFadeBkColor(cJSON* json, const std::string& func);
void ImageAdjustColors(cJSON* json, const std::string& func);
void ImageBinarization(cJSON* json, const std::string& func);
void InitDevice(cJSON* json); void InitDevice(cJSON* json);
void DeinitDevice(cJSON* json); void DeinitDevice(cJSON* json);