WebResult.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #endregion
  23. /// <summary>
  24. /// 构造函数
  25. /// </summary>
  26. /// <param name="ws_ip">WebServer的IP地址</param>
  27. /// <param name="ws_port">WebServer的端口号</param>
  28. public WebResult(String ws_ip,String ws_port)
  29. {
  30. this.webServer_IP = ws_ip;
  31. this.webServer_Port = ws_port;
  32. //更新Web网址
  33. Update_Url();
  34. }
  35. /// <summary>
  36. /// 更新Web网址
  37. /// </summary>
  38. private void Update_Url()
  39. {
  40. this.webServer_Path = "/" + this.webServer_Path;
  41. this.Url = "http://" + this.webServer_IP + ":" + this.webServer_Port + this.webServer_Path;
  42. }
  43. /// <summary>
  44. /// WebServer的IP地址
  45. /// </summary>
  46. public String WebServer_IP
  47. {
  48. //get { return this.webServer_IP; }
  49. set
  50. {
  51. this.webServer_IP = value;
  52. }
  53. }
  54. /// <summary>
  55. /// WebServer的端口号
  56. /// </summary>
  57. public String WebServer_Port
  58. {
  59. //get { return this.webServer_IP; }
  60. set
  61. {
  62. this.webServer_Port = value;
  63. }
  64. }
  65. /// <summary>
  66. /// 向WebServer请求数据及返回结果
  67. /// </summary>
  68. /// <param name="postString">请求数据的JSON</param>
  69. /// <returns>返回结果的JSON</returns>
  70. private String RequestString(String postString)
  71. {
  72. try
  73. {
  74. String res = HttpRequestHelper.DoRequest(this.Url, Post, Content_Type, postString);
  75. return res;
  76. }
  77. catch
  78. {
  79. return "";
  80. }
  81. }
  82. /// <summary>
  83. /// 计算原始图像偏移角度及方向
  84. /// </summary>
  85. /// <param name="imagePath">图片路径</param>
  86. /// <returns>json字符串</returns>
  87. public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out double degree, out int direction, out int state)
  88. {
  89. this.webServer_Path = "FIB_degree_recognize";
  90. Update_Url();
  91. JObject json = new JObject();
  92. json.Add("img_path", imagePath);
  93. json.Add("img_type", imageType);
  94. json.Add("firm", firm);
  95. String res = RequestString(JsonConvert.SerializeObject(json));
  96. JObject jret= (JObject)JsonConvert.DeserializeObject(res);
  97. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  98. {
  99. JObject jname = JObject.Parse(jret["img_name"].ToString());
  100. degree = Convert.ToDouble(jname["degree"].ToString());
  101. direction = Convert.ToInt32(jname["direction"].ToString());
  102. state = Convert.ToInt32(jname["state"].ToString());
  103. }
  104. else
  105. {
  106. state = 0;
  107. direction = 0;
  108. degree = 0;
  109. }
  110. }
  111. /// <summary>
  112. /// 计算切割点位置
  113. /// </summary>
  114. /// <param name="imagePath">图片路径</param>
  115. /// <returns>json字符串</returns>
  116. public void Img_Cut_Position(String imagePath, out double offsetx, out double offsety, out int state)
  117. {
  118. this.webServer_Path = "test2";
  119. Update_Url();
  120. JObject json = new JObject();
  121. json.Add("img_path", imagePath);
  122. String res = RequestString(JsonConvert.SerializeObject(json));
  123. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  124. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  125. {
  126. JObject jname = JObject.Parse(jret["img_name"].ToString());
  127. JArray jlocation = JArray.Parse(jname["location"].ToString());
  128. offsetx = Convert.ToDouble(jlocation[0].ToString());
  129. offsety = Convert.ToDouble(jlocation[1].ToString());
  130. state = Convert.ToInt32(jname["state"].ToString());
  131. }
  132. else
  133. {
  134. state = 0;
  135. offsetx = 0;
  136. offsety = 0;
  137. }
  138. }
  139. /// <summary>
  140. /// 是否切割成功验证
  141. /// </summary>
  142. /// <param name="imageBefore">切割前图像路径</param>
  143. /// <param name="imageAfter">切割后图像路径</param>
  144. /// <returns>json字符串</returns>
  145. public void Img_Cut_Success(String imageBefore, String imageAfter, out int state)
  146. {
  147. this.webServer_Path = "FIB_verify";
  148. Update_Url();
  149. JObject json = new JObject();
  150. json.Add("img_before_path", imageBefore);
  151. json.Add("img_after_path", imageAfter);
  152. String res = RequestString(JsonConvert.SerializeObject(json));
  153. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  154. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  155. {
  156. state = Convert.ToInt32(jret["state"].ToString());
  157. }
  158. else
  159. {
  160. state = 0;
  161. }
  162. }
  163. /// <summary>
  164. /// 计算切割后图像梯形区域上边中心点坐标
  165. /// </summary>
  166. /// <param name="imagePath">图片路径</param>
  167. /// <returns>json字符串</returns>
  168. public void Img_Trapezoid_Top_Center_Position(String imagePath, out double offsetx, out double offsety, out int state)
  169. {
  170. this.webServer_Path = "test4";
  171. Update_Url();
  172. JObject json = new JObject();
  173. json.Add("img_path", imagePath);
  174. String res = RequestString(JsonConvert.SerializeObject(json));
  175. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  176. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  177. {
  178. JObject jname = JObject.Parse(jret["img_name"].ToString());
  179. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  180. offsetx = Convert.ToDouble(jlocation[0].ToString());
  181. offsety = Convert.ToDouble(jlocation[1].ToString());
  182. state = Convert.ToInt32(jname["state"].ToString());
  183. }
  184. else
  185. {
  186. state = 0;
  187. offsetx = 0;
  188. offsety = 0;
  189. }
  190. }
  191. /// <summary>
  192. /// 自动对焦
  193. /// </summary>
  194. /// <param name="imagePath">一组图片路径</param>
  195. /// <returns>json字符串</returns>
  196. public String Img_Auto_Focus(List<String> imagePath)
  197. {
  198. this.webServer_Path = "test5";
  199. Update_Url();
  200. JObject json = new JObject();
  201. for(int i=0;i<imagePath.Count;i++)
  202. {
  203. json.Add("img_path" + i.ToString(), imagePath[i]);
  204. }
  205. String res = RequestString(JsonConvert.SerializeObject(json));
  206. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  207. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  208. {
  209. return jret["img_path"].ToString();
  210. }
  211. else
  212. {
  213. return "";
  214. }
  215. }
  216. /// <summary>
  217. /// 自动消像散
  218. /// </summary>
  219. /// <param name="imagePath">一组图片路径</param>
  220. /// <returns>json字符串</returns>
  221. public String Img_Auto_Stigmatic(List<String> imagePath)
  222. {
  223. this.webServer_Path = "test6";
  224. Update_Url();
  225. JObject json = new JObject();
  226. for (int i = 0; i < imagePath.Count; i++)
  227. {
  228. json.Add("img_path" + i.ToString(), imagePath[i]);
  229. }
  230. String res = RequestString(JsonConvert.SerializeObject(json));
  231. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  232. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  233. {
  234. return jret["img_path"].ToString();
  235. }
  236. else
  237. {
  238. return "";
  239. }
  240. }
  241. /// <summary>
  242. /// 计算切割面区域中心坐标,以及偏移角度及方向
  243. /// </summary>
  244. /// <param name="imagePath">图片路径</param>
  245. /// <returns>json字符串</returns>
  246. public void Img_Center_Position_OffsetAngle_Direction(String imagePath, out double offsetx, out double offsety, out double degree, out int direction, out int state)
  247. {
  248. this.webServer_Path = "test7";
  249. Update_Url();
  250. JObject json = new JObject();
  251. json.Add("img_path", imagePath);
  252. String res = RequestString(JsonConvert.SerializeObject(json));
  253. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  254. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  255. {
  256. JObject jname = JObject.Parse(jret["img_name"].ToString());
  257. JArray jlocation = JArray.Parse(jname["center"].ToString());
  258. offsetx = Convert.ToDouble(jlocation[0].ToString());
  259. offsety = Convert.ToDouble(jlocation[1].ToString());
  260. state = Convert.ToInt32(jname["state"].ToString());
  261. degree = Convert.ToDouble(jname["degree"].ToString());
  262. direction = Convert.ToInt32(jname["direction"].ToString());
  263. }
  264. else
  265. {
  266. state = 0;
  267. direction = 0;
  268. degree = 0;
  269. offsetx = 0;
  270. offsety = 0;
  271. }
  272. }
  273. /// <summary>
  274. /// 测量尺寸
  275. /// </summary>
  276. /// <param name="imagePath">图片路径</param>
  277. /// <param name="ratio">放大倍数</param>
  278. /// <param name="size">一像素代表**mm/um</param>
  279. /// <returns>json字符串</returns>
  280. public void Img_Measure_Size(String imagePath, String ratio, String size, out int state)
  281. {
  282. this.webServer_Path = "test8";
  283. Update_Url();
  284. JObject json = new JObject();
  285. json.Add("img_path", imagePath);
  286. json.Add("ratio", ratio);
  287. json.Add("size", size);
  288. String res = RequestString(JsonConvert.SerializeObject(json));
  289. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  290. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  291. {
  292. JObject jstate = JObject.Parse(jret["img_name"].ToString());
  293. state = Convert.ToInt32(jstate["state"].ToString());
  294. }
  295. else
  296. {
  297. state = 0;
  298. }
  299. }
  300. }
  301. }