WebResult.cs 11 KB

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