Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
刘韬
/
1069_MIMO_PlUS
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 36760465
由
张东亮
编写于
2023-11-29 11:53:14 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
检测料盘图片采集异常问题
1 个父辈
51065abc
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
144 行增加
和
133 行删除
DeviceLibrary/DeviceLibrary/CodeManager.cs
DeviceLibrary/DeviceLibrary/CodeManager.cs
查看文件 @
3676046
...
@@ -22,8 +22,8 @@ namespace DeviceLibrary
...
@@ -22,8 +22,8 @@ namespace DeviceLibrary
{
{
public
class
CodeManager
public
class
CodeManager
{
{
public
static
string
CodeType
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
CodeType
,
"QR Code"
);
public
static
string
CodeType
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
CodeType
,
"QR Code"
);
private
static
int
QRCodeCount
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
QRCodeCount
,
3
);
private
static
int
QRCodeCount
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
QRCodeCount
,
3
);
private
static
int
CodeTimeOut
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
CodeTimeOut
,
1000
);
private
static
int
CodeTimeOut
=
ConfigHelper
.
Config
.
Get
(
Setting_Init
.
CodeTimeOut
,
1000
);
public
static
List
<
string
>
cameraNameList
=
new
List
<
string
>();
public
static
List
<
string
>
cameraNameList
=
new
List
<
string
>();
public
static
List
<
string
>
codeTypeList
=
new
List
<
string
>();
public
static
List
<
string
>
codeTypeList
=
new
List
<
string
>();
...
@@ -67,7 +67,7 @@ namespace DeviceLibrary
...
@@ -67,7 +67,7 @@ namespace DeviceLibrary
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
LogUtil
.
error
(
"解析摄像机配置出错:"
+
ex
.
StackTrace
);
LogUtil
.
error
(
"解析摄像机配置出错:"
+
ex
.
StackTrace
);
}
}
}
}
...
@@ -118,7 +118,7 @@ namespace DeviceLibrary
...
@@ -118,7 +118,7 @@ namespace DeviceLibrary
Camera
.
_cam
.
CloseAll
();
Camera
.
_cam
.
CloseAll
();
}
}
[
HandleProcessCorruptedStateExceptions
]
[
HandleProcessCorruptedStateExceptions
]
public
static
List
<
CodeInfo
>
CameraScan
(
List
<
string
>
cameraNameList
)
public
static
List
<
CodeInfo
>
CameraScan
(
List
<
string
>
cameraNameList
)
{
{
...
@@ -140,13 +140,13 @@ namespace DeviceLibrary
...
@@ -140,13 +140,13 @@ namespace DeviceLibrary
cameraTask
[
ii
]
=
Task
.
Run
(
new
Action
(()
=>
cameraTask
[
ii
]
=
Task
.
Run
(
new
Action
(()
=>
{
{
int
retrytime
=
0
;
int
retrytime
=
0
;
retry
:
retry
:
LogUtil
.
info
(
$
"【"
+
cameraName
+
"】开始取图片"
);
LogUtil
.
info
(
$
"【"
+
cameraName
+
"】开始取图片"
);
if
(
cameraName
.
Trim
().
Equals
(
""
))
if
(
cameraName
.
Trim
().
Equals
(
""
))
{
{
return
;
return
;
}
}
DateTime
startTime
=
DateTime
.
Now
;
DateTime
startTime
=
DateTime
.
Now
;
HalconDotNet
.
HObject
ho_Image
=
null
;
HalconDotNet
.
HObject
ho_Image
=
null
;
Bitmap
bmp
=
null
;
Bitmap
bmp
=
null
;
try
try
...
@@ -160,7 +160,7 @@ namespace DeviceLibrary
...
@@ -160,7 +160,7 @@ namespace DeviceLibrary
retrytime
++;
retrytime
++;
LoadCamera
(
true
);
LoadCamera
(
true
);
LogUtil
.
info
(
$
"bitmap为空重试第{retrytime}次"
);
LogUtil
.
info
(
$
"bitmap为空重试第{retrytime}次"
);
Task
.
Delay
(
1500
).
Wait
();
Task
.
Delay
(
1500
).
Wait
();
goto
retry
;
goto
retry
;
}
}
//HalconDotNet.HOperatorSet.RotateImage()
//HalconDotNet.HOperatorSet.RotateImage()
...
@@ -170,7 +170,7 @@ namespace DeviceLibrary
...
@@ -170,7 +170,7 @@ namespace DeviceLibrary
LoadCamera
(
true
);
LoadCamera
(
true
);
return
;
return
;
}
}
if
(
idx
==
0
)
if
(
idx
==
0
)
camera_event
?.
Invoke
(
null
,
bmp
);
camera_event
?.
Invoke
(
null
,
bmp
);
LogUtil
.
info
(
" 【"
+
cameraName
+
"】取图片完成,开始扫码"
);
LogUtil
.
info
(
" 【"
+
cameraName
+
"】取图片完成,开始扫码"
);
string
r
=
""
;
string
r
=
""
;
...
@@ -183,7 +183,7 @@ namespace DeviceLibrary
...
@@ -183,7 +183,7 @@ namespace DeviceLibrary
List
<
CodeInfo
>
cc
=
new
List
<
CodeInfo
>();
List
<
CodeInfo
>
cc
=
new
List
<
CodeInfo
>();
cc
=
RemoteDecodeHelper
.
DecodeRequest
(
bmp
,
remoteDecodeParam
);
cc
=
RemoteDecodeHelper
.
DecodeRequest
(
bmp
,
remoteDecodeParam
);
if
(
cc
!=
null
&&
cc
.
Count
>
0
)
if
(
cc
!=
null
&&
cc
.
Count
>
0
)
{
{
lock
(
codeList
)
lock
(
codeList
)
{
{
...
@@ -237,97 +237,129 @@ namespace DeviceLibrary
...
@@ -237,97 +237,129 @@ namespace DeviceLibrary
}
}
catch
(
AccessViolationException
e
)
catch
(
AccessViolationException
e
)
{
{
LogUtil
.
error
(
" 扫码出现AccessViolationException异常:"
+
e
.
ToString
());
LogUtil
.
error
(
" 扫码出现AccessViolationException异常:"
+
e
.
ToString
());
//throw new Exception("扫码出现AccessViolationException异常");
//throw new Exception("扫码出现AccessViolationException异常");
// GC.Collect();
// GC.Collect();
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
LogUtil
.
error
(
" 扫码出错:"
+
ex
.
ToString
());
LogUtil
.
error
(
" 扫码出错:"
+
ex
.
ToString
());
//throw new Exception("扫码出错");
//throw new Exception("扫码出错");
}
}
return
codeList
;
return
codeList
;
}
}
static
bool
lastHasReel
=
false
;
static
bool
lastHasReel
=
false
;
public
static
bool
?
TestHasRight
=
null
;
public
static
bool
?
TestHasRight
=
null
;
static
object
testLocObj
=
new
object
();
[
HandleProcessCorruptedStateExceptions
]
[
HandleProcessCorruptedStateExceptions
]
public
static
bool
?
TestHasReel
(
string
cameraName
,
out
string
srcimg
,
out
string
prcimg
)
public
static
bool
?
TestHasReel
(
string
cameraName
,
out
string
srcimg
,
out
string
prcimg
)
{
{
srcimg
=
""
;
srcimg
=
""
;
prcimg
=
""
;
prcimg
=
""
;
int
retrytime
=
0
;
int
retrytime
=
0
;
retry
:
if
(
Monitor
.
TryEnter
(
testLocObj
))
string
logtxt
=
$
"【"
+
cameraName
+
"】开始取图片测试是否有料盘"
+
"\r\n"
;
DateTime
startTime
=
DateTime
.
Now
;
Bitmap
bmp
=
null
;
try
{
{
//bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp");
retry
:
bmp
=
Camera
.
_cam
.
GrabOneImage
(
cameraName
,
PixelType
.
RGB8
);
string
logtxt
=
$
"【"
+
cameraName
+
"】开始取图片测试是否有料盘"
+
"\r\n"
;
if
(
bmp
==
null
)
DateTime
startTime
=
DateTime
.
Now
;
Bitmap
bmp
=
null
;
try
{
{
if
(
retrytime
>
2
)
//bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp");
return
null
;
bmp
=
Camera
.
_cam
.
GrabOneImage
(
cameraName
,
PixelType
.
RGB8
);
retrytime
++;
if
(
bmp
==
null
)
Camera
.
_cam
.
Close
(
cameraName
);
{
//LoadCamera(true);
if
(
retrytime
>
2
)
return
null
;
retrytime
++;
Camera
.
_cam
.
Close
(
cameraName
);
//LoadCamera(true);
LogUtil
.
info
(
$
"bitmap为空重试第{retrytime}次"
);
Thread
.
Sleep
(
2000
);
goto
retry
;
}
logtxt
+=
$
"【"
+
cameraName
+
"】获取到图像"
+
"\r\n"
;
int
totalcover
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_totalcover"
,
69577
);
int
hl
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_HL"
,
40
);
int
hh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_HH"
,
70
);
int
ll
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_LL"
,
15
);
int
lh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_LH"
,
100
);
int
sl
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_SL"
,
20
);
int
sh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_SH"
,
100
);
LogUtil
.
info
(
$
"bitmap为空重试第{retrytime}次"
);
double
threshold
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_threshold"
,
0.6
);
Thread
.
Sleep
(
500
);
goto
retry
;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
}
srcimg
=
SaveImageToFile
(
"test"
,
cameraName
,
bmp
);
logtxt
+=
$
"【"
+
cameraName
+
"】获取到图像"
+
"\r\n"
;
int
totalcover
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_totalcover"
,
69577
);
int
hl
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_HL"
,
40
);
int
hh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_HH"
,
70
);
int
ll
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_LL"
,
15
);
int
lh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_LH"
,
100
);
int
sl
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_SL"
,
20
);
int
sh
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_SH"
,
100
);
double
threshold
=
ConfigHelper
.
Config
.
Get
(
"CamTestReel_threshold"
,
0.6
);
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
srcimg
=
SaveImageToFile
(
"test"
,
cameraName
,
bmp
);
var
b
=
new
Bitmap
(
bmp
.
Width
/
2
,
bmp
.
Height
/
2
,
bmp
.
PixelFormat
);
using
(
var
g
=
Graphics
.
FromImage
(
b
))
{
g
.
InterpolationMode
=
InterpolationMode
.
Low
;
g
.
DrawImage
(
bmp
,
0
,
0
,
b
.
Width
,
b
.
Height
);
}
ImageLockMode
imageLockMode
=
ImageLockMode
.
ReadOnly
;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
imageLockMode
=
ImageLockMode
.
ReadWrite
;
var
bd
=
b
.
LockBits
(
new
Rectangle
(
0
,
0
,
b
.
Width
,
b
.
Height
),
imageLockMode
,
b
.
PixelFormat
);
var
rois
=
TestRoi
.
GetRois
();
if
(
rois
==
null
||
rois
.
Count
==
0
)
{
rois
=
new
List
<
TestRoi
>()
{
new
TestRoi
()
};
rois
[
0
].
CoverCount
=
totalcover
;
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
0
,
0
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
bmp
.
Width
,
0
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
bmp
.
Width
,
bmp
.
Height
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
0
,
bmp
.
Height
));
}
bool
hasReel
=
false
;
var
b
=
new
Bitmap
(
bmp
.
Width
/
2
,
bmp
.
Height
/
2
,
bmp
.
PixelFormat
);
for
(
int
i
=
0
;
i
<
rois
.
Count
;
i
++)
{
using
(
var
g
=
Graphics
.
FromImage
(
b
))
double
maskcout
=
0
;
var
roi
=
rois
[
i
];
var
rp
=
roi
.
RoiPath
.
Select
(
r
=>
new
Point
(
r
.
X
/
2
,
r
.
Y
/
2
)).
ToList
();
var
fp
=
GetFilledPoints
(
rp
);
var
nfp
=
fp
.
Where
(
f
=>
IsPointInPath
(
f
.
X
,
f
.
Y
,
rp
)).
ToList
();
foreach
(
var
xy
in
nfp
)
{
{
int
x
=
(
xy
.
Y
*
bd
.
Width
+
xy
.
X
)
*
3
;
g
.
InterpolationMode
=
InterpolationMode
.
Low
;
//Marshal.WriteByte(bd.Scan0, x, 0);
g
.
DrawImage
(
bmp
,
0
,
0
,
b
.
Width
,
b
.
Height
);
//Marshal.WriteByte(bd.Scan0, x + 1, 0);
}
//Marshal.WriteByte(bd.Scan0, x + 2, 255);
ImageLockMode
imageLockMode
=
ImageLockMode
.
ReadOnly
;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
imageLockMode
=
ImageLockMode
.
ReadWrite
;
var
bd
=
b
.
LockBits
(
new
Rectangle
(
0
,
0
,
b
.
Width
,
b
.
Height
),
imageLockMode
,
b
.
PixelFormat
);
var
rois
=
TestRoi
.
GetRois
();
if
(
rois
==
null
||
rois
.
Count
==
0
)
{
rois
=
new
List
<
TestRoi
>()
{
new
TestRoi
()
};
rois
[
0
].
CoverCount
=
totalcover
;
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
0
,
0
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
bmp
.
Width
,
0
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
bmp
.
Width
,
bmp
.
Height
));
rois
[
0
].
RoiPath
.
Add
(
new
Point
(
0
,
bmp
.
Height
));
}
bool
hasReel
=
false
;
for
(
int
i
=
0
;
i
<
rois
.
Count
;
i
++)
{
double
maskcout
=
0
;
var
roi
=
rois
[
i
];
var
rp
=
roi
.
RoiPath
.
Select
(
r
=>
new
Point
(
r
.
X
/
2
,
r
.
Y
/
2
)).
ToList
();
var
fp
=
GetFilledPoints
(
rp
);
var
nfp
=
fp
.
Where
(
f
=>
IsPointInPath
(
f
.
X
,
f
.
Y
,
rp
)).
ToList
();
foreach
(
var
xy
in
nfp
)
{
int
x
=
(
xy
.
Y
*
bd
.
Width
+
xy
.
X
)
*
3
;
//Marshal.WriteByte(bd.Scan0, x, 0);
//Marshal.WriteByte(bd.Scan0, x + 1, 0);
//Marshal.WriteByte(bd.Scan0, x + 2, 255);
var
cr
=
(
int
)
Marshal
.
ReadByte
(
bd
.
Scan0
,
x
+
2
);
var
cg
=
(
int
)
Marshal
.
ReadByte
(
bd
.
Scan0
,
x
+
1
);
var
cb
=
(
int
)
Marshal
.
ReadByte
(
bd
.
Scan0
,
x
);
var
h
=
ColorHelper
.
RgbToHsv
(
new
ColorRGB
(
cr
,
cg
,
cb
));
if
(
h
.
H
>=
hl
&&
h
.
H
<=
hh
&&
h
.
V
>=
ll
&&
h
.
V
<=
lh
&&
h
.
S
>=
sl
&&
h
.
S
<=
sh
)
{
maskcout
++;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
{
Marshal
.
WriteByte
(
bd
.
Scan0
,
x
,
0
);
Marshal
.
WriteByte
(
bd
.
Scan0
,
x
+
1
,
0
);
Marshal
.
WriteByte
(
bd
.
Scan0
,
x
+
2
,
255
);
}
}
}
double
calc
=
maskcout
/
(
double
)
roi
.
CoverCount
;
logtxt
+=
$
" 检测到Roi[{i}]覆盖面积计数:maskcout:{maskcout}/{roi.CoverCount}={calc}<{threshold},result:{calc <= threshold}\r\n"
;
if
(
calc
<=
threshold
)
{
hasReel
=
true
;
break
;
}
}
/*
for (int x = 0; x < b.Width * b.Height * 3; x = x + 3)
{
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
var cb = (int)Marshal.ReadByte(bd.Scan0, x);
var cb = (int)Marshal.ReadByte(bd.Scan0, x);
...
@@ -343,66 +375,44 @@ namespace DeviceLibrary
...
@@ -343,66 +375,44 @@ namespace DeviceLibrary
Marshal.WriteByte(bd.Scan0, x + 2, 255);
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
}
}
}
}
}
*/
double
calc
=
maskcout
/
(
double
)
roi
.
CoverCount
;
b
.
UnlockBits
(
bd
)
;
logtxt
+=
$
" 检测到Roi[{i}]覆盖面积计数:maskcout:{maskcout}/{roi.CoverCount}={calc}<{threshold},result:{calc <= threshold}\r\n"
;
if
(
calc
<=
threshold
)
if
(
hasReel
!=
lastHasReel
)
{
{
hasReel
=
true
;
lastHasReel
=
hasReel
;
break
;
}
}
else
if
(!
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
logtxt
=
""
;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
)
||
(
TestHasRight
.
HasValue
&&
TestHasRight
.
Value
!=
hasReel
))
prcimg
=
SaveImageToFile
(
"test2"
,
cameraName
,
b
);
b
.
Dispose
();
TestHasRight
=
null
;
return
hasReel
;
}
}
catch
(
AccessViolationException
e
)
/*
for (int x = 0; x < b.Width * b.Height * 3; x = x + 3)
{
{
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
LogUtil
.
error
(
" 扫码出现AccessViolationException异常,关闭相机【"
+
cameraName
+
"】:"
+
e
.
ToString
());
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
Camera
.
_cam
.
Close
(
cameraName
);
var cb = (int)Marshal.ReadByte(bd.Scan0, x);
return
null
;
}
var h = ColorHelper.RgbToHsv(new ColorRGB(cr, cg, cb));
catch
(
Exception
ex
)
if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh)
{
{
LogUtil
.
error
(
" 扫码出错:"
+
ex
.
ToString
());
maskcout++;
return
null
;
if (ConfigHelper.Config.Get("CamTestReel_debug", false))
}
{
finally
Marshal.WriteByte(bd.Scan0, x, 0);
{
Marshal.WriteByte(bd.Scan0, x + 1, 0);
if
(
bmp
!=
null
)
Marshal.WriteByte(bd.Scan0, x + 2, 255);
bmp
.
Dispose
();
}
if
(!
string
.
IsNullOrEmpty
(
logtxt
))
}
LogUtil
.
error
(
logtxt
);
}*/
Monitor
.
Exit
(
testLocObj
);
b
.
UnlockBits
(
bd
);
}
if
(
hasReel
!=
lastHasReel
)
{
lastHasReel
=
hasReel
;
}
else
if
(!
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
))
logtxt
=
""
;
if
(
ConfigHelper
.
Config
.
Get
(
"CamTestReel_debug"
,
false
)
||
(
TestHasRight
.
HasValue
&&
TestHasRight
.
Value
!=
hasReel
))
prcimg
=
SaveImageToFile
(
"test2"
,
cameraName
,
b
);
b
.
Dispose
();
TestHasRight
=
null
;
return
hasReel
;
}
catch
(
AccessViolationException
e
)
{
LogUtil
.
error
(
" 扫码出现AccessViolationException异常,关闭相机【"
+
cameraName
+
"】:"
+
e
.
ToString
());
Camera
.
_cam
.
Close
(
cameraName
);
return
null
;
}
}
catch
(
Exception
ex
)
else
{
LogUtil
.
error
(
" 扫码出错:"
+
ex
.
ToString
());
return
null
;
return
null
;
}
finally
{
if
(
bmp
!=
null
)
bmp
.
Dispose
();
if
(!
string
.
IsNullOrEmpty
(
logtxt
))
LogUtil
.
error
(
logtxt
);
}
}
}
/// <summary>
/// <summary>
///
///
...
@@ -412,7 +422,8 @@ namespace DeviceLibrary
...
@@ -412,7 +422,8 @@ namespace DeviceLibrary
/// <param name="pX"></param>
/// <param name="pX"></param>
/// <param name="pY"></param>
/// <param name="pY"></param>
/// <returns>x,y</returns>
/// <returns>x,y</returns>
static
(
int
,
int
)
PointRoate270
(
int
imgWidth
,
int
imgHeight
,
int
pX
,
int
pY
)
{
static
(
int
,
int
)
PointRoate270
(
int
imgWidth
,
int
imgHeight
,
int
pX
,
int
pY
)
{
int
x
=
pY
;
int
x
=
pY
;
int
y
=
imgWidth
-
pX
;
int
y
=
imgWidth
-
pX
;
return
(
x
,
y
);
return
(
x
,
y
);
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论