Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
刘韬
/
HalconScanCodeServer
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 7be387a5
由
刘韬
编写于
2022-01-19 19:05:29 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
修正xml不兼容不可见字符
1 个父辈
6ee44f36
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
409 行增加
和
43 行删除
ScanCodeServer/Form1.Designer.cs
ScanCodeServer/Form1.cs
ScanCodeServer/HDCodeHelper.cs
ScanCodeServer/Monitor.cs
ScanCodeServer/ScanCodeServer.csproj
ScanCodeServer/WebWork.cs
ScanCodeServer/eyemDecode2.cs
ScanCodeServer/Form1.Designer.cs
查看文件 @
7be387a
...
@@ -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
;
}
}
}
}
ScanCodeServer/Form1.cs
查看文件 @
7be387a
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"
,
5
000
);
numericUpDown1
.
Value
=
ConfigHelper
.
Config
.
Get
<
int
>(
"MaxMenory"
,
8
000
);
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
(
"输入的格式不正确"
);
}
}
}
}
}
}
ScanCodeServer/HDCodeHelper.cs
查看文件 @
7be387a
...
@@ -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
;
}
}
...
...
ScanCodeServer/Monitor.cs
查看文件 @
7be387a
...
@@ -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
=
2
00
;
public
static
int
MaxMenory
=
50
00
;
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
()
{
...
...
ScanCodeServer/ScanCodeServer.csproj
查看文件 @
7be387a
...
@@ -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>
...
...
ScanCodeServer/WebWork.cs
查看文件 @
7be387a
...
@@ -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
p
aram
)
string
Process
(
HObject
ho_Image
,
Bitmap
bitmap
,
RemoteDecodeParam
remoteDecodeP
aram
)
{
{
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
{
{
...
...
ScanCodeServer/eyemDecode2.cs
0 → 100644
查看文件 @
7be387a
using
Microsoft.Win32.SafeHandles
;
using
System
;
using
System.Collections.Generic
;
using
System.Drawing
;
using
System.Drawing.Imaging
;
using
System.IO
;
using
System.Linq
;
using
System.Runtime.InteropServices
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
ScanCodeServer
{
public
unsafe
class
EyemDecode2
{
public
static
string
codeType
=
"QR_CODE|DATA_MATRIX"
;
public
static
int
Init
()
{
if
(!
File
.
Exists
(
".\\darknet\\detect-tiny.cfg"
)
||
!
File
.
Exists
(
".\\darknet\\detect-tiny.weights"
))
{
throw
new
Exception
(
"找不到darknet\\detect-tiny.cfg|detect-tiny.weights"
);
}
return
eyemInitNNDataCodeModel
(
".\\darknet\\detect-tiny.cfg"
,
".\\darknet\\detect-tiny.weights"
,
""
,
""
);
}
static
EyemDecode2
()
{
Init
();
}
//public static List<CodeInfo> Decoder(ref Bitmap bmap, string file = null) {
// return Decoder(ref bmap, file);
//}
/// <summary>
/// 识别图像二维码
/// </summary>
/// <param name="bmap">需要手动释放这个bitmap</param>
/// <param name="file">如果传文件的话,bmp传null</param>
/// <returns></returns>
public
static
List
<
CodeInfo
>
Decoder
(
ref
Bitmap
bmap
,
string
file
=
null
)
{
List
<
CodeInfo
>
codelist
=
new
List
<
CodeInfo
>();
try
{
//创建图像引用
EyemImage
eyemImage
;
BitmapData
bitmapData
=
null
;
if
(
file
==
null
)
{
Rectangle
rect
=
new
Rectangle
(
0
,
0
,
bmap
.
Width
,
bmap
.
Height
);
bitmapData
=
bmap
.
LockBits
(
rect
,
ImageLockMode
.
ReadWrite
,
bmap
.
PixelFormat
);
eyemImage
=
new
EyemImage
();
eyemImage
.
ucpImage
=
bitmapData
.
Scan0
;
eyemImage
.
iWidth
=
rect
.
Width
;
eyemImage
.
iHeight
=
rect
.
Height
;
eyemImage
.
iChannels
=
1
;
eyemImage
.
iDepth
=
0
;
}
else
{
eyemImageRead
(
file
,
-
1
,
out
eyemImage
);
}
//创建扫描区域
EyemRect
eyemRect
=
new
EyemRect
();
eyemRect
.
iXs
=
0
;
eyemRect
.
iYs
=
0
;
eyemRect
.
iWidth
=
eyemImage
.
iWidth
;
eyemRect
.
iHeight
=
eyemImage
.
iHeight
;
//string codeType = "QR_CODE|DATA_MATRIX"; //QRCode
int
ipNum
;
EyemBarCode
*
tpResults
;
DataCodeHandle
hObject
=
null
;
try
{
EyemImage
tpDstImg
;
int
result
=
eyemDetectAndDecodeUseNN
(
eyemImage
,
eyemRect
,
out
hObject
,
out
tpResults
,
out
ipNum
,
out
_
);
// eyemImageFree(ref tpDstImg);
//hObject.Dispose();
//int result = eyemDetectAndDecode(eyemImage, eyemRect, "", codeType, out hObject, out tpResults, out ipNum, false, iBlockSize, iRangeC, iSymbolMin, iSymbolMax);
Common
.
log
.
Info
(
$
"eyemDetectAndDecode:{result},ipNum:{ipNum}"
);
if
(
result
!=
0
||
ipNum
==
0
)
return
codelist
;
for
(
int
i
=
0
;
i
<
ipNum
;
i
++)
{
CodeInfo
ci
=
new
CodeInfo
(
Marshal
.
PtrToStringAnsi
(
tpResults
[
i
].
hText
),
tpResults
[
i
].
iCenterX
,
tpResults
[
i
].
iCenterY
);
ci
.
CodeType
=
Marshal
.
PtrToStringAnsi
(
tpResults
[
i
].
hType
);
codelist
.
Add
(
ci
);
}
}
catch
(
Exception
ex
)
{
Common
.
log
.
Error
(
$
""
+
ex
.
ToString
());
}
finally
{
if
(
hObject
!=
null
)
hObject
.
Dispose
();
if
(
bitmapData
!=
null
)
bmap
.
UnlockBits
(
bitmapData
);
if
(
file
!=
null
)
eyemImageFree
(
ref
eyemImage
);
//bmap.Dispose();
}
}
catch
(
Exception
ex
)
{
Common
.
log
.
Error
(
ex
.
ToString
());
}
//eyemImageFree(eyemImage.ucpImage);
return
codelist
;
}
//释放解码句柄
public
class
DataCodeHandle
:
SafeHandleZeroOrMinusOneIsInvalid
{
public
DataCodeHandle
()
:
base
(
true
)
{
}
protected
override
bool
ReleaseHandle
()
{
return
eyemDetectAndDecodeFree
(
handle
);
}
}
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
private
static
extern
int
eyemDetectAndDecode
(
EyemImage
tpImage
,
EyemRect
tpRoi
,
string
fileName
,
string
strCodeType
,
out
DataCodeHandle
hObject
,
out
EyemBarCode
*
tpResults
,
out
int
ipNum
,
bool
bUseNiBlack
,
int
iBlockSize
,
int
iRangeC
,
int
iSymbolMin
,
int
iSymbolMax
,
double
dScaleUpAndDown
=
1
,
double
dToleErr
=
0.5
,
double
dMinorStep
=
1.0
);
/*
* 参数说明
tpImage 输入图像,可使用”eyemImageRead“接口读取图片(需与“eyemImageFree”配合使用),也可直接创建EyemImage对象。
tpRoi 感兴趣区域,尺寸不能小于iSymbolMax。
fileName 文件名,可不填。
strCodeType 所要识别条码类型,“QRCode|DataMatrix|CODE_128”等等。
hObject 结果释放句柄,配合eyemDetectAndDecodeFree使用
tpResults 识别结果
ipNum 结果数量
bUseNiBlack 使用NiBlack二值化,默认值 false。
iBlockSize 定位块大小(奇数),略大于最大定位块(只二维码),佳世达一般设置为11。
iRangeC 搜索范围,范围越大识别越好时间也相应越长,佳世达一般设置为5。
iSymbolMin 最小二维码尺寸,默认128。
iSymbolMax 最大二维码尺寸,佳世达一般设置为215。
dScaleUpAndDown 缩放,<1表示缩小,>1表示放大,=1表示不缩放
dToleErr 最大允许误差默认50%,越小越严格但可能会识别不到。
dMinorStep 步进,默认1.0。
//返回值
-3 图像不存在
-1 内存不足
0 正常
-100 未识别到条码或参数设置不正确
*/
/// <summary>
/// 读取图像
/// </summary>
/// <param name="filename"></param>
/// <param name="iFalgs"></param>
/// <param name="ucpImage"></param>
/// <returns></returns>
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
private
static
extern
int
eyemImageRead
(
string
filename
,
int
iFalgs
,
out
EyemImage
ucpImage
);
/// <summary>
/// 释放图像资源
/// </summary>
/// <param name="eyemImage"></param>
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
public
static
extern
void
eyemImageFree
(
ref
EyemImage
eyemImage
);
//释放解码句柄
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
private
static
extern
bool
eyemDetectAndDecodeFree
(
IntPtr
hObject
);
//基于深度学习读码程序(仅支持QR、DataMatrix)
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
private
static
extern
int
eyemDetectAndDecodeUseNN
(
EyemImage
tpImage
,
EyemRect
tpRoi
,
out
DataCodeHandle
hObject
,
out
EyemBarCode
*
tpResults
,
out
int
ipNum
,
out
EyemImage
tpDstImg
);
//加载模型配置文件
[
DllImport
(
"eyemLib.dll"
,
CharSet
=
CharSet
.
None
,
CallingConvention
=
CallingConvention
.
Cdecl
)]
private
static
extern
int
eyemInitNNDataCodeModel
(
string
detectorConfigPath
,
string
detectorModelPath
,
string
superResolutionConfigPath
,
string
superResolutionModelPath
);
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
EyemImage
{
/// <summary>
/// 地址
/// </summary>
public
IntPtr
ucpImage
;
/// <summary>
/// 图像内存 x 方向大小
/// </summary>
public
int
iWidth
;
/// <summary>
/// 图像内存 y 方向大小
/// </summary>
public
int
iHeight
;
/// <summary>
/// iDepth:
/// uint8_t 取值范围[0, 255], iDepth = 0(常用,解码图像一般为此种格式)
/// int8_t 取值范围[-128, 127], iDepth = 1
/// uint16_t 取值范围[0, 56635], iDepth = 2(常用,点料机图像一般为此种格式)
/// int16_t 取值范围[-32768, 32767], iDepth = 3
/// int32_t 取值范围[-2147483648, 2147483647], iDepth = 4
/// float_t 取值范围[-3.4028235E38, 3.4028235E38], iDepth = 5
/// double_t 取值范围[-1.7E-308, 1.7E+308], iDepth = 6
/// </summary>
public
int
iDepth
;
/// <summary>
/// 彩色图 iChannels=3或4(比RGB多了Alpha通道),一般为3
/// 灰度图 iChannels = 1
/// </summary>
public
int
iChannels
;
// 图像通道数
}
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
EyemRect
{
public
int
iXs
;
// 起始点(左上角) x 坐标
public
int
iYs
;
// 起始点(左上角) y 坐标
public
int
iWidth
;
// x 方向大小(宽度)
public
int
iHeight
;
// y 方向大小(高度)
}
[
StructLayout
(
LayoutKind
.
Sequential
)]
public
struct
EyemBarCode
{
public
double
dAngle
;
// 角度
public
int
iCenterX
;
// y坐标
public
int
iCenterY
;
// y坐标
public
IntPtr
hType
;
// 码类型
public
IntPtr
hText
;
// 码内容
}
}
}
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论