#include "GscanJsonConfig.h" #include #include #include #include #include "Device/CJsonObject.hpp" GscanJsonConfig::GscanJsonConfig() { } GscanJsonConfig::~GscanJsonConfig() { } GScanCap GscanJsonConfig::GetDefaultGscancapValue() { GScanCap gcap; /*< basic page setting*/ gcap.pixtype = 2;//默认彩色 gcap.paperAlign = PaperAlign::Rot0; gcap.papertype = 0;//TwSS::NONE gcap.is_autocrop = TRUE;//默认自动裁切 gcap.is_duplex = TRUE; gcap.is_autodiscradblank_normal = FALSE; gcap.is_autodiscradblank_vince = FALSE; gcap.is_switchfrontback = TRUE; gcap.resolution_dst = 200.0f; gcap.resolution_native = 200.0f; /*< brightness setting*/ gcap.brightness = 0.0f;//默认亮度为0 gcap.contrast = 0.0f;//默认对比度为0 gcap.threshold = 128; gcap.gamma = 1.0; gcap.is_autocontrast = FALSE;//默认不自动对比度 /*< image proecss setting*/ gcap.autodescrew = TRUE;//自动纠偏 可用 gcap.enhance_color = EnchaceColor::Enhance_None;//默认不进行颜色增强 gcap.en_fold = FALSE;//默认不对折 gcap.fillbackground = FALSE;//默认不填黑框 gcap.fillhole.is_fillhole = FALSE; gcap.fillhole.fillholeratio = 10; gcap.filter = 3;//对应Filter::None gcap.multi_output_red = FALSE; gcap.sharpen = SharpenBlur::Sharpen_None; gcap.hsvcorrect = FALSE;//默认不开启答题卡除红 /*< feeder setting*/ gcap.hardwarecaps.en_doublefeed = TRUE;//默认开启双张 gcap.hardwarecaps.en_skrewdetect = TRUE;//默认开启歪斜检测 gcap.hardwarecaps.en_stapledetect = FALSE;//默认不开启订书针检测 gcap.hardwarecaps.skrewdetectlevel = 3;//默认处于3挡 gcap.hardwarecaps.capturepixtype = 2;//彩色模式 gcap.imageRotateDegree = 90.0f;//默认不进行旋转 gcap.is_autotext = FALSE; gcap.is_backrotate180 = FALSE; gcap.scannum = -1;//默认连续扫描 //gcap.scanrect = { 0 }; return gcap; } void GscanJsonConfig::SaveGscanCapConfig(const GScanCap & gcap, const std::string path) { neb::CJsonObject outJson; outJson.AddEmptySubObject("Config");//header outJson["Config"].Add(PIXTYPE, (int)gcap.pixtype); outJson["Config"].Add(PAPARSIZE, (int)(gcap.papertype)); outJson["Config"].Add(PAPERALIGN, (int)(gcap.paperAlign)); outJson["Config"].Add(AUTOCROP, (bool)(gcap.is_autocrop), false); outJson["Config"].Add(RESOLUTION, (int)(gcap.resolution_dst)); outJson["Config"].Add(DUPLEX, (bool)(gcap.is_duplex),false); outJson["Config"].Add(DISCARBLANK, (bool)(gcap.is_autodiscradblank_normal), false); outJson["Config"].Add(DISCARBLANKVINCE, (bool)(gcap.is_autodiscradblank_vince), false); //outJson["Config"].Add(DB_AREANUM, (int)(pConfigItem->DBlank_AreaNum)); //outJson["Config"].Add(DB_DEVNMAX, (int)(pConfigItem->DBlank_DevnMax)); outJson["Config"].Add(FLOD, (bool)(gcap.en_fold), false); outJson["Config"].Add(SWITCHFRONTBACK, (bool)(gcap.is_switchfrontback), false); outJson["Config"].Add(BRIGHTNESS, (float)(gcap.brightness)); outJson["Config"].Add(AUTOCONTRAST, (bool)(gcap.is_autocontrast), false); outJson["Config"].Add(CONTRAST, (float)(gcap.contrast)); outJson["Config"].Add(GAMMA_, (int)(gcap.gamma)); outJson["Config"].Add(FILTERTYPE, (int)(gcap.filter)); outJson["Config"].Add(SHARPENTYPE, (int)(gcap.sharpen)); outJson["Config"].Add(AUTODESCREW, (bool)(gcap.autodescrew), false); outJson["Config"].Add(FILLBLACK, (bool)(gcap.fillbackground), false); outJson["Config"].Add(MULTIOUTPUT, (bool)(gcap.multi_output_red), false); outJson["Config"].Add(OUTHOLE, (bool)(gcap.fillhole.is_fillhole), false); outJson["Config"].Add(OUTHOLERATIO, (int)(gcap.fillhole.fillholeratio)); outJson["Config"].Add(HSVCORRECT, (bool)(gcap.hsvcorrect)); outJson["Config"].Add(ULTRADETECT, (bool)(gcap.hardwarecaps.en_doublefeed), false); outJson["Config"].Add(BINDINGDETECT, (bool)(gcap.hardwarecaps.en_stapledetect), false); outJson["Config"].Add(SCANCOUNT, (int)(gcap.scannum)); outJson["Config"].Add(DOCORIENTATION, (float)(gcap.imageRotateDegree)); outJson["Config"].Add(AUTO_TEXT, (bool)(gcap.is_autocrop),false); outJson["Config"].Add(BACKROTATE180, (bool)(gcap.is_backrotate180), false); outJson["Config"].Add(SCREWDETECT, (bool)(gcap.hardwarecaps.en_stapledetect), false); outJson["Config"].Add(SCREWLEVEL, (int)(gcap.hardwarecaps.skrewdetectlevel)); outJson["Config"].Add(ITEMCAPTION, (string)(gcap.Caption)); outJson["Config"].Add(SAVEPATH, (string)(gcap.SavePath)); std::ofstream os; os.open(path.c_str()); os << outJson.ToFormattedString(); os.close(); } void GscanJsonConfig::WriteJsonArrayToFile(std::vector cfgArray, const std::string filename) { neb::CJsonObject root; root.AddEmptySubObject("Config"); /*< basic setting*/ root["Config"].AddEmptySubArray(PIXTYPE); root["Config"].AddEmptySubArray(PAPARSIZE); root["Config"].AddEmptySubArray(PAPERALIGN); root["Config"].AddEmptySubArray(AUTOCROP); root["Config"].AddEmptySubArray(RESOLUTION); root["Config"].AddEmptySubArray(DUPLEX); root["Config"].AddEmptySubArray(DISCARBLANK); root["Config"].AddEmptySubArray(DISCARBLANKVINCE); //root["Config"].AddEmptySubArray(DB_AREANUM); //root["Config"].AddEmptySubArray(DB_DEVNMAX); root["Config"].AddEmptySubArray(FLOD); root["Config"].AddEmptySubArray(SWITCHFRONTBACK); /*< brightness setting*/ root["Config"].AddEmptySubArray(BRIGHTNESS); root["Config"].AddEmptySubArray(AUTOCONTRAST); root["Config"].AddEmptySubArray(CONTRAST); root["Config"].AddEmptySubArray(GAMMA_); /*< image process settings*/ root["Config"].AddEmptySubArray(FILTERTYPE); root["Config"].AddEmptySubArray(ENHANCECOLOR); root["Config"].AddEmptySubArray(SHARPENTYPE); root["Config"].AddEmptySubArray(AUTODESCREW); root["Config"].AddEmptySubArray(FILLBLACK); root["Config"].AddEmptySubArray(MULTIOUTPUT); root["Config"].AddEmptySubArray(OUTHOLE); root["Config"].AddEmptySubArray(OUTHOLERATIO); root["Config"].AddEmptySubArray(HSVCORRECT); /*< feeder settings*/ root["Config"].AddEmptySubArray(ULTRADETECT); root["Config"].AddEmptySubArray(BINDINGDETECT); root["Config"].AddEmptySubArray(SCANCOUNT); root["Config"].AddEmptySubArray(DOCORIENTATION); root["Config"].AddEmptySubArray(AUTO_TEXT); root["Config"].AddEmptySubArray(BACKROTATE180); root["Config"].AddEmptySubArray(SCREWDETECT); root["Config"].AddEmptySubArray(SCREWLEVEL); /*< other settings*/ root["Config"].AddEmptySubArray(ITEMCAPTION); root["Config"].AddEmptySubArray(SAVEPATH); for (int i = 0; i < cfgArray.size(); i++) { root["Config"][PIXTYPE].Add((int)cfgArray[i].pixtype); root["Config"][PAPARSIZE].Add((int)cfgArray[i].papertype); root["Config"][PAPERALIGN].Add((int)cfgArray[i].paperAlign); root["Config"][AUTOCROP].Add(i, (bool)cfgArray[i].is_autocrop); root["Config"][RESOLUTION].Add(cfgArray[i].resolution_dst); root["Config"][DUPLEX].Add(i,(bool)cfgArray[i].is_duplex); root["Config"][DISCARBLANK].Add(i, (bool)cfgArray[i].is_autodiscradblank_normal); root["Config"][DISCARBLANKVINCE].Add(i, (bool)cfgArray[i].is_autodiscradblank_vince); //root["Config"][DB_AREANUM].Add(cfgArray[i].DBlank_AreaNum); //root["Config"][DB_DEVNMAX].Add(cfgArray[i].DBlank_DevnMax); root["Config"][FLOD].Add(i, (bool)cfgArray[i].en_fold); root["Config"][SWITCHFRONTBACK].Add(i,(bool)cfgArray[i].is_switchfrontback); root["Config"][BRIGHTNESS].Add(cfgArray[i].brightness); root["Config"][AUTOCONTRAST].Add(i, (bool)cfgArray[i].is_autocontrast); root["Config"][CONTRAST].Add(cfgArray[i].contrast); root["Config"][GAMMA_].Add(cfgArray[i].gamma); root["Config"][FILTERTYPE].Add((int)cfgArray[i].filter); root["Config"][ENHANCECOLOR].Add((int)cfgArray[i].enhance_color); root["Config"][SHARPENTYPE].Add((int)cfgArray[i].sharpen); root["Config"][AUTODESCREW].Add(i, (bool)cfgArray[i].autodescrew); root["Config"][FILLBLACK].Add(i, (bool)cfgArray[i].fillbackground); root["Config"][MULTIOUTPUT].Add(i, (bool)cfgArray[i].multi_output_red); root["Config"][OUTHOLE].Add(i, (bool)cfgArray[i].fillhole.is_fillhole); root["Config"][OUTHOLERATIO].Add(cfgArray[i].fillhole.fillholeratio); root["Config"][HSVCORRECT].Add(i, (bool)cfgArray[i].hsvcorrect); root["Config"][ULTRADETECT].Add(i, (bool)cfgArray[i].hardwarecaps.en_doublefeed); root["Config"][BINDINGDETECT].Add(i, (bool)cfgArray[i].hardwarecaps.en_stapledetect); root["Config"][SCANCOUNT].Add((int)cfgArray[i].scannum); root["Config"][DOCORIENTATION].Add(cfgArray[i].imageRotateDegree); root["Config"][AUTO_TEXT].Add(i, (bool)cfgArray[i].is_autotext); root["Config"][BACKROTATE180].Add(i, (bool)cfgArray[i].is_backrotate180); root["Config"][SCREWDETECT].Add(i, (bool)cfgArray[i].hardwarecaps.en_skrewdetect); root["Config"][SCREWLEVEL].Add(cfgArray[i].hardwarecaps.skrewdetectlevel); if (cfgArray[i].Caption.c_str() != NULL) { root["Config"][ITEMCAPTION].Add(cfgArray[i].Caption); } if (cfgArray[i].SavePath.c_str() != NULL) { root["Config"][SAVEPATH].Add(cfgArray[i].SavePath); } } std::ofstream os; os.open(filename.c_str()); os << root.ToFormattedString(); os.close(); } GScanCap GscanJsonConfig::ReadGscanCap() { TCHAR szIniFile[MAX_PATH] = { 0 }; SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE); _tcscat(szIniFile, HUAGAO_SCAN); _tcscat(szIniFile, TWAIN_INIPATH); _tcscat(szIniFile, TEXT("\\")); std::string dirpath = TCHAR2STRING(szIniFile); bool is_exist = isFolderExist((char*)dirpath.c_str()); if (!is_exist) { createDirectory((char*)dirpath.c_str()); _tcscat(szIniFile, TWAIN_JSON_NAME); std::string dstpath = TCHAR2STRING(szIniFile); vector vc; vc.push_back(GetDefaultGscancapValue()); WriteJsonArrayToFile(vc, dstpath); return vc[0]; } _tcscat(szIniFile, TWAIN_JSON_NAME); std::string s_default = TCHAR2STRING(szIniFile); vector vc; vc = ReadGscanCapsVector(s_default.c_str()); if (vc.size() != 0) { return vc[0]; } return GetDefaultGscancapValue(); } std::vector GscanJsonConfig::ReadGscanCapsVector(const std::string gcapsfilepath) { std::vector re; FILE* file = fopen(gcapsfilepath.c_str(), "rb"); if (!file) { return re; } fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); std::string text; char* buffer = new char[size + 1]; buffer[size] = 0; if (!fread(buffer, 1, size, file) == (unsigned long)size) { return re; } text = buffer; fclose(file); delete[]buffer; re = parseJsonFromString(text); return re; } bool GscanJsonConfig::DeleteJsonFile(std::string path) { return remove(path.c_str()); } std::vector GscanJsonConfig::parseJsonFromString(const std::string str) { neb::CJsonObject root(str); vector vcConfig; int size = 0; neb::CJsonObject itmPaparSize; root["Config"].Get(PAPARSIZE, itmPaparSize); size = itmPaparSize.GetArraySize(); if (size > 0) { neb::CJsonObject itmPixType; root["Config"].Get(PIXTYPE, itmPixType); neb::CJsonObject itmAutoCrop; root["Config"].Get(AUTOCROP, itmAutoCrop); neb::CJsonObject itmPaperAlign; root["Config"].Get(PAPERALIGN, itmPaperAlign); neb::CJsonObject itmRes; root["Config"].Get(RESOLUTION, itmRes); neb::CJsonObject itmDulpex; root["Config"].Get(DUPLEX, itmDulpex); neb::CJsonObject itmDiscardBlk; root["Config"].Get(DISCARBLANK, itmDiscardBlk); neb::CJsonObject itmDiscardBlkVince; root["Config"].Get(DISCARBLANKVINCE, itmDiscardBlkVince); neb::CJsonObject itmDBAreaNum; root["Config"].Get(DB_AREANUM, itmDBAreaNum); neb::CJsonObject itmDBDevnMax; root["Config"].Get(DB_DEVNMAX, itmDBDevnMax); neb::CJsonObject itmEnFlod; root["Config"].Get(FLOD, itmEnFlod); neb::CJsonObject itmEnSwitchFrontBack; root["Config"].Get(SWITCHFRONTBACK, itmEnSwitchFrontBack); neb::CJsonObject itmBrtnes; root["Config"].Get(BRIGHTNESS, itmBrtnes); neb::CJsonObject itmAutoCrnt; root["Config"].Get(AUTOCONTRAST, itmAutoCrnt); neb::CJsonObject itmContrast; root["Config"].Get(CONTRAST, itmContrast); neb::CJsonObject itmGamma; root["Config"].Get(GAMMA_, itmGamma); neb::CJsonObject itmFilter; root["Config"].Get(FILTERTYPE, itmFilter); neb::CJsonObject itmEnchace; root["Config"].Get(ENHANCECOLOR, itmEnchace); neb::CJsonObject itmSharpen; root["Config"].Get(SHARPENTYPE, itmSharpen); neb::CJsonObject itmAutoDescrew; root["Config"].Get(AUTODESCREW, itmAutoDescrew); neb::CJsonObject itmFillBlack; root["Config"].Get(FILLBLACK, itmFillBlack); neb::CJsonObject itmMultiOutput; root["Config"].Get(MULTIOUTPUT, itmMultiOutput); neb::CJsonObject itmOutHole; root["Config"].Get(OUTHOLE, itmOutHole); neb::CJsonObject itmOutHoleRatio; root["Config"].Get(OUTHOLERATIO, itmOutHoleRatio); neb::CJsonObject itmHsvCorrect; root["Config"].Get(HSVCORRECT, itmHsvCorrect); neb::CJsonObject itmUltDetect; root["Config"].Get(ULTRADETECT, itmUltDetect); neb::CJsonObject itmBingdingDetect; root["Config"].Get(BINDINGDETECT, itmBingdingDetect); neb::CJsonObject itmScanCount; root["Config"].Get(SCANCOUNT, itmScanCount); neb::CJsonObject itmDocOrientation; root["Config"].Get(DOCORIENTATION, itmDocOrientation); neb::CJsonObject itmAutotext; root["Config"].Get(AUTO_TEXT, itmAutotext); neb::CJsonObject itmBackRotate; root["Config"].Get(BACKROTATE180, itmBackRotate); neb::CJsonObject itmScrewDetct; root["Config"].Get(SCREWDETECT, itmScrewDetct); neb::CJsonObject itmScrewLevel; root["Config"].Get(SCREWLEVEL, itmScrewLevel); neb::CJsonObject itmCaption; if (!root["Config"][ITEMCAPTION].IsEmpty()) { root["Config"].Get(ITEMCAPTION, itmCaption); } neb::CJsonObject itmSavePtah; if (!root["Config"][SAVEPATH].IsEmpty()) { root["Config"].Get(SAVEPATH, itmSavePtah); } for (int i = 0; i < size; i++) { GScanCap cfp; int i_value; double f_value; bool b_value; std::string s_value; itmPixType.Get(i, i_value); cfp.pixtype = i_value; itmPaparSize.Get(i, i_value); cfp.papertype = (byte)i_value; itmPaperAlign.Get(i, i_value); cfp.paperAlign = (PaperAlign)i_value; itmAutoCrop.Get(i, b_value); cfp.is_autocrop = b_value ? 1 : 0; itmRes.Get(i, i_value); cfp.resolution_dst = i_value; itmDulpex.Get(i, b_value); cfp.is_duplex = b_value ? 1 : 0; itmDiscardBlk.Get(i, b_value); cfp.is_autodiscradblank_normal = b_value ? 1 : 0; itmDiscardBlkVince.Get(i, b_value); cfp.is_autodiscradblank_vince = b_value ? 1 : 0; //itmDBAreaNum.Get(i, i_value); //cfp.DBlank_AreaNum = i_value; //itmDBDevnMax.Get(i, i_value); //cfp.DBlank_DevnMax = i_value; itmEnFlod.Get(i, b_value); cfp.en_fold = b_value ? 1 : 0; itmEnSwitchFrontBack.Get(i, b_value); cfp.is_switchfrontback = b_value ? 1 : 0; itmBrtnes.Get(i, f_value); cfp.brightness = f_value; itmAutoCrnt.Get(i, b_value); cfp.is_autocontrast = b_value ? 1 : 0; itmContrast.Get(i, i_value); cfp.contrast = i_value; itmGamma.Get(i, i_value); cfp.gamma = i_value; itmFilter.Get(i, i_value); cfp.filter = i_value; itmEnchace.Get(i, i_value); cfp.enhance_color = i_value; itmSharpen.Get(i, i_value); cfp.sharpen = i_value; itmAutoDescrew.Get(i, b_value); cfp.autodescrew = b_value ? 1 : 0; itmFillBlack.Get(i, b_value); cfp.fillbackground = b_value ? 1 : 0; itmMultiOutput.Get(i, b_value); cfp.multi_output_red = b_value ? 1 : 0; itmOutHole.Get(i, b_value); cfp.fillhole.is_fillhole = b_value ? 1 : 0; itmOutHoleRatio.Get(i, i_value); cfp.fillhole.fillholeratio = i_value; itmHsvCorrect.Get(i, b_value); cfp.hsvcorrect = b_value ? 1 : 0; itmUltDetect.Get(i, b_value); cfp.hardwarecaps.en_doublefeed = b_value ? 1 : 0; itmBingdingDetect.Get(i, b_value); cfp.hardwarecaps.en_stapledetect = b_value ? 1 : 0; itmScanCount.Get(i, i_value); cfp.scannum = i_value; itmDocOrientation.Get(i, i_value); cfp.imageRotateDegree = i_value; itmAutotext.Get(i, b_value); cfp.is_autotext = b_value?1:0; itmBackRotate.Get(i, b_value); cfp.is_backrotate180 = b_value ? 1 : 0; itmScrewDetct.Get(i, b_value); cfp.hardwarecaps.en_skrewdetect = b_value ? 1 : 0; itmScrewLevel.Get(i, i_value); cfp.hardwarecaps.skrewdetectlevel = i_value; if (!root["Config"][ITEMCAPTION].IsEmpty()) { itmCaption.Get(i, s_value); cfp.Caption = s_value; } if (!root["Config"][SAVEPATH].IsEmpty()) { itmSavePtah.Get(i, s_value); cfp.SavePath = s_value; } vcConfig.push_back(cfp); } } else { GScanCap cfp; int index; bool bvalue; std::string svalue; root["Config"].Get(PIXTYPE, index); cfp.pixtype = index; root["Config"].Get(PAPARSIZE, index); cfp.papertype = index; root["Config"].Get(PAPERALIGN, index); cfp.paperAlign =(PaperAlign) index; root["Config"].Get(AUTOCROP, bvalue); cfp.is_autocrop = bvalue?1:0; root["Config"].Get(RESOLUTION, index); cfp.resolution_dst = index; root["Config"].Get(DUPLEX, bvalue); cfp.is_duplex = bvalue?1:0; root["Config"].Get(DISCARBLANK, bvalue); cfp.is_autodiscradblank_normal = bvalue?1:0; root["Config"].Get(DISCARBLANKVINCE, bvalue); cfp.is_autodiscradblank_vince = bvalue?1:0; //root["Config"].Get(DB_AREANUM, index); //cfp.DBlank_AreaNum = index; //root["Config"].Get(DB_DEVNMAX, index); //cfp.DBlank_DevnMax = index; root["Config"].Get(FLOD, bvalue); cfp.en_fold = bvalue?1:0; root["Config"].Get(SWITCHFRONTBACK, bvalue); cfp.is_switchfrontback = bvalue?1:0; root["Config"].Get(BRIGHTNESS, index); cfp.brightness = (float)index; root["Config"].Get(AUTOCONTRAST, bvalue); cfp.is_autocontrast = bvalue?1:0; root["Config"].Get(CONTRAST, index); cfp.contrast = (float)index; root["Config"].Get(GAMMA_, index); cfp.gamma = (float)index; root["Config"].Get(FILTERTYPE, index); cfp.filter = (byte)index; root["Config"].Get(ENHANCECOLOR, index); cfp.enhance_color =(byte) index; root["config"].Get(SHARPENTYPE, index); cfp.sharpen = (byte)index; root["Config"].Get(AUTODESCREW, bvalue); cfp.autodescrew = bvalue?1:0; root["Config"].Get(FILLBLACK, bvalue); cfp.fillbackground = bvalue?1:0; root["Config"].Get(MULTIOUTPUT, bvalue); cfp.multi_output_red = bvalue?1:0; root["Config"].Get(OUTHOLE, bvalue); cfp.fillhole.is_fillhole = bvalue?1:0; root["Config"].Get(OUTHOLERATIO, index); cfp.fillhole.fillholeratio = index; root["Config"].Get(HSVCORRECT, bvalue); cfp.hsvcorrect = bvalue ? 1 : 0; root["Config"].Get(ULTRADETECT, bvalue); cfp.hardwarecaps.en_doublefeed = bvalue?1:0; root["Config"].Get(BINDINGDETECT, bvalue); cfp.hardwarecaps.en_stapledetect = bvalue ? 1 : 0; root["Config"].Get(SCANCOUNT, index); cfp.scannum = index; root["Config"].Get(DOCORIENTATION, index); cfp.imageRotateDegree = (float)index; root["Config"].Get(AUTO_TEXT, bvalue); cfp.is_autotext = bvalue?1:0; root["Config"].Get(BACKROTATE180, bvalue); cfp.is_backrotate180 = bvalue ? 1 : 0; root["Config"].Get(SCREWDETECT, bvalue); cfp.hardwarecaps.en_skrewdetect = bvalue ? 1 : 0; root["Config"].Get(SCREWLEVEL, index); cfp.hardwarecaps.skrewdetectlevel = index; if (!root["Config"][ITEMCAPTION].IsEmpty()) { root["Config"].Get(ITEMCAPTION, svalue); cfp.Caption = svalue; } if (!root["Config"][SAVEPATH].IsEmpty()) { root["Config"].Get(SAVEPATH, svalue); cfp.SavePath = svalue; } vcConfig.push_back(cfp); } return vcConfig; }