diff --git a/sdk/webservice/ManagerV2.cpp b/sdk/webservice/ManagerV2.cpp index 6909df60..b60de786 100644 --- a/sdk/webservice/ManagerV2.cpp +++ b/sdk/webservice/ManagerV2.cpp @@ -799,12 +799,13 @@ namespace ver_2 return 0; } - int ManagerV2::LocalImageDeskew(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo) + int ManagerV2::LocalImageDeskew(const std::string& imagePath, bool autoCrop, bool deskew, bool fillBlank, int width, int height, + bool temp, std::string& outImagePath, std::string& errInfo) { outImagePath.clear(); errInfo = "错误"; - if (imagePath.empty()) + if (imagePath.empty() || width <= 0 || height <= 0) return -1; int ret = -1; @@ -813,7 +814,8 @@ namespace ver_2 if (NULL != image) { HGImage img = NULL; - HGImgProc_ImageAutoCrop(image, HGTRUE, HGTRUE, HGTRUE, NULL, 0, 0, 0, HGBASE_IMGORIGIN_TOP, &img); + HGImgProc_ImageAutoCrop(image, (HGBool)autoCrop, (HGBool)deskew, (HGBool)fillBlank, NULL, + (HGUInt)width, (HGUInt)height, 0, HGBASE_IMGORIGIN_TOP, &img); if (NULL != img) { if (0 == SaveImage(img, temp, outImagePath)) @@ -1075,6 +1077,76 @@ namespace ver_2 return ret; } + int ManagerV2::LocalImageDirectionCorrect(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) + { + HGImage destImage = NULL; + + HGOCRMgr ocrMgr = NULL; + HGImgProc_CreateOCRMgr(&ocrMgr); + if (NULL != ocrMgr) + { + HGImageInfo imgInfo; + HGBase_GetImageInfo(image, &imgInfo); + + HGUInt direct = 0; + HGImgProc_ImageTextDirectOCR(ocrMgr, image, &direct); + if (HGIMGPROC_OCRTEXTDIRECT_RIGHT == direct) + { + HGBase_CreateImage(imgInfo.height, imgInfo.width, imgInfo.type, imgInfo.origin, &destImage); + HGBase_ImageRotateLeft(image, destImage); + } + else if (HGIMGPROC_OCRTEXTDIRECT_LEFT == direct) + { + HGBase_CreateImage(imgInfo.height, imgInfo.width, imgInfo.type, imgInfo.origin, &destImage); + HGBase_ImageRotateRight(image, destImage); + } + else if (HGIMGPROC_OCRTEXTDIRECT_180 == direct) + { + HGBase_CreateImage(imgInfo.width, imgInfo.height, imgInfo.type, imgInfo.origin, &destImage); + HGBase_ImageRotate180(image, destImage); + } + else + { + HGBase_CloneImage(image, imgInfo.type, imgInfo.origin, &destImage); + } + + HGImgProc_DestroyOCRMgr(ocrMgr); + } + + if (NULL != destImage) + { + if (0 == SaveImage(destImage, temp, outImagePath)) + { + if (!temp) + { + m_saveFilePathList.push_back(outImagePath); + RestoreSaveFilePathList(m_saveFilePathList); + } + + errInfo.clear(); + ret = 0; + } + + HGBase_DestroyImage(destImage); + } + + HGBase_DestroyImage(image); + } + + return ret; + } + int ManagerV2::InitDevice(std::string& errInfo) { errInfo = "错误"; diff --git a/sdk/webservice/ManagerV2.h b/sdk/webservice/ManagerV2.h index 7f1f6ab0..02bf2745 100644 --- a/sdk/webservice/ManagerV2.h +++ b/sdk/webservice/ManagerV2.h @@ -165,7 +165,8 @@ namespace ver_2 int LocalMakeZipFile(const std::vector& filePathList, const std::vector& nameList, bool temp, std::string& outZipPath, std::string& errInfo); // 本地图像纠偏 - int LocalImageDeskew(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo); + int LocalImageDeskew(const std::string& imagePath, bool autoCrop, bool deskew, bool fillBlank, int width, int height, + bool temp, std::string& outImagePath, std::string& errInfo); // 本地图像添加水印 int LocalImageAddWatermark(const std::string& imagePath, const std::string& text, const std::string& textColor, int textOpacity, const std::string& textPos, int marginLeft, int marginTop, int marginRight, int marginBottom, int locationX, int locationY, const std::string& fontName, @@ -173,6 +174,26 @@ namespace ver_2 // 去污 int LocalImageDecontamination(const std::string& imagePath, const std::string mode, const std::string& color, int x, int y, int width, int height, bool temp, std::string& outImagePath, std::string& errInfo); + // 图像方向校正 + int LocalImageDirectionCorrect(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo); + // 二值化 + //int LocalImageBinarization(const std::string& imagePath, const std::string& type, int threshold, bool temp, std::string& outImagePath, std::string& errInfo); + // 通道提取 + //int LocalImageExtractChannel(const std::string& imagePath, const std::string& type, bool temp, std::string& outImagePath, std::string& errInfo); + // 颜色识别 + //int LocalImageColorRecognition(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo); + // 除色散 + //int LocalImageDispersion(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo); + // 去底色 + //int LocalImageFadeBkColor(const std::string& imagePath, bool temp, std::string& outImagePath, std::string& errInfo); + // 图像滤镜 + //int LocalImageFilter(const std::string& imagePath, const std::string& type, bool temp, std::string& outImagePath, std::string& errInfo); + // 除穿孔 + //int LocalImageOutHole(const std::string& imagePath1, const std::string& imagePath2, double borderSize, + // bool temp, std::string& outImagePath1, std::string& outImagePath2, std::string& errInfo); + // 色彩校正 + //int ImageHSVCorrect(const std::string& imagePath, const std::string& option, bool cvtGray, const std::string& color, + // bool temp, std::string& outImagePath, std::string& errInfo); // 设备初始化 int InitDevice(std::string& errInfo); diff --git a/sdk/webservice/WSUser.cpp b/sdk/webservice/WSUser.cpp index d218f598..0c9b43c8 100644 --- a/sdk/webservice/WSUser.cpp +++ b/sdk/webservice/WSUser.cpp @@ -281,6 +281,10 @@ namespace ver_2 { ImageDecontamination(json, func); } + else if ("local_image_direction_correct" == func || "base64_image_direction_correct" == func || "image_direction_correct" == func) + { + ImageDirectionCorrect(json, func); + } else if ("upload_local_file" == func) { UploadLocalFile(json); @@ -1766,7 +1770,7 @@ namespace ver_2 std::string outImagePath; std::string errInfo; - int ret = GetManager()->LocalImageDeskew(imagePath, !localSave, outImagePath, errInfo); + int ret = GetManager()->LocalImageDeskew(imagePath, true, true, true, 0, 0, !localSave, outImagePath, errInfo); if ("image_deskew" == func || "base64_image_deskew" == func) { @@ -2009,6 +2013,84 @@ namespace ver_2 } } + void WSUser::ImageDirectionCorrect(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_direction_correct" == func) + { + int imageIndex = GetJsonIntValue(json, "image_index"); + std::string errInfo2; + GetManager()->SaveImage(imageIndex, true, imagePath, errInfo2); + } + else if ("local_image_direction_correct" == func) + { + imagePath = Utf8ToStdString(GetJsonStringValue(json, "image_path")); + } + else if ("base64_image_direction_correct" == 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()->LocalImageDirectionCorrect(imagePath, !localSave, outImagePath, errInfo); + + if ("image_direction_correct" == func || "base64_image_direction_correct" == 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) { assert(NULL != json); diff --git a/sdk/webservice/WSUser.h b/sdk/webservice/WSUser.h index 798b6a26..a0be68f5 100644 --- a/sdk/webservice/WSUser.h +++ b/sdk/webservice/WSUser.h @@ -47,6 +47,7 @@ namespace ver_2 void ImageDeskew(cJSON* json, const std::string& func); void ImageAddWatermark(cJSON* json, const std::string& func); void ImageDecontamination(cJSON* json, const std::string& func); + void ImageDirectionCorrect(cJSON* json, const std::string& func); void InitDevice(cJSON* json); void DeinitDevice(cJSON* json); diff --git a/test/webservice/demo.html b/test/webservice/demo.html index 8d9edf98..86f974d7 100644 --- a/test/webservice/demo.html +++ b/test/webservice/demo.html @@ -90,6 +90,12 @@ myCanvas.src = message['image_base64']; alert(msg.data); } + else if ("local_image_direction_correct" == message['func']) + { + var myCanvas = document.getElementById("myCanvas"); + myCanvas.src = message['image_base64']; + alert(msg.data); + } else if ("init_device" == message['func']) { alert(msg.data); @@ -299,6 +305,12 @@ myCanvas.src = message['image_base64']; alert(msg.data); } + else if ("image_direction_correct" == message['func']) + { + var myCanvas = document.getElementById("myCanvas"); + myCanvas.src = message['image_base64']; + alert(msg.data); + } } } } @@ -442,6 +454,16 @@ })); } + function LocalImageDirectCorrect() + { + socket.send(JSON.stringify({ + 'func':'local_image_direction_correct', + 'image_path':'D:\\1.jpg', + 'local_save':false, + 'get_base64':true + })); + } + function InitDevice() { socket.send(JSON.stringify({ @@ -767,6 +789,16 @@ })); } + function ImageDirectCorrect() + { + socket.send(JSON.stringify({ + 'func':'image_direction_correct', + 'image_index':0, + 'local_save':false, + 'get_base64':true + })); + } + window.onload = function() { var myimg = document.getElementById("myCanvas"); @@ -808,6 +840,7 @@ + @@ -848,6 +881,7 @@ +