利用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);
		}
	}
}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...