#include <stdio.h>
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <opencv2/highgui.hpp>
#include "opencv2/imgproc.hpp"
using namespace std;
int main(int argc, char* argv[])
{
#if 0
Json::Value root;
// 组装json内容
root["occupation"] = "paladin";
root["camp"] = "alliance";
root["role_id"] = 1;
// 将json内容(缩进格式)输出到文件
Json::StyledWriter writer;
ofstream os;
os.open("test.json");
os << writer.write(root);
os.close();
#endif
// 以二进制形式读取json文件内容
ifstream fin(argv[1] );
cout << argv[1] << endl;
if (!fin.is_open())
{
printf("%s(%d)\n", __FILE__, __LINE__);
cout << "open json file failed." << endl;
return -1;
}
printf("%s(%d)\n", __FILE__, __LINE__);
Json::Reader reader;
Json::Value root;
printf("%s(%d)\n", __FILE__, __LINE__);
string imageName = argv[2] ;
string strOccupation;
string strCamp;
printf("%s(%d)\n", __FILE__, __LINE__);
// 解析json内容
if (reader.parse(fin, root))
{
printf("%s(%d)\n", __FILE__, __LINE__);
cout << root.type()<< endl;
// cout << root["1345557,65c66000735b7b70"]<<endl;
//Json::Value rootsub = root["1345557,65c66000735b7b70"];
Json::Value rootsub = root[imageName.c_str()];
cout << rootsub.type()<< endl;
// imageName = root.asString();
// cout << imageName <<endl;
Json::Value rootsubImg = rootsub["img"];
cout << rootsubImg.type()<< endl;
cv::Mat dst ;
dst.create(256, 256, CV_8UC3);
int cnt=0, cnt1 = 0, cnt2 = 0;
if (rootsubImg.isArray())
{
int nArraySize = rootsubImg.size();
cnt = nArraySize;
for (int i=0; i<nArraySize; i++)
{
//cout<<rootsubImg[i].type()<<endl;
Json::Value rootsubImgIndex = rootsubImg[i];
int nIndexSize = rootsubImgIndex.size();
cnt1 = nIndexSize;
//
unsigned char *pImg = dst.ptr<unsigned char>(i);
for (int j=0; j<nIndexSize; j++)
{
Json::Value rootsubImgIndex2 = rootsubImgIndex[j];
//cout << rootsubImgIndex2.type()<<endl;
int nIndex2Size = rootsubImgIndex2.size();
cnt2 = nIndex2Size;
//cout << nIndex2Size << endl;
for (int z=0; z<nIndex2Size; z++)
{
Json::Value rootsubImgIndex3 = rootsubImgIndex2[z];
// cout << rootsubImgIndex3.type() << " "<< z<<endl;
int nIndex3Size = rootsubImgIndex3.size();
int nID = rootsubImgIndex3.asUInt();
//printf("%08x ", nID);
//
*pImg++ = nID;
}
}
// unsigned char nID = rootsubImg[i].asInt();
// printf("%02x ", nID);
}
char fileDstName[256];
snprintf(fileDstName, 255, "%s.bmp", argv[2]);
printf("%s\n", fileDstName);
cv::imwrite(fileDstName, dst);
}
cout << cnt << " "<< cnt1 << " " << cnt2;
///landmark
Json::Value rootsublmk = rootsub["lm"];
cout <<"lmk " << rootsublmk.type() << " size " << rootsublmk. size() <<endl;
ofstream os;
char fileDstName[256];
Json::StyledWriter writer;
snprintf(fileDstName, 255, "%slmk.json", argv[2]);
os.open(fileDstName);
os << writer.write(rootsublmk);
os.close();
}
printf("%s(%d)\n", __FILE__, __LINE__);
fin.close();
return 0;
}