首页 > 代码库 > 利用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, const vector<Mat>& SrcImg_V, CvSize SubPlot, 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_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
	cvShowImage(MultiShow_WinName.c_str(), &(IplImage(Disp_Img)));
	cvWaitKey(100);
	//cvDestroyWindow(MultiShow_WinName.c_str());
}

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