利用jsoncpp+curl+opencv从服务器上解析到下载到显示图片

前端之家收集整理的这篇文章主要介绍了利用jsoncpp+curl+opencv从服务器上解析到下载到显示图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include<iostream>
#include<fstream>
#include"json.h"
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void get_json_txt()
{
	system("curl \"http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08\" -o json_data.txt");
}

Json::Value get_json_array()
{
	ifstream file("json_data.txt");
	if (!file)
	{
		cout << "Open file the json_dat.txt fail!!!" << endl;
		getchar();
		return -1;
	}

	Json::Value root;
	Json::Reader reader;

	if (!reader.parse(file,root,false))
	{
		cout << "Plz check your url make sure you can  contact your host" << endl;
		getchar();
		return -1;
	}

	return root;
}

vector<string> get_url(Json::Value root)
{
	vector<string> pic_url;
	for (int i = 0; i < 4; ++i)
	{
		pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
	}

	//for (auto s : pic_url)
	//	cout << s << endl;

	return pic_url;
}

void MultiImage_OneWin(const std::string& MultiShow_WinName,const vector<Mat>& SrcImg_V,CvSize Subplot,CvSize ImgMax_Size);

int main()
{
	string pic1_photoId_index(" ");
	string pic2_photoId_index(" ");
	string pic3_photoId_index(" ");
	string pic4_photoId_index(" ");
	while (1)
	{

		get_json_txt();

		Json::Value root;
		root = get_json_array();
		cout << root.size();

		get_url(root);
		int index[4] = { 0,1,2,3 };

		cout << "---------------------------------------------------" << endl;
		cout << "上次图片1的id--->" << pic1_photoId_index << endl;
		cout << "上次图片2的id--->" << pic2_photoId_index << endl;
		cout << "上次图片1的id--->" << pic3_photoId_index << endl;
		cout << "上次图片2的id--->" << pic4_photoId_index << endl;
		string pic1 = root["info"][index[0]]["photoId"].asString();
		string pic2 = root["info"][index[1]]["photoId"].asString();
		string pic3 = root["info"][index[2]]["photoId"].asString();
		string pic4 = root["info"][index[3]]["photoId"].asString();

		cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
		cout << "图片id1--------->" << pic1 << endl;
		cout << "图片id2--------->" << pic2 << endl;
		cout << "图片id3--------->" << pic1 << endl;
		cout << "图片id4--------->" << pic2 << endl;
		cout << "---------------------------------------------------" << endl;

		//if (pic1 != pic1_photoId_index || pic2 != pic2_photoId_index || pic3 != pic3_photoId_index || pic4 != pic4_photoId_index)
		{

			pic1_photoId_index = pic1;
			pic2_photoId_index = pic2;
			pic3_photoId_index = pic3;
			pic4_photoId_index = pic4;

			vector<string>pic_url;
			pic_url = get_url(root);
			string http = root["photoServerIp"].asString();
			cout << pic_url.size() << endl;
			string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
			string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
			string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
			string command4 = "curl -o 4.jpg " + http + "/" + pic_url[3];
			cout << command1 << endl;
			cout << command2 << endl;
			cout << command3 << endl;
			cout << command4 << endl;

			system(command1.c_str());
			system(command2.c_str());
			system(command3.c_str());
			system(command4.c_str());
		}

		vector<Mat> imgs(4);
		imgs[0] = imread("1.jpg");
		imgs[1] = imread("2.jpg");
		imgs[2] = imread("3.jpg");
		imgs[3] = imread("4.jpg");
		MultiImage_OneWin("Multiple Images",imgs,cvSize(2,2),cvSize(400,280));
	}
}


