WebResult.cs 11 KB

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