WebResult.cs 13 KB

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