huago-corrcet_tools/HuaGoCorrect/JsonConfig.cpp

552 lines
14 KiB
C++
Raw Normal View History

2020-08-31 08:08:50 +00:00
#include "StdAfx.h"
#include "JsonConfig.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sstream>
#include "CJsonObject.hpp"
JsonConfig::JsonConfig(void)
{
}
JsonConfig::~JsonConfig(void)
{
}
/************************************************************************/
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD> */
/************************************************************************/
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(RESOLUTION,(int)(pConfigItem->Resolution));
//outJson["Config"].Add(DUPLEX,(int)(pConfigItem->Duplex));
//std::ofstream os;
//os.open(fileNames.c_str());
//os<<outJson.ToFormattedString();
//os.close();
}
void JsonConfig::WriteJsonData(const std::string fileName)
{
}
void JsonConfig::WriteJsonArrayToFile(CONFIGPARAMS cfgConfig,const std::string filename)
{
//neb::CJsonObject root;
//
//root.AddEmptySubObject("Config");
//root["Config"].AddEmptySubArray(PIXTYPE);
//root["Config"].AddEmptySubArray(PAPARSIZE);
//root["Config"].AddEmptySubArray(RESOLUTION);
//root["Config"].AddEmptySubArray(DUPLEX);
//root["Config"].AddEmptySubArray(TABINDEX);
//
//root["Config"][PIXTYPE].Add(cfgConfig.Pixtype);
//root["Config"][PAPARSIZE].Add(cfgConfig.PaperSize);
//root["Config"][RESOLUTION].Add(cfgConfig.Resolution);
//root["Config"][DUPLEX].Add(cfgConfig.Duplex);
//root["Config"][TABINDEX].Add(cfgConfig.TabIndex);
//std::ofstream os;
//os.open(filename.c_str());
//os<<root.ToFormattedString();
//os.close();
}
void JsonConfig::WriteDistorJsonToFile(std::vector<DISTORTIONPARAMS> cfgArray,const std::string filename)
{
/*neb::CJsonObject root;
root.AddEmptySubObject("Distor");
root["Distor"].AddEmptySubArray(OBJVALUE1);
root["Distor"].AddEmptySubArray(OBJVALUE2);
root["Distor"].AddEmptySubArray(OBJVALUE3);
root["Distor"].AddEmptySubArray(OBJVALUE4);
root["Distor"].AddEmptySubArray(OBJVALUE5);
root["Distor"].AddEmptySubArray(OBJVALUE6);
root["Distor"].AddEmptySubArray(LIMITOFFSET1);
root["Distor"].AddEmptySubArray(LIMITOFFSET2);
root["Distor"].AddEmptySubArray(LIMITOFFSET3);
root["Distor"].AddEmptySubArray(LIMITOFFSET4);
root["Distor"].AddEmptySubArray(LIMITOFFSET5);
root["Distor"].AddEmptySubArray(LIMITOFFSET6);
for (int i=0;i<cfgArray.size();i++)
{
root["Distor"][OBJVALUE1].Add(cfgArray[i].ObjValues[0]);
root["Distor"][OBJVALUE2].Add(cfgArray[i].ObjValues[1]);
root["Distor"][OBJVALUE3].Add(cfgArray[i].ObjValues[2]);
root["Distor"][OBJVALUE4].Add(cfgArray[i].ObjValues[3]);
root["Distor"][OBJVALUE5].Add(cfgArray[i].ObjValues[4]);
root["Distor"][OBJVALUE6].Add(cfgArray[i].ObjValues[5]);
root["Distor"][LIMITOFFSET1].Add(cfgArray[i].LimitOffset[0]);
root["Distor"][LIMITOFFSET2].Add(cfgArray[i].LimitOffset[1]);
root["Distor"][LIMITOFFSET3].Add(cfgArray[i].LimitOffset[2]);
root["Distor"][LIMITOFFSET4].Add(cfgArray[i].LimitOffset[3]);
root["Distor"][LIMITOFFSET5].Add(cfgArray[i].LimitOffset[4]);
root["Distor"][LIMITOFFSET6].Add(cfgArray[i].LimitOffset[5]);
}
std::ofstream os;
os.open(filename.c_str());
os<<root.ToFormattedString();
os.close();*/
}
void JsonConfig::WriteMacthJsonToFile(std::vector<MATCHPARAMS> cfgArray,const std::string filename)
{
/*neb::CJsonObject root;
root.AddEmptySubObject("Match");
root["Match"].AddEmptySubArray(LOADNUM);
root["Match"].AddEmptySubArray(XPOINT);
root["Match"].AddEmptySubArray(YPOINT);
root["Match"].AddEmptySubArray(PWIGHT);
root["Match"].AddEmptySubArray(PHEIGHT);
root["Match"].AddEmptySubArray(MATCHPATH);
for (int i=0;i<cfgArray.size();i++)
{
root["Match"][LOADNUM].Add(cfgArray[i].LoadNum);
root["Match"][XPOINT].Add(cfgArray[i].XPoint);
root["Match"][YPOINT].Add(cfgArray[i].YPoint);
root["Match"][PWIGHT].Add(cfgArray[i].PWight);
root["Match"][PHEIGHT].Add(cfgArray[i].PHeight);
if (cfgArray[i].MatchPath.c_str()!=NULL)
{
root["Match"][MATCHPATH].Add(cfgArray[i].MatchPath);
}
}
std::ofstream os;
os.open(filename.c_str());
os<<root.ToFormattedString();
os.close();*/
}
CONFIGPARAMS JsonConfig::ReadDefaultConfig()
{
/*TCHAR szIniFile[MAX_PATH] = { 0 };
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
_tcscat(szIniFile, HUAGAO_SCAN);
_tcscat(szIniFile, PAPER_TEST);
_tcscat(szIniFile, TEXT("\\"));
_tcscat(szIniFile, PAPER_JSON_NAME);
std::string s_default(szIniFile);
vector<CONFIGPARAMS> vc;
vc=ReadJsonArrayFromFile(s_default.c_str());
if (vc.size()!=0)
{
return vc[0];
}
return GetDefaultConfigParams();*/
return CONFIGPARAMS();
}
DISTORTIONPARAMS JsonConfig::ReadDistorConfig()
{
//TCHAR szIniFile[MAX_PATH] = { 0 };
//SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
//_tcscat(szIniFile, HUAGAO_SCAN);
//_tcscat(szIniFile, PAPER_TEST);
//_tcscat(szIniFile, TEXT("\\"));
//_tcscat(szIniFile, DISTOR_JSON_NAME);
//std::string s_default(szIniFile);
//vector<DISTORTIONPARAMS> vc;
//vc=ReadDistorJsonFromFile(s_default.c_str());
//if (vc.size()!=0)
//{
// return vc[0];
//}
//return GetDistorConfigParams();
return GetDistorConfigParams();
}
MATCHPARAMS JsonConfig::ReadMacthConfig()
{
//TCHAR szIniFile[MAX_PATH] = { 0 };
//SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);
//_tcscat(szIniFile, HUAGAO_SCAN);
//_tcscat(szIniFile, PAPER_TEST);
//_tcscat(szIniFile, TEXT("\\"));
//_tcscat(szIniFile, MATCH_JSON_NAME);
//std::string s_default(szIniFile);
//vector<MATCHPARAMS> vc;
//vc=ReadMacthJsonFromFile(s_default.c_str());
//if (vc.size()!=0)
//{
// return vc[0];
//}
//return GetMacthConfigParams();
return MATCHPARAMS();
}
bool JsonConfig::DeleteJsonFile(std::string path)
{
return (remove(path.c_str()));
}
std::vector<CONFIGPARAMS> JsonConfig::ReadJsonArrayFromFile(const std::string filename)
{
std::vector<CONFIGPARAMS> 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;
}
std::vector<DISTORTIONPARAMS> JsonConfig::ReadDistorJsonFromFile(const std::string filename)
{
std::vector<DISTORTIONPARAMS> 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=parseDistorFromString(text);
return re;
}
std::vector<MATCHPARAMS> JsonConfig::ReadMacthJsonFromFile(const std::string filename)
{
std::vector<MATCHPARAMS> 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=parseMacthFromString(text);*/
return re;
}
CONFIGPARAMS JsonConfig::GetDefaultConfigParams()
{
CONFIGPARAMS params;
params.Pixtype=1;//<2F>Ҷ<EFBFBD>
params.PaperSize=11;//A3
params.Resolution=200;//200dpi
params.Duplex=1;//˫<><CBAB>
params.TabIndex = 1;
return params;
}
DISTORTIONPARAMS JsonConfig::GetDistorConfigParams()
{
DISTORTIONPARAMS params;
for(int i = 0;i < 6;i++)
{
params.ObjValues[i] = 10;
params.LimitOffset[i] = 2;
}
return params;
}
MATCHPARAMS JsonConfig::GetMacthConfigParams()
{
MATCHPARAMS params;
params.LoadNum = 4;
params.XPoint = 10;
params.YPoint = 10;
params.PWight = 100;
params.PHeight = 100;
return params;
}
std::vector<CONFIGPARAMS> JsonConfig::parseJsonFromString(const std::string str)
{
//neb::CJsonObject root(str);
vector<CONFIGPARAMS> 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 itmRes;
root["Config"].Get(RESOLUTION,itmRes);
neb::CJsonObject itmDulpex;
root["Config"].Get(DUPLEX,itmDulpex);
neb::CJsonObject itmTabIndex;
root["Config"].Get(TABINDEX,itmTabIndex);
for (int i=0;i<size;i++)
{
CONFIGPARAMS cfp;
int i_value;
itmPixType.Get(i,i_value);
cfp.Pixtype=i_value;
itmPaparSize.Get(i,i_value);
cfp.PaperSize=i_value;
itmRes.Get(i,i_value);
cfp.Resolution=i_value;
itmDulpex.Get(i,i_value);
cfp.Duplex=i_value;
itmTabIndex.Get(i,i_value);
cfp.TabIndex=i_value;
vcConfig.push_back(cfp);
}
}
else
{
CONFIGPARAMS cfp;
int index;
root["Config"].Get(PIXTYPE,index);
cfp.Pixtype=index;
root["Config"].Get(PAPARSIZE,index);
cfp.PaperSize=index;
root["Config"].Get(RESOLUTION,index);
cfp.Resolution=index;
root["Config"].Get(DUPLEX,index);
cfp.Duplex=index;
root["Config"].Get(TABINDEX,index);
cfp.TabIndex=index;
vcConfig.push_back(cfp);
}*/
return vcConfig;
}
std::vector<DISTORTIONPARAMS> JsonConfig::parseDistorFromString(const std::string str)
{
//neb::CJsonObject root(str);
vector<DISTORTIONPARAMS> vcConfig;
//int size=0;
//neb::CJsonObject itmObjValue1;
//root["Distor"].Get(OBJVALUE1,itmObjValue1);
//size = itmObjValue1.GetArraySize();
//if (size>0)
//{
// neb::CJsonObject itmObjValue2;
// root["Distor"].Get(OBJVALUE2,itmObjValue2);
// neb::CJsonObject itmObjValue3;
// root["Distor"].Get(OBJVALUE3,itmObjValue3);
// neb::CJsonObject itmObjValue4;
// root["Distor"].Get(OBJVALUE4,itmObjValue4);
// neb::CJsonObject itmObjValue5;
// root["Distor"].Get(OBJVALUE5,itmObjValue5);
// neb::CJsonObject itmObjValue6;
// root["Distor"].Get(OBJVALUE6,itmObjValue6);
// neb::CJsonObject itmLimitOffset1;
// root["Distor"].Get(LIMITOFFSET1,itmLimitOffset1);
// neb::CJsonObject itmLimitOffset2;
// root["Distor"].Get(LIMITOFFSET2,itmLimitOffset2);
// neb::CJsonObject itmLimitOffset3;
// root["Distor"].Get(LIMITOFFSET3,itmLimitOffset3);
// neb::CJsonObject itmLimitOffset4;
// root["Distor"].Get(LIMITOFFSET4,itmLimitOffset4);
// neb::CJsonObject itmLimitOffset5;
// root["Distor"].Get(LIMITOFFSET5,itmLimitOffset5);
// neb::CJsonObject itmLimitOffset6;
// root["Distor"].Get(LIMITOFFSET6,itmLimitOffset6);
// for (int i=0;i<size;i++)
// {
// DISTORTIONPARAMS cfp;
// //float f_value;
// int i_value;
// itmObjValue1.Get(i,i_value);
// cfp.ObjValues[0]=i_value;
// itmObjValue2.Get(i,i_value);
// cfp.ObjValues[1]=i_value;
// itmObjValue3.Get(i,i_value);
// cfp.ObjValues[2]=i_value;
// itmObjValue4.Get(i,i_value);
// cfp.ObjValues[3]=i_value;
// itmObjValue5.Get(i,i_value);
// cfp.ObjValues[4]=i_value;
// itmObjValue6.Get(i,i_value);
// cfp.ObjValues[5]=i_value;
// itmLimitOffset1.Get(i,i_value);
// cfp.LimitOffset[0]=i_value;
// itmLimitOffset2.Get(i,i_value);
// cfp.LimitOffset[1]=i_value;
// itmLimitOffset3.Get(i,i_value);
// cfp.LimitOffset[2]=i_value;
// itmLimitOffset4.Get(i,i_value);
// cfp.LimitOffset[3]=i_value;
// itmLimitOffset5.Get(i,i_value);
// cfp.LimitOffset[4]=i_value;
// itmLimitOffset6.Get(i,i_value);
// cfp.LimitOffset[5]=i_value;
// vcConfig.push_back(cfp);
// }
//}
//else
//{
// DISTORTIONPARAMS cfp;
// int index;
// //float fndex;
// root["Distor"].Get(OBJVALUE1,index);
// cfp.ObjValues[0]=index;
// root["Distor"].Get(OBJVALUE2,index);
// cfp.ObjValues[1]=index;
// root["Distor"].Get(OBJVALUE3,index);
// cfp.ObjValues[2]=index;
// root["Distor"].Get(OBJVALUE4,index);
// cfp.ObjValues[3]=index;
// root["Distor"].Get(OBJVALUE5,index);
// cfp.ObjValues[4]=index;
// root["Distor"].Get(OBJVALUE6,index);
// cfp.ObjValues[5]=index;
// root["Distor"].Get(LIMITOFFSET1,index);
// cfp.LimitOffset[0]=index;
// root["Distor"].Get(LIMITOFFSET2,index);
// cfp.LimitOffset[1]=index;
// root["Distor"].Get(LIMITOFFSET3,index);
// cfp.LimitOffset[2]=index;
// root["Distor"].Get(LIMITOFFSET4,index);
// cfp.LimitOffset[3]=index;
// root["Distor"].Get(LIMITOFFSET5,index);
// cfp.LimitOffset[4]=index;
// root["Distor"].Get(LIMITOFFSET6,index);
// cfp.LimitOffset[5]=index;
// vcConfig.push_back(cfp);
//}
return vcConfig;
}
std::vector<MATCHPARAMS> JsonConfig::parseMacthFromString(const std::string str)
{
//neb::CJsonObject root(str);
vector<MATCHPARAMS> vcConfig;
/*int size=0;
neb::CJsonObject itmLoadNum;
root["Match"].Get(LOADNUM,itmLoadNum);
size=itmLoadNum.GetArraySize();
if (size>0)
{
neb::CJsonObject itmXPoint;
root["Match"].Get(XPOINT,itmXPoint);
neb::CJsonObject itmYPoint;
root["Match"].Get(YPOINT,itmYPoint);
neb::CJsonObject itmPWight;
root["Match"].Get(PWIGHT,itmPWight);
neb::CJsonObject itmPHeight;
root["Match"].Get(PHEIGHT,itmPHeight);
neb::CJsonObject itmMatchPath;
if (!root["Match"][MATCHPATH].IsEmpty())
{
root["Match"].Get(MATCHPATH,itmMatchPath);
}
for (int i=0;i<size;i++)
{
MATCHPARAMS cfp;
int i_value;
std::string s_value;
itmLoadNum.Get(i,i_value);
cfp.LoadNum=i_value;
itmXPoint.Get(i,i_value);
cfp.XPoint=i_value;
itmYPoint.Get(i,i_value);
cfp.YPoint=i_value;
itmPWight.Get(i,i_value);
cfp.PWight=i_value;
itmPHeight.Get(i,i_value);
cfp.PHeight=i_value;
if (!root["Match"][MATCHPATH].IsEmpty())
{
itmMatchPath.Get(i,s_value);
cfp.MatchPath=s_value;
}
vcConfig.push_back(cfp);
}
}
else
{
MATCHPARAMS cfp;
int index;
std::string svalue;
root["Match"].Get(LOADNUM,index);
cfp.LoadNum=index;
root["Match"].Get(XPOINT,index);
cfp.XPoint=index;
root["Match"].Get(YPOINT,index);
cfp.YPoint=index;
root["Match"].Get(PWIGHT,index);
cfp.PWight=index;
root["Match"].Get(PHEIGHT,index);
cfp.PHeight=index;
if (!root["Match"][MATCHPATH].IsEmpty())
{
root["Match"].Get(MATCHPATH,svalue);
cfp.MatchPath=svalue;
}
vcConfig.push_back(cfp);
}*/
return vcConfig;
}