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

前端之家收集整理的这篇文章主要介绍了利用jsoncpp+curl+opencv从服务器上解析到下载到显示图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include<iostream>
  2. #include<fstream>
  3. #include"json.h"
  4. #include "opencv2/opencv.hpp"
  5.  
  6. using namespace cv;
  7. using namespace std;
  8.  
  9. void get_json_txt()
  10. {
  11. system("curl \"http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08\" -o json_data.txt");
  12. }
  13.  
  14. Json::Value get_json_array()
  15. {
  16. ifstream file("json_data.txt");
  17. if (!file)
  18. {
  19. cout << "Open file the json_dat.txt fail!!!" << endl;
  20. getchar();
  21. return -1;
  22. }
  23.  
  24. Json::Value root;
  25. Json::Reader reader;
  26.  
  27. if (!reader.parse(file,root,false))
  28. {
  29. cout << "Plz check your url make sure you can contact your host" << endl;
  30. getchar();
  31. return -1;
  32. }
  33.  
  34. return root;
  35. }
  36.  
  37. vector<string> get_url(Json::Value root)
  38. {
  39. vector<string> pic_url;
  40. for (int i = 0; i < 4; ++i)
  41. {
  42. pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
  43. }
  44.  
  45. //for (auto s : pic_url)
  46. // cout << s << endl;
  47.  
  48. return pic_url;
  49. }
  50.  
  51. void MultiImage_OneWin(const std::string& MultiShow_WinName,const vector<Mat>& SrcImg_V,CvSize Subplot,CvSize ImgMax_Size);
  52.  
  53. int main()
  54. {
  55. string pic1_photoId_index(" ");
  56. string pic2_photoId_index(" ");
  57. string pic3_photoId_index(" ");
  58. string pic4_photoId_index(" ");
  59. while (1)
  60. {
  61.  
  62. get_json_txt();
  63.  
  64. Json::Value root;
  65. root = get_json_array();
  66. cout << root.size();
  67.  
  68. get_url(root);
  69. int index[4] = { 0,1,2,3 };
  70.  
  71. cout << "---------------------------------------------------" << endl;
  72. cout << "上次图片1的id--->" << pic1_photoId_index << endl;
  73. cout << "上次图片2的id--->" << pic2_photoId_index << endl;
  74. cout << "上次图片1的id--->" << pic3_photoId_index << endl;
  75. cout << "上次图片2的id--->" << pic4_photoId_index << endl;
  76. string pic1 = root["info"][index[0]]["photoId"].asString();
  77. string pic2 = root["info"][index[1]]["photoId"].asString();
  78. string pic3 = root["info"][index[2]]["photoId"].asString();
  79. string pic4 = root["info"][index[3]]["photoId"].asString();
  80.  
  81. cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  82. cout << "图片id1--------->" << pic1 << endl;
  83. cout << "图片id2--------->" << pic2 << endl;
  84. cout << "图片id3--------->" << pic1 << endl;
  85. cout << "图片id4--------->" << pic2 << endl;
  86. cout << "---------------------------------------------------" << endl;
  87.  
  88. //if (pic1 != pic1_photoId_index || pic2 != pic2_photoId_index || pic3 != pic3_photoId_index || pic4 != pic4_photoId_index)
  89. {
  90.  
  91. pic1_photoId_index = pic1;
  92. pic2_photoId_index = pic2;
  93. pic3_photoId_index = pic3;
  94. pic4_photoId_index = pic4;
  95.  
  96. vector<string>pic_url;
  97. pic_url = get_url(root);
  98. string http = root["photoServerIp"].asString();
  99. cout << pic_url.size() << endl;
  100. string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
  101. string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
  102. string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
  103. string command4 = "curl -o 4.jpg " + http + "/" + pic_url[3];
  104. cout << command1 << endl;
  105. cout << command2 << endl;
  106. cout << command3 << endl;
  107. cout << command4 << endl;
  108.  
  109. system(command1.c_str());
  110. system(command2.c_str());
  111. system(command3.c_str());
  112. system(command4.c_str());
  113. }
  114.  
  115. vector<Mat> imgs(4);
  116. imgs[0] = imread("1.jpg");
  117. imgs[1] = imread("2.jpg");
  118. imgs[2] = imread("3.jpg");
  119. imgs[3] = imread("4.jpg");
  120. MultiImage_OneWin("Multiple Images",imgs,cvSize(2,2),cvSize(400,280));
  121. }
  122. }
  123.  
  124.  
  125. void MultiImage_OneWin(const std::string& MultiShow_WinName,CvSize ImgMax_Size)
  126. {
  127. //Reference : http://blog.csdn.net/yangyangyang20092010/article/details/21740373
  128. //Window's image
  129. Mat Disp_Img;
  130. //Width of source image
  131. CvSize Img_OrigSize = cvSize(SrcImg_V[0].cols,SrcImg_V[0].rows);
  132.  
  133. //******************** Set the width for displayed image ********************//
  134. //Width vs height ratio of source image
  135. float WH_Ratio_Orig = Img_OrigSize.width / (float)Img_OrigSize.height;
  136. CvSize ImgDisp_Size = cvSize(100,100);
  137. //if (Img_OrigSize.width > ImgMax_Size.width)
  138. // ImgDisp_Size = cvSize(ImgMax_Size.width,(int)ImgMax_Size.width / WH_Ratio_Orig);
  139. //else if (Img_OrigSize.height > ImgMax_Size.height)
  140. // ImgDisp_Size = cvSize((int)ImgMax_Size.height*WH_Ratio_Orig,ImgMax_Size.height);
  141. //else
  142. ImgDisp_Size = cvSize(Img_OrigSize.width,Img_OrigSize.height);
  143.  
  144. //******************** Check Image numbers with Subplot layout ********************//
  145. int Img_Num = (int)SrcImg_V.size();
  146. if (Img_Num > Subplot.width * Subplot.height)
  147. {
  148. cout << "Your Subplot Setting is too small !" << endl;
  149. exit(0);
  150. }
  151.  
  152. //******************** Blank setting ********************//
  153. CvSize DispBlank_Edge = cvSize(80,60);
  154. CvSize DispBlank_Gap = cvSize(15,15);
  155. //******************** Size for Window ********************//
  156. 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);
  157. Disp_Img.setTo(0);//Background
  158. //Left top position for each image
  159. int EdgeBlank_X = (Disp_Img.cols - (ImgDisp_Size.width*Subplot.width + (Subplot.width - 1)*DispBlank_Gap.width)) / 2;
  160. int EdgeBlank_Y = (Disp_Img.rows - (ImgDisp_Size.height*Subplot.height + (Subplot.height - 1)*DispBlank_Gap.height)) / 2;
  161. CvPoint LT_BasePos = cvPoint(EdgeBlank_X,EdgeBlank_Y);
  162. CvPoint LT_Pos = LT_BasePos;
  163.  
  164. //Display all images
  165. for (int i = 0; i < Img_Num; i++)
  166. {
  167. //Obtain the left top position
  168. if ((i%Subplot.width == 0) && (LT_Pos.x != LT_BasePos.x))
  169. {
  170. LT_Pos.x = LT_BasePos.x;
  171. LT_Pos.y += (DispBlank_Gap.height + ImgDisp_Size.height);
  172. }
  173. //Writting each to Window's Image
  174. Mat imgROI = Disp_Img(Rect(LT_Pos.x,LT_Pos.y,ImgDisp_Size.width,ImgDisp_Size.height));
  175. resize(SrcImg_V[i],imgROI,Size(ImgDisp_Size.width,ImgDisp_Size.height));
  176.  
  177. LT_Pos.x += (DispBlank_Gap.width + ImgDisp_Size.width);
  178. }
  179.  
  180. //Get the screen size of computer
  181. //int Scree_W = GetSystemMetrics(SM_CXSCREEN);
  182. //int Scree_H = GetSystemMetrics(SM_CYSCREEN);
  183. int Scree_W = 1920;
  184. int Scree_H = 1080;
  185.  
  186. //namedWindow("pic_viewer",CV_WINDOW_NORMAL);
  187. //setWindowProperty("pic_viewer",CV_WND_PROP_FULLSCREEN,CV_WINDOW_FULLSCREEN);
  188.  
  189. cvNamedWindow(MultiShow_WinName.c_str(),CV_WINDOW_NORMAL);
  190. //cvMoveWindow(MultiShow_WinName.c_str(),(Scree_W - Disp_Img.cols) / 2,(Scree_H - Disp_Img.rows) / 2);//Centralize the window
  191. setWindowProperty(MultiShow_WinName,CV_WINDOW_FULLSCREEN);
  192. cvShowImage(MultiShow_WinName.c_str(),&(IplImage(Disp_Img)));
  193. cvWaitKey(100);
  194. //cvDestroyWindow(MultiShow_WinName.c_str());
  195. }


