json文件转成img

#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;
}
 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值