void MultiImage_OneWin(const std::string& MultiShow_WinName,CvSize ImgMax_Size)
{
	//Reference : http://blog.csdn.net/yangyangyang20092010/article/details/21740373
	//Window's image
	Mat Disp_Img;
	//Width of source image
	CvSize Img_OrigSize = cvSize(SrcImg_V[0].cols,SrcImg_V[0].rows);

	//******************** Set the width for displayed image ********************//
	//Width vs height ratio of source image
	float WH_Ratio_Orig = Img_OrigSize.width / (float)Img_OrigSize.height;
	CvSize ImgDisp_Size = cvSize(100,100);
	//if (Img_OrigSize.width > ImgMax_Size.width)
	//	ImgDisp_Size = cvSize(ImgMax_Size.width,(int)ImgMax_Size.width / WH_Ratio_Orig);
	//else if (Img_OrigSize.height > ImgMax_Size.height)
	//	ImgDisp_Size = cvSize((int)ImgMax_Size.height*WH_Ratio_Orig,ImgMax_Size.height);
	//else
	ImgDisp_Size = cvSize(Img_OrigSize.width,Img_OrigSize.height);

	//******************** Check Image numbers with Subplot layout ********************//
	int Img_Num = (int)SrcImg_V.size();
	if (Img_Num > Subplot.width * Subplot.height)
	{
		cout << "Your Subplot Setting is too small !" << endl;
		exit(0);
	}

	//******************** Blank setting ********************//
	CvSize DispBlank_Edge = cvSize(80,60);
	CvSize DispBlank_Gap = cvSize(15,15);
	//******************** Size for Window ********************//
	Disp_Img.create(Size(ImgDisp_Size.width*Subplot.width + DispBlank_Edge.width + (Subplot.width - 1)*DispBlank_Gap.width,ImgDisp_Size.height*Subplot.height + DispBlank_Edge.height + (Subplot.height - 1)*DispBlank_Gap.height),CV_8UC3);
	Disp_Img.setTo(0);//Background
	//Left top position for each image
	int EdgeBlank_X = (Disp_Img.cols - (ImgDisp_Size.width*Subplot.width + (Subplot.width - 1)*DispBlank_Gap.width)) / 2;
	int EdgeBlank_Y = (Disp_Img.rows - (ImgDisp_Size.height*Subplot.height + (Subplot.height - 1)*DispBlank_Gap.height)) / 2;
	CvPoint LT_BasePos = cvPoint(EdgeBlank_X,EdgeBlank_Y);
	CvPoint LT_Pos = LT_BasePos;

	//Display all images
	for (int i = 0; i < Img_Num; i++)
	{
		//Obtain the left top position
		if ((i%Subplot.width == 0) && (LT_Pos.x != LT_BasePos.x))
		{
			LT_Pos.x = LT_BasePos.x;
			LT_Pos.y += (DispBlank_Gap.height + ImgDisp_Size.height);
		}
		//Writting each to Window's Image
		Mat imgROI = Disp_Img(Rect(LT_Pos.x,LT_Pos.y,ImgDisp_Size.width,ImgDisp_Size.height));
		resize(SrcImg_V[i],imgROI,Size(ImgDisp_Size.width,ImgDisp_Size.height));

		LT_Pos.x += (DispBlank_Gap.width + ImgDisp_Size.width);
	}

	//Get the screen size of computer
	//int Scree_W = GetSystemMetrics(SM_CXSCREEN);
	//int Scree_H = GetSystemMetrics(SM_CYSCREEN);
	int Scree_W = 1920;
	int Scree_H = 1080;

	//namedWindow("pic_viewer",CV_WINDOW_NORMAL);
	//setWindowProperty("pic_viewer",CV_WND_PROP_FULLSCREEN,CV_WINDOW_FULLSCREEN);

	cvNamedWindow(MultiShow_WinName.c_str(),CV_WINDOW_NORMAL);
	//cvMoveWindow(MultiShow_WinName.c_str(),(Scree_W - Disp_Img.cols) / 2,(Scree_H - Disp_Img.rows) / 2);//Centralize the window
	setWindowProperty(MultiShow_WinName,CV_WINDOW_FULLSCREEN);
	cvShowImage(MultiShow_WinName.c_str(),&(IplImage(Disp_Img)));
	cvWaitKey(100);
	//cvDestroyWindow(MultiShow_WinName.c_str());
}


修改版本:

/************************************************************************
*  Author : stallman chen
*    Date : 2015/01/17
* Address : pciture works  shanghai
*   Email : 275076730@qq.com
* function: Show 4 images in one window
*************************************************************************/
#include<iostream>
#include<fstream>
#include<string>
#include"json.h"
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void get_json_txt()
{
	system("curl \"http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08\" -o json_data.txt");
}

Json::Value get_json_array()
{
	ifstream file("json_data.txt",ios::out);
	if (!file)
	{
		cout << "Open file the json_dat.txt fail!!!" << endl;
		getchar();
		return -1;
	}

	Json::Value root;
	Json::Reader reader;

	if (!reader.parse(file,false))
	{
		cout << "Plz check your url make sure that you can  contact your host" << endl;
		getchar();
		return -1;
	}

	return root;
}

vector<string> get_url(Json::Value root)
{
	vector<string> pic_url;
	for (int i = 0; i < 4; ++i)
	{
		pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
	}
	//for (auto s : pic_url)
	//	cout << s << endl;
	return pic_url;
}

