WebResult1.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json.Linq;
  7. using Newtonsoft.Json;
  8. using System.Drawing;
  9. using Extender;
  10. namespace WebManager
  11. {
  12. public class WebResult
  13. {
  14. #region
  15. /// <summary>
  16. /// 数据定义
  17. /// </summary>
  18. private String webServer_IP = "";
  19. private String webServer_Port = "";
  20. private String webServer_Path = "";
  21. private String Url = "";
  22. private const String Post = "Post";
  23. private const String Content_Type= "Content-Type:application/json";
  24. private String[] _url = new string[10];
  25. #endregion
  26. /// <summary>
  27. /// 构造函数
  28. /// </summary>
  29. /// <param name="ws_ip">WebServer的IP地址</param>
  30. /// <param name="ws_port">WebServer的端口号</param>
  31. public WebResult(String ws_ip,String ws_port,String ws_url)
  32. {
  33. this.webServer_IP = ws_ip;
  34. this.webServer_Port = ws_port;
  35. this._url = ws_url.Split(',');
  36. //更新Web网址
  37. Update_Url();
  38. }
  39. /// <summary>
  40. /// 更新Web网址
  41. /// </summary>
  42. private void Update_Url()
  43. {
  44. this.webServer_Path = "/" + this.webServer_Path;
  45. this.Url = "http://" + this.webServer_IP + ":" + this.webServer_Port + this.webServer_Path;
  46. }
  47. /// <summary>
  48. /// WebServer的IP地址
  49. /// </summary>
  50. public String WebServer_IP
  51. {
  52. //get { return this.webServer_IP; }
  53. set
  54. {
  55. this.webServer_IP = value;
  56. }
  57. }
  58. /// <summary>
  59. /// WebServer的端口号
  60. /// </summary>
  61. public String WebServer_Port
  62. {
  63. //get { return this.webServer_IP; }
  64. set
  65. {
  66. this.webServer_Port = value;
  67. }
  68. }
  69. /// <summary>
  70. /// 向WebServer请求数据及返回结果
  71. /// </summary>
  72. /// <param name="postString">请求数据的JSON</param>
  73. /// <returns>返回结果的JSON</returns>
  74. private String RequestString(String postString)
  75. {
  76. try
  77. {
  78. String res = HttpRequestHelper.DoRequest(this.Url, Post, Content_Type, postString);
  79. return res;
  80. }
  81. catch
  82. {
  83. return "";
  84. }
  85. }
  86. /// <summary>
  87. /// 计算原始图像偏移角度及方向
  88. /// </summary>
  89. /// <param name="imagePath">图片路径</param>
  90. /// <param name="imageType">孔类型</param>
  91. /// <param name="firm">厂家类型</param>
  92. /// <returns>json字符串</returns>
  93. public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out float degree, out int direction, out int state)
  94. {
  95. try
  96. {
  97. this.webServer_Path = this._url[0];
  98. }
  99. catch
  100. {
  101. this.webServer_Path = "FIB_degree_recognize";
  102. }
  103. Update_Url();
  104. JObject json = new JObject();
  105. json.Add("img_path", imagePath);
  106. json.Add("img_type", imageType);
  107. json.Add("firm", firm);
  108. String res = RequestString(JsonConvert.SerializeObject(json));
  109. JObject jret= (JObject)JsonConvert.DeserializeObject(res);
  110. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  111. {
  112. JObject jname = JObject.Parse(jret[imagePath].ToString());
  113. state = Convert.ToInt32(jname["state"].ToString());
  114. if (state == 1)
  115. {
  116. degree = Convert.ToSingle(jname["degree"].ToString());
  117. direction = Convert.ToInt32(jname["direction"].ToString());
  118. }
  119. else
  120. {
  121. degree = 0;
  122. direction = 0;
  123. }
  124. }
  125. else
  126. {
  127. state = 0;
  128. direction = 0;
  129. degree = 0;
  130. }
  131. }
  132. /// <summary>
  133. /// 计算切割点位置
  134. /// </summary>
  135. /// <param name="imagePath">图片路径</param>
  136. /// <param name="imageType">孔类型</param>
  137. /// <param name="firm">厂家类型</param>
  138. /// <param name="offsetx1">切割点相对于原点坐标左上角x值</param>
  139. /// <param name="offsetx2">切割点相对于原点坐标左上角y值</param>
  140. /// <param name="offsety1">切割点相对于原点坐标右上角x值</param>
  141. /// <param name="offsety2">切割点相对于原点坐标右上角y值</param>
  142. /// <param name="state">返回状态</param>
  143. /// <returns>json字符串</returns>
  144. public void Img_Cut_Position(String imagePath, int imageType, String firm, out float offsetx1, out float offsety1, out float offsetx2, out float offsety2, out int state)
  145. {
  146. try
  147. {
  148. this.webServer_Path = this._url[1];
  149. }
  150. catch
  151. {
  152. this.webServer_Path = "test2";
  153. }
  154. Update_Url();
  155. JObject json = new JObject();
  156. json.Add("img_path", imagePath);
  157. json.Add("img_type", imageType);
  158. json.Add("firm", firm);
  159. String res = RequestString(JsonConvert.SerializeObject(json));
  160. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  161. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  162. {
  163. JObject jname = JObject.Parse(jret[imagePath].ToString());
  164. JArray jlocation = JArray.Parse(jname["location"].ToString());
  165. state = Convert.ToInt32(jname["state"].ToString());
  166. if (state == 1)
  167. {
  168. offsetx1 = Convert.ToSingle(jlocation[0][0].ToString());
  169. offsety1 = Convert.ToSingle(jlocation[0][1].ToString());
  170. offsetx2 = Convert.ToSingle(jlocation[1][0].ToString());
  171. offsety2 = Convert.ToSingle(jlocation[1][1].ToString());
  172. }
  173. else
  174. {
  175. offsetx1 = 0;
  176. offsety1 = 0;
  177. offsetx2 = 0;
  178. offsety2 = 0;
  179. }
  180. }
  181. else
  182. {
  183. state = 0;
  184. offsetx1 = 0;
  185. offsety1 = 0;
  186. offsetx2 = 0;
  187. offsety2 = 0;
  188. }
  189. }
  190. /// <summary>
  191. /// 是否切割成功验证
  192. /// </summary>
  193. /// <param name="imageBefore">切割前图像路径</param>
  194. /// <param name="imageAfter">切割后图像路径</param>
  195. /// <returns>json字符串</returns>
  196. public void Img_Cut_Success(String imageBefore, String imageAfter, out int state)
  197. {
  198. try
  199. {
  200. this.webServer_Path = this._url[2];
  201. }
  202. catch
  203. {
  204. this.webServer_Path = "FIB_verify";
  205. }
  206. Update_Url();
  207. JObject json = new JObject();
  208. json.Add("img_before_path", imageBefore);
  209. json.Add("img_after_path", imageAfter);
  210. String res = RequestString(JsonConvert.SerializeObject(json));
  211. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  212. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  213. {
  214. state = Convert.ToInt32(jret["state"].ToString());
  215. }
  216. else
  217. {
  218. state = 0;
  219. }
  220. }
  221. /// <summary>
  222. /// 计算切割后图像梯形区域上边中心点坐标
  223. /// </summary>
  224. /// <param name="imagePath">图片路径</param>
  225. /// <returns>json字符串</returns>
  226. public void Img_Trapezoid_Top_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
  227. {
  228. try
  229. {
  230. this.webServer_Path = this._url[3];
  231. }
  232. catch
  233. {
  234. this.webServer_Path = "test4";
  235. }
  236. Update_Url();
  237. JObject json = new JObject();
  238. json.Add("img_path", imagePath);
  239. String res = RequestString(JsonConvert.SerializeObject(json));
  240. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  241. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  242. {
  243. JObject jname = JObject.Parse(jret[imagePath].ToString());
  244. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  245. state = Convert.ToInt32(jname["state"].ToString());
  246. if(state==1)
  247. {
  248. offsetx = Convert.ToSingle(jlocation[0].ToString());
  249. offsety = Convert.ToSingle(jlocation[1].ToString());
  250. }
  251. else
  252. {
  253. offsetx = 0;
  254. offsety = 0;
  255. }
  256. }
  257. else
  258. {
  259. state = 0;
  260. offsetx = 0;
  261. offsety = 0;
  262. }
  263. }
  264. /// <summary>
  265. /// 自动对焦
  266. /// </summary>
  267. /// <param name="imagePath">一组图片路径</param>
  268. /// <returns>json字符串</returns>
  269. public String Img_Auto_Focus(List<String> imagePath)
  270. {
  271. try
  272. {
  273. this.webServer_Path = this._url[4];
  274. }
  275. catch
  276. {
  277. this.webServer_Path = "test5";
  278. }
  279. Update_Url();
  280. JObject json = new JObject();
  281. for(int i=0;i<imagePath.Count;i++)
  282. {
  283. json.Add("img_path" + i.ToString(), imagePath[i]);
  284. }
  285. String res = RequestString(JsonConvert.SerializeObject(json));
  286. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  287. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  288. {
  289. return jret["img_path"].ToString();
  290. }
  291. else
  292. {
  293. return "";
  294. }
  295. }
  296. /// <summary>
  297. /// 自动消像散
  298. /// </summary>
  299. /// <param name="imagePath">一组图片路径</param>
  300. /// <returns>json字符串</returns>
  301. public String Img_Auto_Stigmatic(List<String> imagePath)
  302. {
  303. try
  304. {
  305. this.webServer_Path = this._url[5];
  306. }
  307. catch
  308. {
  309. this.webServer_Path = "test6";
  310. }
  311. Update_Url();
  312. JObject json = new JObject();
  313. for (int i = 0; i < imagePath.Count; i++)
  314. {
  315. json.Add("img_path" + i.ToString(), imagePath[i]);
  316. }
  317. String res = RequestString(JsonConvert.SerializeObject(json));
  318. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  319. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  320. {
  321. return jret["img_path"].ToString();
  322. }
  323. else
  324. {
  325. return "";
  326. }
  327. }
  328. /// <summary>
  329. /// 计算切割面区域偏移角度及方向
  330. /// </summary>
  331. /// <param name="imagePath">图片路径</param>
  332. /// <returns>json字符串</returns>
  333. public void Img_Center_Position_OffsetAngle_Direction(String imagePath, out float degree, out int direction, out int state)
  334. {
  335. try
  336. {
  337. this.webServer_Path = this._url[6];
  338. }
  339. catch
  340. {
  341. this.webServer_Path = "test7";
  342. }
  343. Update_Url();
  344. JObject json = new JObject();
  345. json.Add("img_path", imagePath);
  346. String res = RequestString(JsonConvert.SerializeObject(json));
  347. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  348. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  349. {
  350. JObject jname = JObject.Parse(jret[imagePath].ToString());
  351. state = Convert.ToInt32(jname["state"].ToString());
  352. if (state == 1)
  353. {
  354. degree = Convert.ToSingle(jname["degree"].ToString());
  355. direction = Convert.ToInt32(jname["direction"].ToString());
  356. }
  357. else
  358. {
  359. degree = 0;
  360. direction = 0;
  361. }
  362. }
  363. else
  364. {
  365. state = 0;
  366. direction = 0;
  367. degree = 0;
  368. //offsetx = 0;
  369. //offsety = 0;
  370. }
  371. }
  372. ///// <summary>
  373. ///// 计算切割后图像梯形区域上边中心点坐标
  374. ///// </summary>
  375. ///// <param name="imagePath">图片路径</param>
  376. ///// <returns>json字符串</returns>
  377. //public void Img_Surface_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
  378. //{
  379. // try
  380. // {
  381. // this.webServer_Path = this._url[7];
  382. // }
  383. // catch
  384. // {
  385. // this.webServer_Path = "test4";
  386. // }
  387. // Update_Url();
  388. // JObject json = new JObject();
  389. // json.Add("img_path", imagePath);
  390. // String res = RequestString(JsonConvert.SerializeObject(json));
  391. // JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  392. // if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  393. // {
  394. // JObject jname = JObject.Parse(jret[imagePath].ToString());
  395. // JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  396. // state = Convert.ToInt32(jname["state"].ToString());
  397. // if(state==1)
  398. // {
  399. // offsetx = Convert.ToSingle(jlocation[0].ToString());
  400. // offsety = Convert.ToSingle(jlocation[1].ToString());
  401. // }
  402. // else
  403. // {
  404. // offsetx = 0;
  405. // offsety = 0;
  406. // }
  407. // }
  408. // else
  409. // {
  410. // state = 0;
  411. // offsetx = 0;
  412. // offsety = 0;
  413. // }
  414. //}
  415. /// <summary>
  416. /// 计算两个测量区域坐标
  417. /// </summary>
  418. /// <param name="imagePath">图片路径</param>
  419. /// <returns>json字符串</returns>
  420. public void Img_Two_Region_Position(String imagePath, int imageType, String firm, out Point center1, out float mag1, out Point center2, out float mag2, out int state)
  421. {
  422. try
  423. {
  424. this.webServer_Path = this._url[7];
  425. }
  426. catch
  427. {
  428. this.webServer_Path = "test8";
  429. }
  430. Update_Url();
  431. JObject json = new JObject();
  432. json.Add("img_path", imagePath);
  433. json.Add("img_type", imageType);
  434. json.Add("firm", firm);
  435. String res = RequestString(JsonConvert.SerializeObject(json));
  436. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  437. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  438. {
  439. JObject jname = JObject.Parse(jret[imagePath].ToString());
  440. state = Convert.ToInt32(jname["state"].ToString());
  441. if (state == 1)
  442. {
  443. JArray jcenter1 = JArray.Parse(jname["center1"].ToString());
  444. String ptx = jcenter1[0].ToString();
  445. String pty = jcenter1[1].ToString();
  446. center1 = new Point(Convert.ToInt32(ptx),Convert.ToInt32(pty));
  447. String jmag1 = jname["magnification1"].ToString();
  448. mag1 = Convert.ToSingle(jmag1.ToString());
  449. JArray jcenter2 = JArray.Parse(jname["center2"].ToString());
  450. ptx = jcenter2[0].ToString();
  451. pty = jcenter2[1].ToString();
  452. center2 = new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty));
  453. String jmag2 = jname["magnification2"].ToString();
  454. mag2 = Convert.ToSingle(jmag2.ToString());
  455. }
  456. else
  457. {
  458. center1 = new Point(0, 0);
  459. center2 = new Point(0, 0);
  460. mag1 = 0;
  461. mag2 = 0;
  462. state = 0;
  463. }
  464. }
  465. else
  466. {
  467. center1 = new Point(0, 0);
  468. center2 = new Point(0, 0);
  469. mag1 = 0;
  470. mag2 = 0;
  471. state = 0;
  472. }
  473. }
  474. /// <summary>
  475. /// 测量尺寸
  476. /// </summary>
  477. /// <param name="imagePath">图片路径</param>
  478. /// <param name="ratio">放大倍数</param>
  479. /// <param name="size">一像素代表**mm/um</param>
  480. /// <returns>json字符串</returns>
  481. public void Img_Measure_Size(String imagePath1, String imagePath2, float size1, float size2, String resultPath, int imageType, String firm, out int state)
  482. {
  483. try
  484. {
  485. this.webServer_Path = this._url[8];
  486. }
  487. catch
  488. {
  489. this.webServer_Path = "test9";
  490. }
  491. Update_Url();
  492. JObject json = new JObject();
  493. json.Add("img_path1", imagePath1);
  494. json.Add("img_path2", imagePath2);
  495. json.Add("size1", size1);
  496. json.Add("size2", size2);
  497. json.Add("result_path", resultPath);
  498. json.Add("img_type", imageType);
  499. json.Add("firm", firm);
  500. String res = RequestString(JsonConvert.SerializeObject(json));
  501. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  502. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  503. {
  504. state = Convert.ToInt32(jret["state"].ToString());
  505. }
  506. else
  507. {
  508. state = 0;
  509. }
  510. }
  511. /// <summary>
  512. /// 测试-ESD返回数据
  513. /// </summary>
  514. /// <param name="imagePath">图片路径</param>
  515. /// <returns>json字符串</returns>
  516. public void EDS_Param(String imagePath, out List<Point> listPoints, out List<List<Segment>> listFeature, out int state)
  517. {
  518. try
  519. {
  520. this.webServer_Path = this._url[9];
  521. }
  522. catch
  523. {
  524. this.webServer_Path = "test9";
  525. }
  526. listPoints = new List<Point>();
  527. listFeature = new List<List<Segment>>();
  528. List<Segment> listSegment = new List<Segment>();
  529. state = 0;
  530. Update_Url();
  531. JObject json = new JObject();
  532. json.Add("img_path", imagePath);
  533. String res = RequestString(JsonConvert.SerializeObject(json));
  534. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  535. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  536. {
  537. JObject jname = JObject.Parse(jret[imagePath].ToString());
  538. state = Convert.ToInt32(jname["state"].ToString());
  539. if(state==1)
  540. {
  541. JArray jpoints = JArray.Parse(jname["points"].ToString());
  542. for(int i=0;i<jpoints.Count;i++)
  543. {
  544. Point pt = new Point();
  545. pt.X = Convert.ToInt32(jpoints[i][0].ToString());
  546. pt.Y = Convert.ToInt32(jpoints[i][1].ToString());
  547. listPoints.Add(pt);
  548. }
  549. JArray jareas = JArray.Parse(jname["areas"].ToString());
  550. for(int i=0;i<jareas.Count;i++)
  551. {
  552. listSegment = new List<Segment>();
  553. JArray jarea = JArray.Parse(jareas[i].ToString());
  554. for(int j=0;j<jarea.Count;j++)
  555. {
  556. Segment st = new Segment();
  557. st.X = Convert.ToInt32(jarea[j][0].ToString());
  558. st.Y = Convert.ToInt32(jarea[j][1].ToString());
  559. st.Length = Convert.ToInt32(jarea[j][2].ToString());
  560. listSegment.Add(st);
  561. }
  562. listFeature.Add(listSegment);
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }