RemoteDecodeHelper.cs
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using CodeLibrary;
using DeviceLibrary;
using HalconDotNet;
using Newtonsoft.Json;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
public class RemoteDecodeHelper_mod
{
static bool IsIDCamera = ConfigHelper.Config.Get<bool>("isStartscanningcode");
static int webclienttimeout = 30 * 1000;
static Process p = new Process();
const string serverhost = "http://127.0.0.1:58137/";
public static WebResultCode DecodeRequest(Bitmap bitmap, RemoteDecodeParam remoteDecodeParam)
{
CheckAndRunServer();
if (bitmap == null)
return null;
byte[] requestdata;
lock (bitmap)
{
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, bitmap);
requestdata = mStream.ToArray();
mStream.Close();
}
}
return DecodeRequest(requestdata, remoteDecodeParam);
}
static WebResultCode DecodeRequest(byte[] requestdata, RemoteDecodeParam remoteDecodeParam,bool isHObject=false)
{
//CheckAndRunServer();
string param;
using (MemoryStream mStreamparam = new MemoryStream())
{
XmlSerializer xf = new XmlSerializer(typeof(RemoteDecodeParam));
xf.Serialize(mStreamparam, remoteDecodeParam);
param = base64UrlEncode(mStreamparam.ToArray());
mStreamparam.Close();
}
string url = serverhost+ "NeoScan/ProcessBitmap?param=";
if (isHObject)
url = serverhost+ "NeoScan/Process?param=";
byte[] resp;
try
{
MyWebClient webClient = new MyWebClient(webclienttimeout);
resp = webClient.UploadData(url + param, requestdata);
requestdata = null;
webClient.Dispose();
}
catch(WebException we)
{
return null;
}
catch
{
return null;
}
WebResultCode codeInfos = null;
var ss = Encoding.UTF8.GetString(resp);
try
{
codeInfos = JsonConvert.DeserializeObject<WebResultCode>(ss);
}
catch (Exception ex)
{
throw new Exception("数据解析出错:" + ex + "\r\n" + ss);
}
return codeInfos;
}
#region 扫码相机请求ns100方法
public static WebResultCode NeoSacnRequest(Bitmap bitmap, List<CodeInfo> codeInfos,int PlateW)
{
WebResultCode resultCode = null;
try
{
CheckAndRunServer();
BitmapData bitmapData = new BitmapData();
string iamgestr = BitmapToBase64(bitmap);
List<BarcodeInfos> barcodeInfos = new List<BarcodeInfos>();
codeInfos.ForEach(c =>
{
barcodeInfos.Add(new BarcodeInfos
{
Text = c.CodeStr,
CodeType = c.CodeType,
Angle = (float)c.Orientation,
Distance = c.LabelDistance,
X=c.X, Y=c.Y
});
});
bitmapData.ImageData = iamgestr;
bitmapData.BarCodeList = barcodeInfos;
bitmapData.X = RobotManage.Config.Right_Batch_X;
bitmapData.Y = RobotManage.Config.Right_Batch_Y;
bitmapData.IsIDCamera = IsIDCamera;
bitmapData.PlateW = PlateW;
string methodname = "NeoScan/ProcessBitmaps";
string repststr=NeoSacnPost(bitmapData, methodname);
resultCode = JsonConvert.DeserializeObject<WebResultCode>(repststr);
//因为传值时未传入条码中心点,所以在这里,还是用
resultCode.workCodeInfo = new List<BarcodeInfo>();
codeInfos.ForEach((c) => {
resultCode.workCodeInfo.Add(new BarcodeInfo
{
Text = c.CodeStr,
CodeType = c.CodeType,
Angle = (float)c.Orientation,
Center = new PointF(c.X,c.Y),
Distance = c.LabelDistance,
}); ;
});
}
catch (Exception ex)
{
LogUtil.error($"请求错误:{ex.Message}");
}
return resultCode;
}
public static WebResultCode NeoSacnRequest_ordinary(Bitmap bitmap,int PlateW)
{
WebResultCode resultCode = null;
try
{
CheckAndRunServer();
BitmapData bitmapData = new BitmapData();
string iamgestr = BitmapToBase64(bitmap);
List<BarcodeInfos> barcodeInfos = new List<BarcodeInfos>();
bitmapData.ImageData = iamgestr;
bitmapData.BarCodeList = barcodeInfos;
bitmapData.X = RobotManage.Config.Right_Batch_X;
bitmapData.Y = RobotManage.Config.Right_Batch_Y;
bitmapData.IsIDCamera = IsIDCamera;
bitmapData.PlateW = PlateW;
string methodname = "NeoScan/ProcessBitmaps";
string repststr = NeoSacnPost(bitmapData, methodname);
resultCode = JsonConvert.DeserializeObject<WebResultCode>(repststr);
}
catch (Exception ex)
{
LogUtil.error($"请求错误:{ex.Message}");
}
return resultCode;
}
public class BitmapData
{
public string ImageData { get; set; }
public List<BarcodeInfos> BarCodeList { get; set; }
/// <summary>
/// xy轴中心线
/// </summary>
public int X { get; set; }
public int Y { get; set; }
/// <summary>
/// 是否扫码相机
/// </summary>
public bool IsIDCamera { get; set; }
/// <summary>
/// 料盘宽度
/// </summary>
public int PlateW { get; set; }
}
public class BarcodeInfos
{
//
// 摘要:
// 文本
public string Text { get; set; }
//
// 摘要:
// 条码类型
public string CodeType { get; set; }
////
//// 摘要:
//// 中心点
//public PointF Center { get; set; }
/// <summary>
/// 条码xy轴
/// </summary>
public int X { get; set; }
public int Y { get; set; }
////
//// 摘要:
//// 条码尺寸大小
//public SizeF Size { get; set; }
//
// 摘要:
// 角度,3点钟方向0°,逆时针为正,顺时针为负。
public float Angle { get; set; }
//
// 摘要:
// 原点垂直于经过中心点的直线的距离
public float Distance { get; set; }
}
static byte[] BitmapToByteArray(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
// 将 Bitmap 保存到 MemoryStream
bitmap.Save(stream, bitmap.RawFormat);
// 返回 MemoryStream 中的字节数组
return stream.ToArray();
}
}
static string BitmapToBase64(Bitmap bitmap)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 将Bitmap保存到MemoryStream中
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// 获取字节数组
byte[] byteArray = memoryStream.ToArray();
// 将字节数组转换为Base64编码的字符串
return Convert.ToBase64String(byteArray);
}
}
public static string NeoSacnPost(BitmapData bitmapData,string methodname)
{
string url = serverhost + methodname;
string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(bitmapData);
using (HttpClient httpClient = new HttpClient())
{
StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
string res= response.Content.ReadAsStringAsync().Result;
return res;
}
else
{
LogUtil.error($"请求失败返回状态码:{response.StatusCode};");
return null;
}
}
}
#endregion
/// <summary>
/// 在url中传递base64字符串需要替换加号等符号
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static string base64UrlEncode(byte[] input)
{
return Convert.ToBase64String(input).Replace('+', '-').Replace('/', '_');
}
/// <summary>
/// 检查并自动启动服务器
/// </summary>
static void CheckAndRunServer()
{
lock (p)
{
Process proes = new Process();
var pss = Process.GetProcessesByName("Neo Scan");
if (pss.Length > 0)
{
LogUtil.info("ns100服务已启动;");
return;
}
var f = ConfigHelper.Config.Get("Display_GetDataAppPath");
string path = Path.Combine(Application.StartupPath, f);
LogUtil.info("启动ns100服务:"+path);
if (!File.Exists(path))
{
LogUtil.error("找不到扫码服务器文件");
return;
};
p.StartInfo = new ProcessStartInfo(path);
p.StartInfo.Arguments = "hide";
p.Start();
int checkcount = 5;
while (checkcount > 0)
{
try
{
checkcount--;
Thread.Sleep(1000);
MyWebClient webClient = new MyWebClient(webclienttimeout);
var s = webClient.DownloadString(serverhost + "alive");
if (s.Trim() == "\"1\"")
{
LogUtil.info("ns100服务启动成功;");
return;
}
}
catch (Exception)
{
}
}
LogUtil.error("扫码服务器打开失败");
}
}
[Serializable]
public struct RemoteDecodeParam
{
public string[] codeTypeList;
public int codeCount;
public int timeout;
}
[Serializable]
public class WebResultCode
{
public int ErrorCode { get; set; }
public string Msg { get; set; }
//public WebCodeText[] Data { get; set; }
public List<KeyValuePair<string,string>> workCodeKeyword { get; set; }
public List<BarcodeInfo> workCodeInfo;
public WebResultCode()
{
ErrorCode = 0;
Msg = "OK";
}
/// <summary>
///计算好的角度
/// </summary>
public NewPositionAngle positionAngle { get; set; }
}
//
// 摘要:
// 条码信息,1DBarcode、2DBarcode
[Serializable]
public class BarcodeInfo
{
//
// 摘要:
// 文本
public string Text { get; set; }
//
// 摘要:
// 条码类型
public string CodeType { get; set; }
//
// 摘要:
// 中心点
public PointF Center { get; set; }
//
// 摘要:
// 角度,3点钟方向0°,逆时针为正,顺时针为负。
public float Angle { get; set; }
//
// 摘要:
// 条码尺寸大小
//public SizeF Size { get; set; }
//
// 摘要:
// 原点垂直于经过中心点的直线的距离
public float Distance { get; set; }
}
public class MyWebClient : WebClient
{
private int _timeout;
public MyWebClient(int timeout)
{
this._timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
}
}
public class NewPositionAngle
{
public int X { get; set; }
public int Y { get; set; }
public int Angle { get; set; }
public bool[] IsCodeUsed { get; set; }
}
}