Mat show_4_images(const vector<Mat>& src_img)
{
	Mat image_display;
	size_t sz = src_img.size();
	CvSize src_image_size = cvSize(src_img[0].cols,src_img[0].rows);

	//CvSize image_display_size = cvSize(src_image_size.width,src_image_size.height);

	//******************** Blank setting ********************//
	CvSize DispBlank_Edge = cvSize(200,200);
	CvSize DispBlank_Gap = cvSize(200,200);

	image_display.create(Size(6934,3900),CV_8UC3);

	image_display.setTo(0);//Background
	
	if (sz >= 1)
	{
		Mat imgROI0 = image_display(Rect(250,100,src_image_size.width,src_image_size.height));
		resize(src_img[0],imgROI0,Size(2400,1800),INTER_CUBIC);
	}

	if (sz >= 2)
	{
		Mat imgROI1 = image_display(Rect(3724,src_image_size.height));
		resize(src_img[1],imgROI1,INTER_CUBIC);
	}

	if (sz >= 3)
	{
		Mat imgROI2 = image_display(Rect(650,2000,src_image_size.height));
		resize(src_img[2],imgROI2,INTER_CUBIC);
	}

	if (sz >= 4)
	{
		Mat imgROI3 = image_display(Rect(4300,src_image_size.height));
		resize(src_img[3],imgROI3,INTER_CUBIC);
	}

	return image_display;
}


