Commit 7be387a5 刘韬

修正xml不兼容不可见字符

1 个父辈 6ee44f36
...@@ -38,6 +38,9 @@ namespace ScanCodeServer ...@@ -38,6 +38,9 @@ namespace ScanCodeServer
this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.textBox_addr = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.contextMenuStrip1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
...@@ -113,11 +116,41 @@ namespace ScanCodeServer ...@@ -113,11 +116,41 @@ namespace ScanCodeServer
this.label2.TabIndex = 2; this.label2.TabIndex = 2;
this.label2.Text = "自动重启内存阈值"; this.label2.Text = "自动重启内存阈值";
// //
// textBox_addr
//
this.textBox_addr.Location = new System.Drawing.Point(197, 279);
this.textBox_addr.Name = "textBox_addr";
this.textBox_addr.Size = new System.Drawing.Size(227, 21);
this.textBox_addr.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(65, 279);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(126, 25);
this.label3.TabIndex = 2;
this.label3.Text = "服务监听地址";
//
// button1
//
this.button1.Location = new System.Drawing.Point(442, 279);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(69, 23);
this.button1.TabIndex = 4;
this.button1.Text = "应用";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox_addr);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.numericUpDown1); this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
...@@ -140,6 +173,9 @@ namespace ScanCodeServer ...@@ -140,6 +173,9 @@ namespace ScanCodeServer
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
private System.Windows.Forms.NumericUpDown numericUpDown1; private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox_addr;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1;
} }
} }
using System; using ConfigHelper;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
...@@ -15,10 +16,11 @@ namespace ScanCodeServer ...@@ -15,10 +16,11 @@ namespace ScanCodeServer
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
const string server = "http://0.0.0.0:58137"; string server = "http://0.0.0.0:58137";
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
server = Config.Get("Addr", "http://0.0.0.0:58137");
WebServer.Open(server); WebServer.Open(server);
FormClosing += Form1_FormClosing; FormClosing += Form1_FormClosing;
Shown += Form1_Shown; Shown += Form1_Shown;
...@@ -29,6 +31,7 @@ namespace ScanCodeServer ...@@ -29,6 +31,7 @@ namespace ScanCodeServer
private void Monitor_OverTimes(object sender, EventArgs e) private void Monitor_OverTimes(object sender, EventArgs e)
{ {
this.Invoke((EventHandler)delegate { this.Invoke((EventHandler)delegate {
Common.log.Error($"内存达到阈值触发程序退出:已调用识别:{Monitor.ScanTimes}次,当前占用内存:{Monitor.ScanMemory}mb");
Thread.Sleep(1000); Thread.Sleep(1000);
退出ToolStripMenuItem_Click(null, EventArgs.Empty); 退出ToolStripMenuItem_Click(null, EventArgs.Empty);
},sender,e); },sender,e);
...@@ -39,6 +42,7 @@ namespace ScanCodeServer ...@@ -39,6 +42,7 @@ namespace ScanCodeServer
bool needclose = false; bool needclose = false;
private void Form1_FormClosing(object sender, FormClosingEventArgs e) private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ {
if (!needclose) if (!needclose)
{ {
e.Cancel = true; e.Cancel = true;
...@@ -48,9 +52,11 @@ namespace ScanCodeServer ...@@ -48,9 +52,11 @@ namespace ScanCodeServer
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
numericUpDown1.Value = ConfigHelper.Config.Get<int>("MaxMenory", 5000); numericUpDown1.Value = ConfigHelper.Config.Get<int>("MaxMenory", 8000);
Monitor.MaxMenory = (int)numericUpDown1.Value; Monitor.MaxMenory = (int)numericUpDown1.Value;
numericUpDown1.ValueChanged += numericUpDown1_ValueChanged; numericUpDown1.ValueChanged += numericUpDown1_ValueChanged;
textBox_addr.Text = server;
} }
private void timer1_Tick(object sender, EventArgs e) private void timer1_Tick(object sender, EventArgs e)
...@@ -108,5 +114,21 @@ namespace ScanCodeServer ...@@ -108,5 +114,21 @@ namespace ScanCodeServer
{ {
} }
private void button1_Click(object sender, EventArgs e)
{
try
{
var u = new Uri(textBox_addr.Text);
Config.Set("Addr", u.ToString());
Config.SaveChange();
WebServer.Close();
server = textBox_addr.Text;
WebServer.Open(server);
}
catch {
MessageBox.Show("输入的格式不正确");
}
}
} }
} }
...@@ -108,7 +108,7 @@ namespace ScanCodeServer ...@@ -108,7 +108,7 @@ namespace ScanCodeServer
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle); HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
} }
// HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 3000); // HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 3000);
ho_SymbolXLDs.Dispose(); //ho_SymbolXLDs.Dispose();
// set_data_code_2d_param(DataCodeHandle, 'timeout', 200) // set_data_code_2d_param(DataCodeHandle, 'timeout', 200)
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", timeOut); HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", timeOut);
//HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "string_encoding", "raw"); //HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "string_encoding", "raw");
...@@ -128,7 +128,9 @@ namespace ScanCodeServer ...@@ -128,7 +128,9 @@ namespace ScanCodeServer
ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs); ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs);
} }
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle); HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
//HOperatorSet.ClearAllDataCode2dModels();
//HOperatorSet.ClearDistanceTransformXld(ho_SymbolXLDs);
if (hv_DecodedDataStrings.Length > 0) if (hv_DecodedDataStrings.Length > 0)
{ {
string[] resultList = hv_DecodedDataStrings.SArr; string[] resultList = hv_DecodedDataStrings.SArr;
...@@ -189,6 +191,7 @@ namespace ScanCodeServer ...@@ -189,6 +191,7 @@ namespace ScanCodeServer
catch (Exception ex) catch (Exception ex)
{ {
Common.log.Error("DecodeCode出错:" + ex.ToString()); Common.log.Error("DecodeCode出错:" + ex.ToString());
Console.WriteLine("DecodeCode出错:" + ex.ToString());
Common.log.Debug(" DecodeCode[" + symbolType + "][" + hv_model_path + "][" + codeCount + "] 结束,返回数量:" + codeList.Count); Common.log.Debug(" DecodeCode[" + symbolType + "][" + hv_model_path + "][" + codeCount + "] 结束,返回数量:" + codeList.Count);
return codeList; return codeList;
} }
......
...@@ -10,7 +10,7 @@ namespace ScanCodeServer ...@@ -10,7 +10,7 @@ namespace ScanCodeServer
{ {
public static int ScanMemory = 0; public static int ScanMemory = 0;
public static int ScanTimes = 0; public static int ScanTimes = 0;
public static int MaxMenory=200; public static int MaxMenory=5000;
public static event EventHandler OverTimes; public static event EventHandler OverTimes;
static bool overtimes = false; static bool overtimes = false;
public static void Test() { public static void Test() {
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
...@@ -63,6 +64,7 @@ ...@@ -63,6 +64,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Common.cs" /> <Compile Include="Common.cs" />
<Compile Include="eyemDecode2.cs" />
<Compile Include="Form1.cs"> <Compile Include="Form1.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
......
...@@ -8,6 +8,8 @@ using System.Runtime.Serialization.Formatters.Binary; ...@@ -8,6 +8,8 @@ using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization; using System.Xml.Serialization;
using System.Text; using System.Text;
using System.Drawing; using System.Drawing;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ScanCodeServer namespace ScanCodeServer
{ {
...@@ -32,65 +34,105 @@ namespace ScanCodeServer ...@@ -32,65 +34,105 @@ namespace ScanCodeServer
BinaryFormatter bf = new BinaryFormatter(); BinaryFormatter bf = new BinaryFormatter();
Bitmap bitmap = (Bitmap)bf.Deserialize(info); Bitmap bitmap = (Bitmap)bf.Deserialize(info);
HDCodeHelper.Bitmap2HObjectBpp24(bitmap, out HObject ho_Image); HDCodeHelper.Bitmap2HObjectBpp24(bitmap, out HObject ho_Image);
bitmap.Dispose(); //bitmap.Dispose();
return Process(ho_Image, param); return Process(ho_Image, bitmap, GetRemoteDecodeParam(param));
} }
public string Process(Stream info, string param) public string Process(Stream info, string param)
{ {
HObject ho_Image = HObject.Deserialize(info); HObject ho_Image = HObject.Deserialize(info);
return Process(ho_Image, param); return Process(ho_Image,null, GetRemoteDecodeParam(param));
} }
object o = new object(); object o = new object();
string Process(HObject ho_Image, string param) string Process(HObject ho_Image,Bitmap bitmap, RemoteDecodeParam remoteDecodeParam)
{ {
param = param.Replace('-', '+').Replace('_', '/');
var stream = Convert.FromBase64String(param);
MemoryStream mStream = new MemoryStream(stream);
XmlSerializer xf = new XmlSerializer(typeof(RemoteDecodeParam));
RemoteDecodeParam remoteDecodeParam = (RemoteDecodeParam)xf.Deserialize(mStream);
mStream.Close();
string[] codeTypeList = remoteDecodeParam.codeTypeList; string[] codeTypeList = remoteDecodeParam.codeTypeList;
int codeCount = remoteDecodeParam.codeCount; int codeCount = remoteDecodeParam.codeCount;
int timeout = remoteDecodeParam.timeout; int timeout = remoteDecodeParam.timeout;
Common.log.Info($"参数:codeTypeList:{string.Join(",",codeTypeList)}, codeCount:{codeCount}, timeout:{timeout}");
Result res = new Result(); Result res = new Result();
//HObject ho_Image = HObject.Deserialize(info);
List<CodeInfo> cc = new List<CodeInfo>();
List<string> code = new List<string>(); List<string> code = new List<string>();
Task[] t = new Task[codeTypeList.Length];
int ti = 0;
foreach (string codeType in codeTypeList) foreach (string codeType in codeTypeList)
{ {
lock (o) { t[ti] = Task.Run(() =>
Monitor.ScanTimes++;
}
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else
{
cc = HDCodeHelper.DecodeCode(ho_Image, codeType, HDCodeHelper.GetCodeParamFilePath(codeType), codeCount, timeout);
}
foreach (CodeInfo c in cc)
{ {
c.CodeType = codeType; lock (o)
if (!code.Contains(c.CodeStr))
{ {
Common.log.Info("识别到:"+c.CodeType+", " + c.CodeStr); Monitor.ScanTimes++;
code.Add(c.CodeStr);
res.CodeInfos.Add(c);
} }
} try
{
List<CodeInfo> cc = new List<CodeInfo>();
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else if (codeType.ToLower().StartsWith("eyem") && bitmap != null)
{
var b = DeepClone(bitmap);
cc = EyemDecode2.Decoder(ref b);
b.Dispose();
}
else
{
cc = HDCodeHelper.DecodeCode(ho_Image, codeType, HDCodeHelper.GetCodeParamFilePath(codeType), codeCount, timeout);
}
if (cc != null)
{
foreach (CodeInfo c in cc)
{
c.CodeType = codeType;
c.CodeStr = RemoveInvalidXMLChars(c.CodeStr);
//c.CodeStr = "<![CDATA[" + c.CodeStr+"]]";
lock (code)
{
if (!code.Contains(c.CodeStr))
{
Common.log.Info("识别到:" + c.CodeType + ", " + c.CodeStr);
code.Add(c.CodeStr);
res.CodeInfos.Add(c);
}
}
}
}
}
catch (Exception ex)
{
Common.log.Error(ex.ToString());
}
finally
{
}
});
ti++;
}
Task.WaitAll(t,1000*10);
ho_Image.Dispose();
if (bitmap != null)
bitmap.Dispose();
using (MemoryStream respStream = new MemoryStream())
{
XmlSerializer xff = new XmlSerializer(typeof(List<CodeInfo>));
xff.Serialize(respStream, res.CodeInfos);
Monitor.Test();
return Convert.ToBase64String(respStream.ToArray());
} }
MemoryStream respStream = new MemoryStream();
XmlSerializer xff = new XmlSerializer(typeof(List<CodeInfo>));
xff.Serialize(respStream, res.CodeInfos);
Monitor.Test();
return Convert.ToBase64String(respStream.ToArray());
} }
RemoteDecodeParam GetRemoteDecodeParam(string param) {
param = param.Replace('-', '+').Replace('_', '/');
var stream = Convert.FromBase64String(param);
MemoryStream mStream = new MemoryStream(stream);
XmlSerializer xf = new XmlSerializer(typeof(RemoteDecodeParam));
RemoteDecodeParam remoteDecodeParam = (RemoteDecodeParam)xf.Deserialize(mStream);
mStream.Close();
return remoteDecodeParam;
}
private Result TakeOld(string place) private Result TakeOld(string place)
{ {
...@@ -98,7 +140,41 @@ namespace ScanCodeServer ...@@ -98,7 +140,41 @@ namespace ScanCodeServer
return res; return res;
} }
public static T DeepClone<T>(T _object)
{
try
{
T dstobject;
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, _object);
mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
dstobject = (T)bf.Deserialize(mStream);
mStream.Close();
}
return dstobject;
}
catch (Exception e)
{
Common.log.Error("DeepClone" + e.ToString());
return default;
}
}
// filters control characters but allows only properly-formed surrogate sequences
private static Regex _invalidXMLChars = new Regex(
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
RegexOptions.Compiled);
/// <summary>
/// removes any unusual unicode characters that can't be encoded into XML
/// </summary>
public static string RemoveInvalidXMLChars(string text)
{
if (string.IsNullOrEmpty(text)) return "";
return _invalidXMLChars.Replace(text, "");
}
} }
public struct RemoteDecodeParam public struct RemoteDecodeParam
{ {
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!