修改版本:

  1. /************************************************************************
  2. * Author : stallman chen
  3. * Date : 2015/01/17
  4. * Address : pciture works shanghai
  5. * Email : 275076730@qq.com
  6. * function: Show 4 images in one window
  7. *************************************************************************/
  8. #include<iostream>
  9. #include<fstream>
  10. #include<string>
  11. #include"json.h"
  12. #include "opencv2/opencv.hpp"
  13.  
  14. using namespace cv;
  15. using namespace std;
  16.  
  17. void get_json_txt()
  18. {
  19. system("curl \"http://192.168.8.3:3000/getPhotoWallLogin?user=steve02&key=670b14728ad9902aecba32e22fa4f6bd&screenCode=sc08\" -o json_data.txt");
  20. }
  21.  
  22. Json::Value get_json_array()
  23. {
  24. ifstream file("json_data.txt",ios::out);
  25. if (!file)
  26. {
  27. cout << "Open file the json_dat.txt fail!!!" << endl;
  28. getchar();
  29. return -1;
  30. }
  31.  
  32. Json::Value root;
  33. Json::Reader reader;
  34.  
  35. if (!reader.parse(file,false))
  36. {
  37. cout << "Plz check your url make sure that you can contact your host" << endl;
  38. getchar();
  39. return -1;
  40. }
  41.  
  42. return root;
  43. }
  44.  
  45. vector<string> get_url(Json::Value root)
  46. {
  47. vector<string> pic_url;
  48. for (int i = 0; i < 4; ++i)
  49. {
  50. pic_url.push_back(root["info"][i]["originalInfo"]["url"].asString());
  51. }
  52. //for (auto s : pic_url)
  53. // cout << s << endl;
  54. return pic_url;
  55. }
  56.  
  57. Mat show_4_images(const vector<Mat>& src_img)
  58. {
  59. Mat image_display;
  60. size_t sz = src_img.size();
  61. CvSize src_image_size = cvSize(src_img[0].cols,src_img[0].rows);
  62.  
  63. //CvSize image_display_size = cvSize(src_image_size.width,src_image_size.height);
  64.  
  65. //******************** Blank setting ********************//
  66. CvSize DispBlank_Edge = cvSize(200,200);
  67. CvSize DispBlank_Gap = cvSize(200,200);
  68.  
  69. image_display.create(Size(6934,3900),CV_8UC3);
  70.  
  71. image_display.setTo(0);//Background
  72. if (sz >= 1)
  73. {
  74. Mat imgROI0 = image_display(Rect(250,100,src_image_size.width,src_image_size.height));
  75. resize(src_img[0],imgROI0,Size(2400,1800),INTER_CUBIC);
  76. }
  77.  
  78. if (sz >= 2)
  79. {
  80. Mat imgROI1 = image_display(Rect(3724,src_image_size.height));
  81. resize(src_img[1],imgROI1,INTER_CUBIC);
  82. }
  83.  
  84. if (sz >= 3)
  85. {
  86. Mat imgROI2 = image_display(Rect(650,2000,src_image_size.height));
  87. resize(src_img[2],imgROI2,INTER_CUBIC);
  88. }
  89.  
  90. if (sz >= 4)
  91. {
  92. Mat imgROI3 = image_display(Rect(4300,src_image_size.height));
  93. resize(src_img[3],imgROI3,INTER_CUBIC);
  94. }
  95.  
  96. return image_display;
  97. }
  98.  
  99.  
  100. int main()
  101. {
  102. string pic1_photoId_index("");
  103. string pic2_photoId_index("");
  104. string pic3_photoId_index("");
  105. string pic4_photoId_index("");
  106.  
  107. bool flag = false;
  108. int index[4] = { 0,3 };
  109.  
  110. while (1)
  111. {
  112. get_json_txt();
  113. Json::Value root;
  114. root = get_json_array();
  115. int info_zize = root["info"].size();
  116. //cout << root.size() << endl;
  117.  
  118. cout << info_zize << endl;
  119. bool flag = false;
  120.  
  121. if (info_zize == 4)
  122. {
  123. cout << "-----------------------------------------------------" << endl;
  124. cout << "上次图片1的id--->" << pic1_photoId_index << endl;
  125. cout << "上次图片2的id--->" << pic2_photoId_index << endl;
  126. cout << "上次图片3的id--->" << pic3_photoId_index << endl;
  127. cout << "上次图片4的id--->" << pic4_photoId_index << endl;
  128. string pic1 = root["info"][index[0]]["photoId"].asString();
  129. string pic2 = root["info"][index[1]]["photoId"].asString();
  130. string pic3 = root["info"][index[2]]["photoId"].asString();
  131. string pic4 = root["info"][index[3]]["photoId"].asString();
  132. cout << "----------------------------------------------------" << endl;
  133. cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  134. cout << "图片id1--------->" << pic1 << endl;
  135. cout << "图片id2--------->" << pic2 << endl;
  136. cout << "图片id3--------->" << pic3 << endl;
  137. cout << "图片id4--------->" << pic4 << endl;
  138. cout << "---------------------------------------------------" << endl;
  139. if (pic1 != pic1_photoId_index)
  140. {
  141. ofstream input("photoid.txt");
  142. input << pic1 << endl;
  143. input.close();
  144. vector<string>pic_url;
  145. pic_url = get_url(root);
  146. string http = root["photoServerIp"].asString();
  147. cout << pic_url.size() << endl;
  148. string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
  149. string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
  150. string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
  151. string command4 = "curl -o 4.jpg " + http + "/" + pic_url[3];
  152. cout << command1 << endl;
  153. cout << command2 << endl;
  154. cout << command3 << endl;
  155. cout << command4 << endl;
  156. cout << "---------------------------------------------------" << endl;
  157. cout << "---------------------------------------------------" << endl;
  158. system(command1.c_str());
  159. system(command2.c_str());
  160. system(command3.c_str());
  161. system(command4.c_str());
  162.  
  163. pic1_photoId_index = pic1;
  164. pic2_photoId_index = pic2;
  165. pic3_photoId_index = pic3;
  166. pic4_photoId_index = pic4;
  167. vector<Mat> imgs;
  168. imgs.push_back(imread("1.jpg"));
  169. imgs.push_back(imread("2.jpg"));
  170. imgs.push_back(imread("3.jpg"));
  171. imgs.push_back(imread("4.jpg"));
  172. Mat display_img = show_4_images(imgs);
  173. namedWindow("pic_viewer",CV_WINDOW_NORMAL);
  174. setWindowProperty("pic_viewer",CV_WINDOW_FULLSCREEN);
  175. imshow("pic_viewer",display_img);
  176. waitKey(10);
  177. }
  178.  
  179. }
  180.  
  181. if (info_zize == 3)
  182. {
  183. cout << "-----------------------------------------------------" << endl;
  184. cout << "上次图片1的id--->" << pic1_photoId_index << endl;
  185. cout << "上次图片2的id--->" << pic2_photoId_index << endl;
  186. cout << "上次图片3的id--->" << pic3_photoId_index << endl;
  187. cout << "上次图片4的id--->" << pic4_photoId_index << endl;
  188. string pic1 = root["info"][index[0]]["photoId"].asString();
  189. string pic2 = root["info"][index[1]]["photoId"].asString();
  190. string pic3 = root["info"][index[2]]["photoId"].asString();
  191. string pic4 = pic4_photoId_index;
  192. cout << "----------------------------------------------------" << endl;
  193. cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  194. cout << "图片id1--------->" << pic1 << endl;
  195. cout << "图片id2--------->" << pic2 << endl;
  196. cout << "图片id3--------->" << pic3 << endl;
  197. cout << "图片id4--------->" << pic4 << endl;
  198. cout << "---------------------------------------------------" << endl;
  199. if (pic1 != pic1_photoId_index)
  200. {
  201. ofstream input("photoid.txt");
  202. input << pic1 << endl;
  203. input.close();
  204. vector<string>pic_url;
  205. pic_url = get_url(root);
  206. string http = root["photoServerIp"].asString();
  207. cout << pic_url.size() << endl;
  208. string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
  209. string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
  210. string command3 = "curl -o 3.jpg " + http + "/" + pic_url[2];
  211.  
  212. cout << command1 << endl;
  213. cout << command2 << endl;
  214. cout << command3 << endl;
  215.  
  216. cout << "---------------------------------------------------" << endl;
  217. cout << "---------------------------------------------------" << endl;
  218. system(command1.c_str());
  219. system(command2.c_str());
  220. system(command3.c_str());
  221.  
  222. pic1_photoId_index = pic1;
  223. pic2_photoId_index = pic2;
  224. pic3_photoId_index = pic3;
  225.  
  226. vector<Mat> imgs;
  227. imgs.push_back(imread("1.jpg"));
  228. imgs.push_back(imread("2.jpg"));
  229. imgs.push_back(imread("3.jpg"));
  230. Mat display_img = show_4_images(imgs);
  231. namedWindow("pic_viewer",display_img);
  232. waitKey(10);
  233. }
  234. }
  235.  
  236. if (info_zize == 2)
  237. {
  238. cout << "-----------------------------------------------------" << endl;
  239. cout << "上次图片1的id--->" << pic1_photoId_index << endl;
  240. cout << "上次图片2的id--->" << pic2_photoId_index << endl;
  241. cout << "上次图片3的id--->" << pic3_photoId_index << endl;
  242. cout << "上次图片4的id--->" << pic4_photoId_index << endl;
  243. string pic1 = root["info"][index[0]]["photoId"].asString();
  244. string pic2 = root["info"][index[1]]["photoId"].asString();
  245. string pic3 = pic4_photoId_index;
  246. string pic4 = pic4_photoId_index;
  247. cout << "----------------------------------------------------" << endl;
  248. cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  249. cout << "图片id1--------->" << pic1 << endl;
  250. cout << "图片id2--------->" << pic2 << endl;
  251. cout << "图片id3--------->" << pic3 << endl;
  252. cout << "图片id4--------->" << pic4 << endl;
  253. cout << "---------------------------------------------------" << endl;
  254. if (pic1 != pic1_photoId_index)
  255. {
  256. ofstream input("photoid.txt");
  257. input << pic1 << endl;
  258. input.close();
  259. vector<string>pic_url;
  260. pic_url = get_url(root);
  261. string http = root["photoServerIp"].asString();
  262. cout << pic_url.size() << endl;
  263.  
  264. string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
  265. string command2 = "curl -o 2.jpg " + http + "/" + pic_url[1];
  266. cout << command1 << endl;
  267. cout << command2 << endl;
  268. cout << "---------------------------------------------------" << endl;
  269. cout << "---------------------------------------------------" << endl;
  270.  
  271. system(command1.c_str());
  272. system(command2.c_str());
  273.  
  274. pic1_photoId_index = pic1;
  275. pic2_photoId_index = pic2;
  276.  
  277. vector<Mat> imgs;
  278. imgs.push_back(imread("1.jpg"));
  279. imgs.push_back(imread("2.jpg"));
  280. Mat display_img = show_4_images(imgs);
  281. namedWindow("pic_viewer",display_img);
  282. waitKey(10);
  283. }
  284. }
  285.  
  286. if (info_zize == 1)
  287. {
  288. cout << "-----------------------------------------------------" << endl;
  289. cout << "上次图片1的id--->" << pic1_photoId_index << endl;
  290. cout << "上次图片2的id--->" << pic2_photoId_index << endl;
  291. cout << "上次图片3的id--->" << pic3_photoId_index << endl;
  292. cout << "上次图片4的id--->" << pic4_photoId_index << endl;
  293. string pic1 = root["info"][index[0]]["photoId"].asString();
  294. string pic2 = pic4_photoId_index;
  295. string pic3 = pic4_photoId_index;
  296. string pic4 = pic4_photoId_index;
  297. cout << "----------------------------------------------------" << endl;
  298. cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
  299. cout << "图片id1--------->" << pic1 << endl;
  300. cout << "图片id2--------->" << pic2 << endl;
  301. cout << "图片id3--------->" << pic3 << endl;
  302. cout << "图片id4--------->" << pic4 << endl;
  303. cout << "---------------------------------------------------" << endl;
  304. if (pic1 != pic1_photoId_index)
  305. {
  306. ofstream input("photoid.txt");
  307. input << pic1 << endl;
  308. input.close();
  309. vector<string>pic_url;
  310. pic_url = get_url(root);
  311. string http = root["photoServerIp"].asString();
  312. cout << pic_url.size() << endl;
  313.  
  314. string command1 = "curl -o 1.jpg " + http + "/" + pic_url[0];
  315. cout << command1 << endl;
  316.  
  317. cout << "---------------------------------------------------" << endl;
  318. cout << "---------------------------------------------------" << endl;
  319.  
  320. system(command1.c_str());
  321.  
  322. pic1_photoId_index = pic1;
  323.  
  324. vector<Mat> imgs;
  325. imgs.push_back(imread("1.jpg"));
  326. Mat display_img = show_4_images(imgs);
  327. namedWindow("pic_viewer",display_img);
  328. waitKey(10);
  329. }
  330. }
  331.  
  332. if (info_zize == 0)
  333. {
  334. namedWindow("pic_viewer",CV_WINDOW_NORMAL);
  335. setWindowProperty("pic_viewer",CV_WINDOW_FULLSCREEN);
  336. Mat black = imread("result.jpg");
  337. imshow("pic_viewer",black);
  338. cvWaitKey(2);
  339. }
  340. }
  341. }

猜你在找的Json相关文章