WebResult.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /// <param name="imageType">孔类型</param>
  87. /// <param name="firm">厂家类型</param>
  88. /// <returns>json字符串</returns>
  89. public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out double degree, out int direction, out int state)
  90. {
  91. this.webServer_Path = "FIB_degree_recognize";
  92. Update_Url();
  93. JObject json = new JObject();
  94. json.Add("img_path", imagePath);
  95. json.Add("img_type", imageType);
  96. json.Add("firm", firm);
  97. String res = RequestString(JsonConvert.SerializeObject(json));
  98. JObject jret= (JObject)JsonConvert.DeserializeObject(res);
  99. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  100. {
  101. JObject jname = JObject.Parse(jret["img_name"].ToString());
  102. degree = Convert.ToDouble(jname["degree"].ToString());
  103. direction = Convert.ToInt32(jname["direction"].ToString());
  104. state = Convert.ToInt32(jname["state"].ToString());
  105. }
  106. else
  107. {
  108. state = 0;
  109. direction = 0;
  110. degree = 0;
  111. }
  112. }
  113. /// <summary>
  114. /// 计算切割点位置
  115. /// </summary>
  116. /// <param name="imagePath">图片路径</param>
  117. /// <param name="imageType">孔类型</param>
  118. /// <param name="firm">厂家类型</param>
  119. /// <returns>json字符串</returns>
  120. public void Img_Cut_Position(String imagePath, int imageType, String firm, out double offsetx, out double offsety, out int state)
  121. {
  122. this.webServer_Path = "test2";
  123. Update_Url();
  124. JObject json = new JObject();
  125. json.Add("img_path", imagePath);
  126. json.Add("img_type", imageType);
  127. json.Add("firm", firm);
  128. String res = RequestString(JsonConvert.SerializeObject(json));
  129. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  130. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  131. {
  132. JObject jname = JObject.Parse(jret["img_name"].ToString());
  133. JArray jlocation = JArray.Parse(jname["location"].ToString());
  134. offsetx = Convert.ToDouble(jlocation[0].ToString());
  135. offsety = Convert.ToDouble(jlocation[1].ToString());
  136. state = Convert.ToInt32(jname["state"].ToString());
  137. }
  138. else
  139. {
  140. state = 0;
  141. offsetx = 0;
  142. offsety = 0;
  143. }
  144. }
  145. /// <summary>
  146. /// 是否切割成功验证
  147. /// </summary>
  148. /// <param name="imageBefore">切割前图像路径</param>
  149. /// <param name="imageAfter">切割后图像路径</param>
  150. /// <returns>json字符串</returns>
  151. public void Img_Cut_Success(String imageBefore, String imageAfter, out int state)
  152. {
  153. this.webServer_Path = "FIB_verify";
  154. Update_Url();
  155. JObject json = new JObject();
  156. json.Add("img_before_path", imageBefore);
  157. json.Add("img_after_path", imageAfter);
  158. String res = RequestString(JsonConvert.SerializeObject(json));
  159. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  160. if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "")
  161. {
  162. state = Convert.ToInt32(jret["state"].ToString());
  163. }
  164. else
  165. {
  166. state = 0;
  167. }
  168. }
  169. /// <summary>
  170. /// 计算切割后图像梯形区域上边中心点坐标
  171. /// </summary>
  172. /// <param name="imagePath">图片路径</param>
  173. /// <returns>json字符串</returns>
  174. public void Img_Trapezoid_Top_Center_Position(String imagePath, out double offsetx, out double offsety, out int state)
  175. {
  176. this.webServer_Path = "test4";
  177. Update_Url();
  178. JObject json = new JObject();
  179. json.Add("img_path", imagePath);
  180. String res = RequestString(JsonConvert.SerializeObject(json));
  181. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  182. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  183. {
  184. JObject jname = JObject.Parse(jret["img_name"].ToString());
  185. JArray jlocation = JArray.Parse(jname["top_center"].ToString());
  186. offsetx = Convert.ToDouble(jlocation[0].ToString());
  187. offsety = Convert.ToDouble(jlocation[1].ToString());
  188. state = Convert.ToInt32(jname["state"].ToString());
  189. }
  190. else
  191. {
  192. state = 0;
  193. offsetx = 0;
  194. offsety = 0;
  195. }
  196. }
  197. /// <summary>
  198. /// 自动对焦
  199. /// </summary>
  200. /// <param name="imagePath">一组图片路径</param>
  201. /// <returns>json字符串</returns>
  202. public String Img_Auto_Focus(List<String> imagePath)
  203. {
  204. this.webServer_Path = "test5";
  205. Update_Url();
  206. JObject json = new JObject();
  207. for(int i=0;i<imagePath.Count;i++)
  208. {
  209. json.Add("img_path" + i.ToString(), imagePath[i]);
  210. }
  211. String res = RequestString(JsonConvert.SerializeObject(json));
  212. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  213. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  214. {
  215. return jret["img_path"].ToString();
  216. }
  217. else
  218. {
  219. return "";
  220. }
  221. }
  222. /// <summary>
  223. /// 自动消像散
  224. /// </summary>
  225. /// <param name="imagePath">一组图片路径</param>
  226. /// <returns>json字符串</returns>
  227. public String Img_Auto_Stigmatic(List<String> imagePath)
  228. {
  229. this.webServer_Path = "test6";
  230. Update_Url();
  231. JObject json = new JObject();
  232. for (int i = 0; i < imagePath.Count; i++)
  233. {
  234. json.Add("img_path" + i.ToString(), imagePath[i]);
  235. }
  236. String res = RequestString(JsonConvert.SerializeObject(json));
  237. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  238. if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "")
  239. {
  240. return jret["img_path"].ToString();
  241. }
  242. else
  243. {
  244. return "";
  245. }
  246. }
  247. /// <summary>
  248. /// 计算切割面区域中心坐标,以及偏移角度及方向
  249. /// </summary>
  250. /// <param name="imagePath">图片路径</param>
  251. /// <returns>json字符串</returns>
  252. public void Img_Center_Position_OffsetAngle_Direction(String imagePath, int imageType, String firm, out double offsetx, out double offsety, out double degree, out int direction, out int state)
  253. {
  254. this.webServer_Path = "test7";
  255. Update_Url();
  256. JObject json = new JObject();
  257. json.Add("img_path", imagePath);
  258. json.Add("img_type", imageType);
  259. json.Add("firm", firm);
  260. String res = RequestString(JsonConvert.SerializeObject(json));
  261. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  262. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  263. {
  264. JObject jname = JObject.Parse(jret["img_name"].ToString());
  265. JArray jlocation = JArray.Parse(jname["center"].ToString());
  266. offsetx = Convert.ToDouble(jlocation[0].ToString());
  267. offsety = Convert.ToDouble(jlocation[1].ToString());
  268. state = Convert.ToInt32(jname["state"].ToString());
  269. degree = Convert.ToDouble(jname["degree"].ToString());
  270. direction = Convert.ToInt32(jname["direction"].ToString());
  271. }
  272. else
  273. {
  274. state = 0;
  275. direction = 0;
  276. degree = 0;
  277. offsetx = 0;
  278. offsety = 0;
  279. }
  280. }
  281. /// <summary>
  282. /// 测量尺寸
  283. /// </summary>
  284. /// <param name="imagePath">图片路径</param>
  285. /// <param name="ratio">放大倍数</param>
  286. /// <param name="size">一像素代表**mm/um</param>
  287. /// <returns>json字符串</returns>
  288. public void Img_Measure_Size(String imagePath, String ratio, String size, out int state)
  289. {
  290. this.webServer_Path = "test8";
  291. Update_Url();
  292. JObject json = new JObject();
  293. json.Add("img_path", imagePath);
  294. json.Add("ratio", ratio);
  295. json.Add("size", size);
  296. String res = RequestString(JsonConvert.SerializeObject(json));
  297. JObject jret = (JObject)JsonConvert.DeserializeObject(res);
  298. if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "")
  299. {
  300. JObject jstate = JObject.Parse(jret["img_name"].ToString());
  301. state = Convert.ToInt32(jstate["state"].ToString());
  302. }
  303. else
  304. {
  305. state = 0;
  306. }
  307. }
  308. }
  309. }