WebResult.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 double 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("img_name") != null && jret.Property("img_name").ToString() != "")
  109. {
  110. JObject jname = JObject.Parse(jret["img_name"].ToString());
  111. degree = Convert.ToDouble(jname["degree"].ToString());
  112. direction = Convert.ToInt32(jname["direction"].ToString());
  113. state = Convert.ToInt32(jname["state"].ToString());
  114. }
  115. else
  116. {
  117. state = 0;
  118. direction = 0;
  119. degree = 0;
  120. }
  121. }
  122. /// <summary>
  123. /// 计算切割点位置
  124. /// </summary>
  125. /// <param name="imagePath">图片路径</param>
  126. /// <param name="imageType">孔类型</param>
  127. /// <param name="firm">厂家类型</param>
  128. /// <param name="offsetx1">切割点相对于原点坐标左上角x值</param>
  129. /// <param name="offsetx2">切割点相对于原点坐标左上角y值</param>
  130. /// <param name="offsety1">切割点相对于原点坐标右上角x值</param>
  131. /// <param name="offsety2">切割点相对于原点坐标右上角y值</param>
  132. /// <param name="state">返回状态</param>
  133. /// <returns>json字符串</returns>
  134. public void Img_Cut_Position(String imagePath, String imageType, String firm, out float offsetx1, out float offsety1, out float offsetx2, out float offsety2, out int state)
  135. {
  136. try
  137. {
  138. this.webServer_Path = this._url[1];
  139. }
  140. catch
  141. {
  142. this.webServer_Path = "test2";
  143. }
  144. Update_Url();
  145. JObject json = new JObject();
  146. json.Add("img_path", imagePath);
  147. json.Add("img_type", imageType);
  148. json.Add("firm", firm);
  149. String res = RequestString(JsonConvert.SerializeObject(json));
  150. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  151. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  152. {
  153. JObject jname = JObject.Parse(jret["img_name"].ToString());
  154. JArray jlocation = JArray.Parse(jname["location"].ToString());
  155. offsetx1 = Convert.ToSingle(jlocation[0][0].ToString());
  156. offsety1 = Convert.ToSingle(jlocation[0][1].ToString());
  157. offsetx2 = Convert.ToSingle(jlocation[1][0].ToString());
  158. offsety2 = Convert.ToSingle(jlocation[1][1].ToString());
  159. state = Convert.ToInt32(jname["state"].ToString());
  160. }
  161. else
  162. {
  163. state = 0;
  164. offsetx1 = 0;
  165. offsety1 = 0;
  166. offsetx2 = 0;
  167. offsety2 = 0;
  168. }
  169. }
  170. /// <summary>
  171. /// 是否切割成功验证
  172. /// </summary>
  173. /// <param name="imageBefore">切割前图像路径</param>
  174. /// <param name="imageAfter">切割后图像路径</param>
  175. /// <returns>json字符串</returns>
  176. public void Img_Cut_Success(String imageBefore, String imageAfter, out int state)
  177. {
  178. try
  179. {
  180. this.webServer_Path = this._url[2];
  181. }
  182. catch
  183. {
  184. this.webServer_Path = "FIB_verify";
  185. }
  186. Update_Url();
  187. JObject json = new JObject();
  188. json.Add("img_before_path", imageBefore);
  189. json.Add("img_after_path", imageAfter);
  190. String res = RequestString(JsonConvert.SerializeObject(json));
  191. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  192. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  193. {
  194. state = Convert.ToInt32(jret["state"].ToString());
  195. }
  196. else
  197. {
  198. state = 0;
  199. }
  200. }
  201. /// <summary>
  202. /// 计算切割后图像梯形区域上边中心点坐标
  203. /// </summary>
  204. /// <param name="imagePath">图片路径</param>
  205. /// <returns>json字符串</returns>
  206. public void Img_Trapezoid_Top_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
  207. {
  208. try
  209. {
  210. this.webServer_Path = this._url[3];
  211. }
  212. catch
  213. {
  214. this.webServer_Path = "test4";
  215. }
  216. Update_Url();
  217. JObject json = new JObject();
  218. json.Add("img_path", imagePath);
  219. String res = RequestString(JsonConvert.SerializeObject(json));
  220. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  221. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  222. {
  223. JObject jname = JObject.Parse(jret["img_name"].ToString());
  224. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  225. offsetx = Convert.ToSingle(jlocation[0].ToString());
  226. offsety = Convert.ToSingle(jlocation[1].ToString());
  227. state = Convert.ToInt32(jname["state"].ToString());
  228. }
  229. else
  230. {
  231. state = 0;
  232. offsetx = 0;
  233. offsety = 0;
  234. }
  235. }
  236. /// <summary>
  237. /// 自动对焦
  238. /// </summary>
  239. /// <param name="imagePath">一组图片路径</param>
  240. /// <returns>json字符串</returns>
  241. public String Img_Auto_Focus(List<String> imagePath)
  242. {
  243. try
  244. {
  245. this.webServer_Path = this._url[4];
  246. }
  247. catch
  248. {
  249. this.webServer_Path = "test5";
  250. }
  251. Update_Url();
  252. JObject json = new JObject();
  253. for(int i=0;i<imagePath.Count;i++)
  254. {
  255. json.Add("img_path" + i.ToString(), imagePath[i]);
  256. }
  257. String res = RequestString(JsonConvert.SerializeObject(json));
  258. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  259. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  260. {
  261. return jret["img_path"].ToString();
  262. }
  263. else
  264. {
  265. return "";
  266. }
  267. }
  268. /// <summary>
  269. /// 自动消像散
  270. /// </summary>
  271. /// <param name="imagePath">一组图片路径</param>
  272. /// <returns>json字符串</returns>
  273. public String Img_Auto_Stigmatic(List<String> imagePath)
  274. {
  275. try
  276. {
  277. this.webServer_Path = this._url[5];
  278. }
  279. catch
  280. {
  281. this.webServer_Path = "test6";
  282. }
  283. Update_Url();
  284. JObject json = new JObject();
  285. for (int i = 0; i < imagePath.Count; i++)
  286. {
  287. json.Add("img_path" + i.ToString(), imagePath[i]);
  288. }
  289. String res = RequestString(JsonConvert.SerializeObject(json));
  290. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  291. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  292. {
  293. return jret["img_path"].ToString();
  294. }
  295. else
  296. {
  297. return "";
  298. }
  299. }
  300. /// <summary>
  301. /// 计算切割面区域中心坐标,以及偏移角度及方向
  302. /// </summary>
  303. /// <param name="imagePath">图片路径</param>
  304. /// <returns>json字符串</returns>
  305. public void Img_Center_Position_OffsetAngle_Direction(String imagePath, String imageType, String firm, out float offsetx, out float offsety, out float degree, out int direction, out int state)
  306. {
  307. try
  308. {
  309. this.webServer_Path = this._url[6];
  310. }
  311. catch
  312. {
  313. this.webServer_Path = "test7";
  314. }
  315. Update_Url();
  316. JObject json = new JObject();
  317. json.Add("img_path", imagePath);
  318. json.Add("img_type", imageType);
  319. json.Add("firm", firm);
  320. String res = RequestString(JsonConvert.SerializeObject(json));
  321. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  322. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  323. {
  324. JObject jname = JObject.Parse(jret["img_name"].ToString());
  325. JArray jlocation = JArray.Parse(jname["center"].ToString());
  326. offsetx = Convert.ToSingle(jlocation[0].ToString());
  327. offsety = Convert.ToSingle(jlocation[1].ToString());
  328. state = Convert.ToInt32(jname["state"].ToString());
  329. degree = Convert.ToSingle(jname["degree"].ToString());
  330. direction = Convert.ToInt32(jname["direction"].ToString());
  331. }
  332. else
  333. {
  334. state = 0;
  335. direction = 0;
  336. degree = 0;
  337. offsetx = 0;
  338. offsety = 0;
  339. }
  340. }
  341. /// <summary>
  342. /// 测量尺寸
  343. /// </summary>
  344. /// <param name="imagePath">图片路径</param>
  345. /// <param name="ratio">放大倍数</param>
  346. /// <param name="size">一像素代表**mm/um</param>
  347. /// <returns>json字符串</returns>
  348. public void Img_Measure_Size(String imagePath, float ratio, float size, out int state)
  349. {
  350. try
  351. {
  352. this.webServer_Path = this._url[7];
  353. }
  354. catch
  355. {
  356. this.webServer_Path = "test8";
  357. }
  358. Update_Url();
  359. JObject json = new JObject();
  360. json.Add("img_path", imagePath);
  361. json.Add("ratio", ratio);
  362. json.Add("size", size);
  363. String res = RequestString(JsonConvert.SerializeObject(json));
  364. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  365. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  366. {
  367. JObject jstate = JObject.Parse(jret["img_name"].ToString());
  368. state = Convert.ToInt32(jstate["state"].ToString());
  369. }
  370. else
  371. {
  372. state = 0;
  373. }
  374. }
  375. }
  376. }