#include "StdAfx.h" #include "JsonConfig.h" #include #include #include #include #include "CJsonObject.hpp" JsonConfig::JsonConfig(void) { } JsonConfig::~JsonConfig(void) { } /************************************************************************/ /* 保存配置参数 */ /************************************************************************/ void JsonConfig::WriteToJson(PCONFIGPARAMS pConfigItem,const std::string fileNames,bool isConfigItem) { neb::CJsonObject outJson; outJson.AddEmptySubObject("Config");//header outJson["Config"].Add(PIXTYPE,(int)(pConfigItem->Pixtype)); outJson["Config"].Add(PAPARSIZE,(int)(pConfigItem->PaperSize)); outJson["Config"].Add(AUTOCROP,(bool)(pConfigItem->EnAutoCrop),false); outJson["Config"].Add(RESOLUTION,(int)(pConfigItem->Resolution)); outJson["Config"].Add(DUPLEX,(int)(pConfigItem->Duplex)); outJson["Config"].Add(DISCARBLANK,(bool)(pConfigItem->EnDiscardBlank),false); outJson["Config"].Add(DISCARBLANKVINCE,(bool)(pConfigItem->EnDiscardBlankVince),false); outJson["Config"].Add(DB_AREANUM, (int)(pConfigItem->DBlank_AreaNum)); outJson["Config"].Add(DB_DEVNMAX, (int)(pConfigItem->DBlank_DevnMax)); outJson["Config"].Add(FLOD, (bool)(pConfigItem->EnFlod),false); outJson["Config"].Add(SWITCHFRONTBACK, (bool)(pConfigItem->EnSwitchFrontBack), false); outJson["Config"].Add(BRIGHTNESS,(float)(pConfigItem->Brightness)); outJson["Config"].Add(AUTOCONTRAST,(bool)(pConfigItem->EnAutoContrast),false); outJson["Config"].Add(CONTRAST,(float)(pConfigItem->Contrast)); outJson["Config"].Add(GAMMA_,(int)(pConfigItem->Gamma)); outJson["Config"].Add(FILTERTYPE,(int)(pConfigItem->Filter)); outJson["Config"].Add(SHARPENTYPE, (int)(pConfigItem->Sharpen)); outJson["Config"].Add(AUTODESCREW,(bool)(pConfigItem->EnAutoDescrew),false); outJson["Config"].Add(FILLBLACK,(bool)(pConfigItem->EnFillBlack),false); outJson["Config"].Add(MULTIOUTPUT,(bool)(pConfigItem->EnMultiOutPutR),false); outJson["Config"].Add(OUTHOLE,(bool)(pConfigItem->EnOutHole),false); outJson["Config"].Add(OUTHOLERATIO,(int)(pConfigItem->OutHoleRatio)); outJson["Config"].Add(ULTRADETECT,(bool)(pConfigItem->EnUltrasonicDetect),false); outJson["Config"].Add(BINDINGDETECT,(bool)(pConfigItem->EnBindingDetect),false); outJson["Config"].Add(SCANCOUNT,(int)(pConfigItem->ScanCount)); outJson["Config"].Add(DOCORIENTATION,(int)(pConfigItem->Orentation)); outJson["Config"].Add(BACKROTATE180,(bool)(pConfigItem->EnBackRotate180),false); outJson["Config"].Add(SCREWDETECT,(bool)(pConfigItem->EnScrewDetect),false); outJson["Config"].Add(SCREWLEVEL,(int)(pConfigItem->ScrewDetectLevel)); if (isConfigItem) { outJson["Config"].Add(ITEMCAPTION,(string)(pConfigItem->Caption)); outJson["Config"].Add(SAVEPATH,(string)(pConfigItem->SavePath)); } std::ofstream os; os.open(fileNames.c_str()); os< cfgArray,const std::string filename) { //Json::StyledWriter sw; neb::CJsonObject root; root.AddEmptySubObject("Config"); root["Config"].AddEmptySubArray(PIXTYPE); root["Config"].AddEmptySubArray(PAPARSIZE); 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); root["Config"].AddEmptySubArray(BRIGHTNESS); root["Config"].AddEmptySubArray(AUTOCONTRAST); root["Config"].AddEmptySubArray(CONTRAST); root["Config"].AddEmptySubArray(GAMMA_); root["Config"].AddEmptySubArray(FILTERTYPE); 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(ULTRADETECT); root["Config"].AddEmptySubArray(BINDINGDETECT); root["Config"].AddEmptySubArray(SCANCOUNT); root["Config"].AddEmptySubArray(DOCORIENTATION); root["Config"].AddEmptySubArray(BACKROTATE180); root["Config"].AddEmptySubArray(SCREWDETECT); root["Config"].AddEmptySubArray(SCREWLEVEL); root["Config"].AddEmptySubArray(ITEMCAPTION); root["Config"].AddEmptySubArray(SAVEPATH); for (int i = 0; i < cfgArray.size(); i++) { root["Config"][PIXTYPE].Add(cfgArray[i].Pixtype); root["Config"][PAPARSIZE].Add(cfgArray[i].PaperSize); root["Config"][AUTOCROP].Add(i, cfgArray[i].EnAutoCrop); root["Config"][RESOLUTION].Add(cfgArray[i].Resolution); root["Config"][DUPLEX].Add(cfgArray[i].Duplex); root["Config"][DISCARBLANK].Add(i, cfgArray[i].EnDiscardBlank); root["Config"][DISCARBLANKVINCE].Add(i, cfgArray[i].EnDiscardBlankVince); root["Config"][DB_AREANUM].Add(cfgArray[i].DBlank_AreaNum); root["Config"][DB_DEVNMAX].Add(cfgArray[i].DBlank_DevnMax); root["Config"][FLOD].Add(i,cfgArray[i].EnFlod); root["Config"][SWITCHFRONTBACK].Add(i, cfgArray[i].EnSwitchFrontBack); root["Config"][BRIGHTNESS].Add(cfgArray[i].Brightness); root["Config"][AUTOCONTRAST].Add(i,cfgArray[i].EnAutoContrast); root["Config"][CONTRAST].Add(cfgArray[i].Contrast); root["Config"][GAMMA_].Add(cfgArray[i].Gamma); root["Config"][FILTERTYPE].Add(cfgArray[i].Filter); root["Config"][SHARPENTYPE].Add(cfgArray[i].Sharpen); root["Config"][AUTODESCREW].Add(i,cfgArray[i].EnAutoDescrew); root["Config"][FILLBLACK].Add(i,cfgArray[i].EnFillBlack); root["Config"][MULTIOUTPUT].Add(i,cfgArray[i].EnMultiOutPutR); root["Config"][OUTHOLE].Add(i,cfgArray[i].EnOutHole); root["Config"][OUTHOLERATIO].Add(cfgArray[i].OutHoleRatio); root["Config"][ULTRADETECT].Add(i,cfgArray[i].EnUltrasonicDetect); root["Config"][BINDINGDETECT].Add(i,cfgArray[i].EnBindingDetect); root["Config"][SCANCOUNT].Add(cfgArray[i].ScanCount); root["Config"][DOCORIENTATION].Add(cfgArray[i].Orentation); root["Config"][BACKROTATE180].Add(i,cfgArray[i].EnBackRotate180); root["Config"][SCREWDETECT].Add(i,cfgArray[i].EnScrewDetect); root["Config"][SCREWLEVEL].Add(cfgArray[i].ScrewDetectLevel); 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< vc; vc.push_back(GetDefaultConfigParams()); WriteJsonArrayToFile(vc, dstpath); return vc[0]; } _tcscat(szIniFile, TWAIN_JSON_NAME); std::string s_default = TCHAR2STRING(szIniFile); vector vc; vc=ReadJsonArrayFromFile(s_default.c_str()); if (vc.size()!=0) { return vc[0]; } return GetDefaultConfigParams(); } bool JsonConfig::DeleteJsonFile(std::string path) { return (remove(path.c_str())); } std::vector JsonConfig::ReadJsonArrayFromFile(const std::string filename) { std::vector re; FILE* file=fopen(filename.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; } CONFIGPARAMS JsonConfig::GetDefaultConfigParams() { CONFIGPARAMS params; params.Pixtype=1;//灰度 params.PaperSize=0;//A3 params.EnAutoCrop=FALSE;//自动裁切 params.Resolution=2;//200dpi params.Duplex=1;//双面 params.EnDiscardBlank=FALSE;//自动丢弃空白页 params.EnDiscardBlankVince=FALSE; params.DBlank_AreaNum = 8; params.DBlank_DevnMax = 200; params.EnFlod = FALSE; params.EnSwitchFrontBack = FALSE; params.Brightness=0.0f;//亮度 params.EnAutoContrast=FALSE;//自动对比度 params.Contrast=0;//对比度 params.Gamma=1.0f;//伽玛值 params.Filter=0;//除色 无 params.Sharpen = 0;//不锐化 params.EnFillBlack=FALSE;//不填黑框 params.EnAutoDescrew=TRUE;//不自动纠偏 params.EnMultiOutPutR=FALSE;//不多流输出除红 params.EnOutHole=FALSE;//不去除孔洞 params.OutHoleRatio=10;//默认值0.10 params.EnUltrasonicDetect=TRUE;//超声检测开关 开 params.EnBindingDetect=FALSE;//装订检测开关 开 params.ScanCount=-1;//扫描张数 params.Orentation=0;//旋转方向0度 params.EnBackRotate180=FALSE;//背面旋转180度 不旋转 params.EnScrewDetect=TRUE;//歪斜检测 开 params.ScrewDetectLevel=3;//歪斜检测等级 3级 return params; } std::vector JsonConfig::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 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 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 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 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