int main()
{
	string pic1_photoId_index("");
	string pic2_photoId_index("");
	string pic3_photoId_index("");
	string pic4_photoId_index("");

	bool flag = false;
	int index[4] = { 0,3 };

	while (1)
	{
		get_json_txt();
		Json::Value root;
		root = get_json_array();
		int info_zize = root["info"].size();
		//cout << root.size() << endl;

		cout << info_zize << endl;
		bool flag = false;

		if (info_zize == 4)
		{
			cout << "-----------------------------------------------------" << endl;
			cout << "上次图片1的id--->" << pic1_photoId_index << endl;
			cout << "上次图片2的id--->" << pic2_photoId_index << endl;
			cout << "上次图片3的id--->" << pic3_photoId_index << endl;
			cout << "上次图片4的id--->" << pic4_photoId_index << endl;
			string pic1 = root["info"][index[0]]["photoId"].asString();
			string pic2 = root["info"][index[1]]["photoId"].asString();
			string pic3 = root["info"][index[2]]["photoId"].asString();
			string pic4 = root["info"][index[3]]["photoId"].asString();
			cout << "----------------------------------------------------" << endl;
			cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
			cout << "图片id1--------->" << pic1 << endl;
			cout << "图片id2--------->" << pic2 << endl;
			cout << "图片id3--------->" << pic3 << endl;
			cout << "图片id4--------->" << pic4 << endl;
			cout << "---------------------------------------------------" << endl;
			if (pic1 != pic1_photoId_index)
			{
				ofstream input("photoid.txt");
				input << pic1 << endl;
				input.close();
				vector<string>pic_url;
				pic_url = get_url(root);
				string http = root["photoServerIp"].asString();
				cout << pic_url.size() << endl;
				string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
				string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
				string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
				string command4 = "curl -o 4.jpg " + http + "/" + pic_url[3];
				cout << command1 << endl;
				cout << command2 << endl;
				cout << command3 << endl;
				cout << command4 << endl;
				cout << "---------------------------------------------------" << endl;
				cout << "---------------------------------------------------" << endl;
				system(command1.c_str());
				system(command2.c_str());
				system(command3.c_str());
				system(command4.c_str());

				pic1_photoId_index = pic1;
				pic2_photoId_index = pic2;
				pic3_photoId_index = pic3;
				pic4_photoId_index = pic4;
				vector<Mat> imgs;
				imgs.push_back(imread("1.jpg"));
				imgs.push_back(imread("2.jpg"));
				imgs.push_back(imread("3.jpg"));
				imgs.push_back(imread("4.jpg"));
				Mat display_img = show_4_images(imgs);
				namedWindow("pic_viewer",CV_WINDOW_NORMAL);
				setWindowProperty("pic_viewer",CV_WINDOW_FULLSCREEN);
				imshow("pic_viewer",display_img);
				waitKey(10);
			}

		}

		if (info_zize == 3)
		{
			cout << "-----------------------------------------------------" << endl;
			cout << "上次图片1的id--->" << pic1_photoId_index << endl;
			cout << "上次图片2的id--->" << pic2_photoId_index << endl;
			cout << "上次图片3的id--->" << pic3_photoId_index << endl;
			cout << "上次图片4的id--->" << pic4_photoId_index << endl;
			string pic1 = root["info"][index[0]]["photoId"].asString();
			string pic2 = root["info"][index[1]]["photoId"].asString();
			string pic3 = root["info"][index[2]]["photoId"].asString();
			string pic4 = pic4_photoId_index;
			cout << "----------------------------------------------------" << endl;
			cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
			cout << "图片id1--------->" << pic1 << endl;
			cout << "图片id2--------->" << pic2 << endl;
			cout << "图片id3--------->" << pic3 << endl;
			cout << "图片id4--------->" << pic4 << endl;
			cout << "---------------------------------------------------" << endl;
			if (pic1 != pic1_photoId_index)
			{
				ofstream input("photoid.txt");
				input << pic1 << endl;
				input.close();
				vector<string>pic_url;
				pic_url = get_url(root);
				string http = root["photoServerIp"].asString();
				cout << pic_url.size() << endl;
				string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
				string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
				string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];

				cout << command1 << endl;
				cout << command2 << endl;
				cout << command3 << endl;

				cout << "---------------------------------------------------" << endl;
				cout << "---------------------------------------------------" << endl;
				system(command1.c_str());
				system(command2.c_str());
				system(command3.c_str());

				pic1_photoId_index = pic1;
				pic2_photoId_index = pic2;
				pic3_photoId_index = pic3;

				vector<Mat> imgs;
				imgs.push_back(imread("1.jpg"));
				imgs.push_back(imread("2.jpg"));
				imgs.push_back(imread("3.jpg"));
				Mat display_img = show_4_images(imgs);
				namedWindow("pic_viewer",display_img);
				waitKey(10);
			}
			
		}

		if (info_zize == 2)
		{
			cout << "-----------------------------------------------------" << endl;
			cout << "上次图片1的id--->" << pic1_photoId_index << endl;
			cout << "上次图片2的id--->" << pic2_photoId_index << endl;
			cout << "上次图片3的id--->" << pic3_photoId_index << endl;
			cout << "上次图片4的id--->" << pic4_photoId_index << endl;
			string pic1 = root["info"][index[0]]["photoId"].asString();
			string pic2 = root["info"][index[1]]["photoId"].asString();
			string pic3 = pic4_photoId_index;
			string pic4 = pic4_photoId_index;
			cout << "----------------------------------------------------" << endl;
			cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
			cout << "图片id1--------->" << pic1 << endl;
			cout << "图片id2--------->" << pic2 << endl;
			cout << "图片id3--------->" << pic3 << endl;
			cout << "图片id4--------->" << pic4 << endl;
			cout << "---------------------------------------------------" << endl;
			if (pic1 != pic1_photoId_index)
			{
				ofstream input("photoid.txt");
				input << pic1 << endl;
				input.close();
				vector<string>pic_url;
				pic_url = get_url(root);
				string http = root["photoServerIp"].asString();
				cout << pic_url.size() << endl;

				string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
				string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
				cout << command1 << endl;
				cout << command2 << endl;
				cout << "---------------------------------------------------" << endl;
				cout << "---------------------------------------------------" << endl;

				system(command1.c_str());
				system(command2.c_str());

				pic1_photoId_index = pic1;
				pic2_photoId_index = pic2;

				vector<Mat> imgs;
				imgs.push_back(imread("1.jpg"));
				imgs.push_back(imread("2.jpg"));
				Mat display_img = show_4_images(imgs);
				namedWindow("pic_viewer",display_img);
				waitKey(10);
			}
		}

		if (info_zize == 1)
		{
			cout << "-----------------------------------------------------" << endl;
			cout << "上次图片1的id--->" << pic1_photoId_index << endl;
			cout << "上次图片2的id--->" << pic2_photoId_index << endl;
			cout << "上次图片3的id--->" << pic3_photoId_index << endl;
			cout << "上次图片4的id--->" << pic4_photoId_index << endl;
			string pic1 = root["info"][index[0]]["photoId"].asString();
			string pic2 = pic4_photoId_index;
			string pic3 = pic4_photoId_index;
			string pic4 = pic4_photoId_index;
			cout << "----------------------------------------------------" << endl;
			cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
			cout << "图片id1--------->" << pic1 << endl;
			cout << "图片id2--------->" << pic2 << endl;
			cout << "图片id3--------->" << pic3 << endl;
			cout << "图片id4--------->" << pic4 << endl;
			cout << "---------------------------------------------------" << endl;
			if (pic1 != pic1_photoId_index)
			{
				ofstream input("photoid.txt");
				input << pic1 << endl;
				input.close();
				vector<string>pic_url;
				pic_url = get_url(root);
				string http = root["photoServerIp"].asString();
				cout << pic_url.size() << endl;

				string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
				cout << command1 << endl;

				cout << "---------------------------------------------------" << endl;
				cout << "---------------------------------------------------" << endl;

				system(command1.c_str());

				pic1_photoId_index = pic1;

				vector<Mat> imgs;
				imgs.push_back(imread("1.jpg"));
				Mat display_img = show_4_images(imgs);
				namedWindow("pic_viewer",display_img);
				waitKey(10);
			}
		}

		if (info_zize == 0)
		{
			namedWindow("pic_viewer",CV_WINDOW_NORMAL);
			setWindowProperty("pic_viewer",CV_WINDOW_FULLSCREEN);
			Mat black = imread("result.jpg");
			imshow("pic_viewer",black);
			cvWaitKey(2);
		}
	}
}

猜你在找的Json相关文章