WebResult.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. namespace WebManager
  9. {
  10. public class WebResult
  11. {
  12. #region
  13. /// <summary>
  14. /// 数据定义
  15. /// </summary>
  16. private String webServer_IP = "";
  17. private String webServer_Port = "";
  18. private String webServer_Path = "";
  19. private String Url = "";
  20. private const String Post = "Post";
  21. private const String Content_Type= "Content-Type:application/json";
  22. private String[] _url = new string[8];
  23. #endregion
  24. /// <summary>
  25. /// 构造函数
  26. /// </summary>
  27. /// <param name="ws_ip">WebServer的IP地址</param>
  28. /// <param name="ws_port">WebServer的端口号</param>
  29. public WebResult(String ws_ip,String ws_port,String ws_url)
  30. {
  31. this.webServer_IP = ws_ip;
  32. this.webServer_Port = ws_port;
  33. this._url = ws_url.Split(',');
  34. //更新Web网址
  35. Update_Url();
  36. }
  37. /// <summary>
  38. /// 更新Web网址
  39. /// </summary>
  40. private void Update_Url()
  41. {
  42. this.webServer_Path = "/" + this.webServer_Path;
  43. this.Url = "http://" + this.webServer_IP + ":" + this.webServer_Port + this.webServer_Path;
  44. }
  45. /// <summary>
  46. /// WebServer的IP地址
  47. /// </summary>
  48. public String WebServer_IP
  49. {
  50. //get { return this.webServer_IP; }
  51. set
  52. {
  53. this.webServer_IP = value;
  54. }
  55. }
  56. /// <summary>
  57. /// WebServer的端口号
  58. /// </summary>
  59. public String WebServer_Port
  60. {
  61. //get { return this.webServer_IP; }
  62. set
  63. {
  64. this.webServer_Port = value;
  65. }
  66. }
  67. /// <summary>
  68. /// 向WebServer请求数据及返回结果
  69. /// </summary>
  70. /// <param name="postString">请求数据的JSON</param>
  71. /// <returns>返回结果的JSON</returns>
  72. private String RequestString(String postString)
  73. {
  74. try
  75. {
  76. String res = HttpRequestHelper.DoRequest(this.Url, Post, Content_Type, postString);
  77. return res;
  78. }
  79. catch
  80. {
  81. return "";
  82. }
  83. }
  84. /// <summary>
  85. /// 计算原始图像偏移角度及方向
  86. /// </summary>
  87. /// <param name="imagePath">图片路径</param>
  88. /// <param name="imageType">孔类型</param>
  89. /// <param name="firm">厂家类型</param>
  90. /// <returns>json字符串</returns>
  91. public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out float degree, out int direction, out int state)
  92. {
  93. try
  94. {
  95. this.webServer_Path = this._url[0];
  96. }
  97. catch
  98. {
  99. this.webServer_Path = "FIB_degree_recognize";
  100. }
  101. Update_Url();
  102. JObject json = new JObject();
  103. json.Add("img_path", imagePath);
  104. json.Add("img_type", imageType);
  105. json.Add("firm", firm);
  106. String res = RequestString(JsonConvert.SerializeObject(json));
  107. JObject jret= (JObject)JsonConvert.DeserializeObject(res);
  108. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  109. {
  110. JObject jname = JObject.Parse(jret[imagePath].ToString());
  111. state = Convert.ToInt32(jname["state"].ToString());
  112. if (state == 1)
  113. {
  114. degree = Convert.ToSingle(jname["degree"].ToString());
  115. direction = Convert.ToInt32(jname["direction"].ToString());
  116. }
  117. else
  118. {
  119. degree = 0;
  120. direction = 0;
  121. }
  122. }
  123. else
  124. {
  125. state = 0;
  126. direction = 0;
  127. degree = 0;
  128. }
  129. }
  130. /// <summary>
  131. /// 计算切割点位置
  132. /// </summary>
  133. /// <param name="imagePath">图片路径</param>
  134. /// <param name="imageType">孔类型</param>
  135. /// <param name="firm">厂家类型</param>
  136. /// <param name="offsetx1">切割点相对于原点坐标左上角x值</param>
  137. /// <param name="offsetx2">切割点相对于原点坐标左上角y值</param>
  138. /// <param name="offsety1">切割点相对于原点坐标右上角x值</param>
  139. /// <param name="offsety2">切割点相对于原点坐标右上角y值</param>
  140. /// <param name="state">返回状态</param>
  141. /// <returns>json字符串</returns>
  142. 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)
  143. {
  144. try
  145. {
  146. this.webServer_Path = this._url[1];
  147. }
  148. catch
  149. {
  150. this.webServer_Path = "test2";
  151. }
  152. Update_Url();
  153. JObject json = new JObject();
  154. json.Add("img_path", imagePath);
  155. json.Add("img_type", imageType);
  156. json.Add("firm", firm);
  157. String res = RequestString(JsonConvert.SerializeObject(json));
  158. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  159. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  160. {
  161. JObject jname = JObject.Parse(jret[imagePath].ToString());
  162. JArray jlocation = JArray.Parse(jname["location"].ToString());
  163. state = Convert.ToInt32(jname["state"].ToString());
  164. if (state == 1)
  165. {
  166. offsetx1 = Convert.ToSingle(jlocation[0][0].ToString());
  167. offsety1 = Convert.ToSingle(jlocation[0][1].ToString());
  168. offsetx2 = Convert.ToSingle(jlocation[1][0].ToString());
  169. offsety2 = Convert.ToSingle(jlocation[1][1].ToString());
  170. }
  171. else
  172. {
  173. offsetx1 = 0;
  174. offsety1 = 0;
  175. offsetx2 = 0;
  176. offsety2 = 0;
  177. }
  178. }
  179. else
  180. {
  181. state = 0;
  182. offsetx1 = 0;
  183. offsety1 = 0;
  184. offsetx2 = 0;
  185. offsety2 = 0;
  186. }
  187. }
  188. /// <summary>
  189. /// 是否切割成功验证
  190. /// </summary>
  191. /// <param name="imageBefore">切割前图像路径</param>
  192. /// <param name="imageAfter">切割后图像路径</param>
  193. /// <returns>json字符串</returns>
  194. public void Img_Cut_Success(String imageBefore, String imageAfter, out int state)
  195. {
  196. try
  197. {
  198. this.webServer_Path = this._url[2];
  199. }
  200. catch
  201. {
  202. this.webServer_Path = "FIB_verify";
  203. }
  204. Update_Url();
  205. JObject json = new JObject();
  206. json.Add("img_before_path", imageBefore);
  207. json.Add("img_after_path", imageAfter);
  208. String res = RequestString(JsonConvert.SerializeObject(json));
  209. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  210. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  211. {
  212. state = Convert.ToInt32(jret["state"].ToString());
  213. }
  214. else
  215. {
  216. state = 0;
  217. }
  218. }
  219. /// <summary>
  220. /// 计算切割后图像梯形区域上边中心点坐标
  221. /// </summary>
  222. /// <param name="imagePath">图片路径</param>
  223. /// <returns>json字符串</returns>
  224. public void Img_Trapezoid_Top_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
  225. {
  226. try
  227. {
  228. this.webServer_Path = this._url[3];
  229. }
  230. catch
  231. {
  232. this.webServer_Path = "test4";
  233. }
  234. Update_Url();
  235. JObject json = new JObject();
  236. json.Add("img_path", imagePath);
  237. String res = RequestString(JsonConvert.SerializeObject(json));
  238. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  239. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  240. {
  241. JObject jname = JObject.Parse(jret[imagePath].ToString());
  242. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  243. state = Convert.ToInt32(jname["state"].ToString());
  244. if(state==1)
  245. {
  246. offsetx = Convert.ToSingle(jlocation[0].ToString());
  247. offsety = Convert.ToSingle(jlocation[1].ToString());
  248. }
  249. else
  250. {
  251. offsetx = 0;
  252. offsety = 0;
  253. }
  254. }
  255. else
  256. {
  257. state = 0;
  258. offsetx = 0;
  259. offsety = 0;
  260. }
  261. }
  262. /// <summary>
  263. /// 自动对焦
  264. /// </summary>
  265. /// <param name="imagePath">一组图片路径</param>
  266. /// <returns>json字符串</returns>
  267. public String Img_Auto_Focus(List<String> imagePath)
  268. {
  269. try
  270. {
  271. this.webServer_Path = this._url[4];
  272. }
  273. catch
  274. {
  275. this.webServer_Path = "test5";
  276. }
  277. Update_Url();
  278. JObject json = new JObject();
  279. for(int i=0;i<imagePath.Count;i++)
  280. {
  281. json.Add("img_path" + i.ToString(), imagePath[i]);
  282. }
  283. String res = RequestString(JsonConvert.SerializeObject(json));
  284. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  285. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  286. {
  287. return jret["img_path"].ToString();
  288. }
  289. else
  290. {
  291. return "";
  292. }
  293. }
  294. /// <summary>
  295. /// 自动消像散
  296. /// </summary>
  297. /// <param name="imagePath">一组图片路径</param>
  298. /// <returns>json字符串</returns>
  299. public String Img_Auto_Stigmatic(List<String> imagePath)
  300. {
  301. try
  302. {
  303. this.webServer_Path = this._url[5];
  304. }
  305. catch
  306. {
  307. this.webServer_Path = "test6";
  308. }
  309. Update_Url();
  310. JObject json = new JObject();
  311. for (int i = 0; i < imagePath.Count; i++)
  312. {
  313. json.Add("img_path" + i.ToString(), imagePath[i]);
  314. }
  315. String res = RequestString(JsonConvert.SerializeObject(json));
  316. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  317. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  318. {
  319. return jret["img_path"].ToString();
  320. }
  321. else
  322. {
  323. return "";
  324. }
  325. }
  326. /// <summary>
  327. /// 计算切割面区域偏移角度及方向
  328. /// </summary>
  329. /// <param name="imagePath">图片路径</param>
  330. /// <returns>json字符串</returns>
  331. public void Img_Center_Position_OffsetAngle_Direction(String imagePath, out float degree, out int direction, out int state)
  332. {
  333. try
  334. {
  335. this.webServer_Path = this._url[6];
  336. }
  337. catch
  338. {
  339. this.webServer_Path = "test7";
  340. }
  341. Update_Url();
  342. JObject json = new JObject();
  343. json.Add("img_path", imagePath);
  344. String res = RequestString(JsonConvert.SerializeObject(json));
  345. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  346. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  347. {
  348. JObject jname = JObject.Parse(jret[imagePath].ToString());
  349. state = Convert.ToInt32(jname["state"].ToString());
  350. if (state == 1)
  351. {
  352. degree = Convert.ToSingle(jname["degree"].ToString());
  353. direction = Convert.ToInt32(jname["direction"].ToString());
  354. }
  355. else
  356. {
  357. degree = 0;
  358. direction = 0;
  359. }
  360. }
  361. else
  362. {
  363. state = 0;
  364. direction = 0;
  365. degree = 0;
  366. //offsetx = 0;
  367. //offsety = 0;
  368. }
  369. }
  370. /// <summary>
  371. /// 计算切割后图像梯形区域上边中心点坐标
  372. /// </summary>
  373. /// <param name="imagePath">图片路径</param>
  374. /// <returns>json字符串</returns>
  375. public void Img_Surface_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
  376. {
  377. try
  378. {
  379. this.webServer_Path = this._url[7];
  380. }
  381. catch
  382. {
  383. this.webServer_Path = "test4";
  384. }
  385. Update_Url();
  386. JObject json = new JObject();
  387. json.Add("img_path", imagePath);
  388. String res = RequestString(JsonConvert.SerializeObject(json));
  389. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  390. if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
  391. {
  392. JObject jname = JObject.Parse(jret[imagePath].ToString());
  393. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  394. state = Convert.ToInt32(jname["state"].ToString());
  395. if(state==1)
  396. {
  397. offsetx = Convert.ToSingle(jlocation[0].ToString());
  398. offsety = Convert.ToSingle(jlocation[1].ToString());
  399. }
  400. else
  401. {
  402. offsetx = 0;
  403. offsety = 0;
  404. }
  405. }
  406. else
  407. {
  408. state = 0;
  409. offsetx = 0;
  410. offsety = 0;
  411. }
  412. }
  413. /// <summary>
  414. /// 测量尺寸
  415. /// </summary>
  416. /// <param name="imagePath">图片路径</param>
  417. /// <param name="ratio">放大倍数</param>
  418. /// <param name="size">一像素代表**mm/um</param>
  419. /// <returns>json字符串</returns>
  420. public void Img_Measure_Size(String imagePath, float ratio, float size, out int state)
  421. {
  422. try
  423. {
  424. this.webServer_Path = this._url[8];
  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("ratio", ratio);
  434. json.Add("size", size);
  435. String res = RequestString(JsonConvert.SerializeObject(json));
  436. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  437. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  438. {
  439. JObject jstate = JObject.Parse(jret["img_name"].ToString());
  440. state = Convert.ToInt32(jstate["state"].ToString());
  441. }
  442. else
  443. {
  444. state = 0;
  445. }
  446. }
  447. }
  448. }