Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
GeneralClassLibrary
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 21c3fca6
由
LN
编写于
2019-11-19 13:49:49 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
修改为可同时打开多个相机
1 个父辈
4abd82a9
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
860 行增加
和
0 行删除
CodeLibraryProject/CodeLibrary/CodeLibrary.csproj
CodeLibraryProject/CodeLibrary/basler/BaslerCManager.cs
CodeLibraryProject/CodeLibrary/basler/BaslerCameraBean.cs
CodeLibraryProject/CodeLibrary/hik/HIKCManager.cs
CodeLibraryProject/CodeLibrary/hik/HIKCameraBean.cs
CodeLibraryProject/编译好的DLL/CodeLibrary.dll
CodeLibraryProject/CodeLibrary/CodeLibrary.csproj
查看文件 @
21c3fca
...
@@ -62,6 +62,8 @@
...
@@ -62,6 +62,8 @@
</Reference>
</Reference>
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Compile Include="basler\BaslerCManager.cs" />
<Compile Include="basler\BaslerCameraBean.cs" />
<Compile Include="FrmBase.cs">
<Compile Include="FrmBase.cs">
<SubType>Form</SubType>
<SubType>Form</SubType>
</Compile>
</Compile>
...
@@ -85,6 +87,8 @@
...
@@ -85,6 +87,8 @@
<Compile Include="HDCodeHelper.cs" />
<Compile Include="HDCodeHelper.cs" />
<Compile Include="HDCodeLearnHelper.cs" />
<Compile Include="HDCodeLearnHelper.cs" />
<Compile Include="HDLogUtil.cs" />
<Compile Include="HDLogUtil.cs" />
<Compile Include="hik\HIKCameraBean.cs" />
<Compile Include="hik\HIKCManager.cs" />
<Compile Include="ImageHelper.cs" />
<Compile Include="ImageHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CodeResourceControl.cs" />
<Compile Include="CodeResourceControl.cs" />
...
@@ -107,5 +111,6 @@
...
@@ -107,5 +111,6 @@
</Content>
</Content>
<Content Include="记录.txt" />
<Content Include="记录.txt" />
</ItemGroup>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
\ No newline at end of file
\ No newline at end of file
CodeLibraryProject/CodeLibrary/basler/BaslerCManager.cs
0 → 100644
查看文件 @
21c3fca
using
Basler.Pylon
;
using
System
;
using
System.Collections.Generic
;
using
System.Drawing
;
using
System.Drawing.Imaging
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
CodeLibrary
{
public
class
BaslerCManager
{
/// <summary>
/// 当前相机
/// </summary>
// private Camera cameraCur = null;
private
static
Dictionary
<
string
,
BaslerCameraBean
>
cameraMap
=
new
Dictionary
<
string
,
BaslerCameraBean
>();
/// <summary>
/// 所有相机列表
/// </summary>
private
static
List
<
ICameraInfo
>
cameraAll
;
/// <summary>
/// 所有相机的名称
/// </summary>
private
static
List
<
string
>
cameraName
;
/// <summary>
/// 获取连续图像
/// </summary>
public
delegate
void
GrabImageEvent
();
///// <summary>
///// 获取连续图像事件,需要跨线程操作
///// </summary>
//public event GrabImageEvent GrabImage;
private
BaslerCManager
()
{
Load
();
}
/// <summary>
/// 错误信息
/// </summary>
public
static
string
ErrInfo
{
set
;
get
;
}
/// <summary>
/// 相机总数
/// </summary>
public
int
Count
{
get
{
return
cameraAll
==
null
?
0
:
cameraAll
.
Count
;
}
}
/// <summary>
/// 相机名称,ModelName,SerialNumber
/// </summary>
public
static
string
[]
CameraName
{
get
{
if
(
cameraName
==
null
)
{
cameraName
=
new
List
<
string
>();
}
return
cameraName
.
ToArray
();
}
}
public
static
BaslerCameraBean
GetCamera
(
string
cName
)
{
if
(
cameraMap
.
ContainsKey
(
cName
))
{
return
cameraMap
[
cName
];
}
return
null
;
}
public
static
void
AddCamera
(
string
name
,
BaslerCameraBean
bean
)
{
if
(
cameraMap
.
ContainsKey
(
name
))
{
cameraMap
.
Remove
(
name
);
}
cameraMap
.
Add
(
name
,
bean
);
}
/// <summary>
/// 当前相机是否打开
/// </summary>
public
static
bool
IsOpen
(
string
name
)
{
BaslerCameraBean
bean
=
GetCamera
(
name
);
if
(
bean
==
null
)
{
return
false
;
}
else
{
return
bean
.
cameraCur
.
IsOpen
;
}
}
/// <summary>
/// 加载相机
/// </summary>
public
static
void
Load
()
{
try
{
cameraAll
=
CameraFinder
.
Enumerate
();
cameraName
=
new
List
<
string
>();
foreach
(
ICameraInfo
info
in
cameraAll
)
cameraName
.
Add
(
info
[
CameraInfoKey
.
ModelName
].
ToString
()
+
" ("
+
info
[
CameraInfoKey
.
SerialNumber
].
ToString
()
+
")"
);
}
catch
(
Exception
ex
)
{
HDLogUtil
.
error
(
"Basler Load Error:"
+
ex
.
StackTrace
);
}
}
/// <summary>
/// 打开指定相机
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public
static
bool
Open
(
string
name
,
GrabImageEvent
grab
=
null
)
{
int
n
=
cameraName
.
FindIndex
(
s
=>
s
==
name
);
if
(
n
==
-
1
)
return
false
;
else
{
if
(
cameraMap
.
ContainsKey
(
name
))
{
if
(
cameraMap
[
name
].
cameraCur
!=
null
)
{
cameraMap
[
name
].
cameraCur
.
Close
();
}
cameraMap
.
Remove
(
name
);
}
Camera
cameraCur
=
null
;
if
(
n
<
0
||
n
>=
cameraAll
.
Count
)
{
return
false
;
}
try
{
cameraCur
=
new
Camera
(
cameraAll
[
n
]);
BaslerCameraBean
bean
=
new
BaslerCameraBean
(
cameraCur
);
bean
.
Open
(
);
AddCamera
(
name
,
bean
);
}
catch
(
Exception
ex
)
{
ErrInfo
=
ex
.
Message
;
return
false
;
}
return
true
;
}
}
/// <summary>
/// 关闭当前相机
/// </summary>
public
static
void
Close
(
string
name
)
{
BaslerCameraBean
bean
=
GetCamera
(
name
);
if
(
bean
!=
null
&&
bean
.
cameraCur
!=
null
)
{
bean
.
Close
();
}
}
public
static
void
CloseAll
()
{
foreach
(
string
key
in
cameraMap
.
Keys
)
{
Close
(
key
);
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public
static
void
Stop
(
string
name
)
{
BaslerCameraBean
bean
=
GetCamera
(
name
);
if
(
bean
!=
null
)
{
bean
.
Stop
();
}
}
/// <summary>
/// 抓取一张图像
/// </summary>
public
static
Bitmap
GrabOne
(
string
name
)
{
BaslerCameraBean
bean
=
GetCamera
(
name
);
if
(
bean
!=
null
&&
bean
.
cameraCur
!=
null
)
{
return
bean
.
GrabOne
();
}
return
null
;
}
}
}
CodeLibraryProject/CodeLibrary/basler/BaslerCameraBean.cs
0 → 100644
查看文件 @
21c3fca
using
Basler.Pylon
;
using
System
;
using
System.Collections.Generic
;
using
System.Drawing
;
using
System.Drawing.Imaging
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
static
CodeLibrary
.
BaslerCamera
;
namespace
CodeLibrary
{
public
class
BaslerCameraBean
{
public
Camera
cameraCur
=
null
;
/// <summary>
/// 相机图像宽度
/// </summary>
public
int
Width
{
set
;
get
;
}
/// <summary>
/// 相机图像高度
/// </summary>
public
int
Height
{
set
;
get
;
}
/// <summary>
/// 相机32位缓存
/// </summary>
public
byte
[]
Buffer
{
get
;
private
set
;
}
/// <summary>
/// 相机32位图像
/// </summary>
public
Bitmap
Image
{
get
;
private
set
;
}
/// <summary>
/// 获取连续图像事件,需要跨线程操作
/// </summary>
public
event
GrabImageEvent
GrabImage
;
/// <summary>
/// 错误信息
/// </summary>
public
string
ErrInfo
{
set
;
get
;
}
public
BaslerCameraBean
(
Camera
c
)
{
this
.
cameraCur
=
c
;
}
public
BaslerCameraBean
(
Camera
c
,
int
width
,
int
height
)
{
this
.
cameraCur
=
c
;
this
.
Width
=
width
;
this
.
Height
=
height
;
}
public
void
Open
()
{
cameraCur
.
StreamGrabber
.
ImageGrabbed
+=
OnImageGrabbed
;
//cameraCur.StreamGrabber.GrabStopped += OnGrabStopped;
cameraCur
.
Open
();
int
Width
=
Convert
.
ToInt32
(
cameraCur
.
Parameters
[
PLCamera
.
Width
].
GetValue
());
int
Height
=
Convert
.
ToInt32
(
cameraCur
.
Parameters
[
PLCamera
.
Height
].
GetValue
());
cameraCur
.
Parameters
[
PLCamera
.
UserSetSelector
].
SetValue
(
PLCamera
.
UserSetSelector
.
UserSet1
);
//加载用户设置1
bool
bln
=
cameraCur
.
Parameters
[
PLCamera
.
UserSetLoad
].
TryExecute
();
//执行设置
}
/// <summary>
/// 关闭当前相机
/// </summary>
public
void
Close
()
{
if
(
cameraCur
!=
null
)
{
cameraCur
.
Close
();
cameraCur
.
Dispose
();
cameraCur
=
null
;
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public
void
Stop
()
{
if
(
cameraCur
!=
null
)
cameraCur
.
StreamGrabber
.
Stop
();
}
/// <summary>
/// 抓取一张图像
/// </summary>
public
Bitmap
GrabOne
()
{
cameraCur
.
Parameters
[
PLCamera
.
AcquisitionMode
].
SetValue
(
PLCamera
.
AcquisitionMode
.
SingleFrame
);
//cameraCur.StreamGrabber.Start();
//IGrabResult grabResult = cameraCur.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
IGrabResult
grabResult
=
cameraCur
.
StreamGrabber
.
GrabOne
(
5000
);
if
(!
grabResult
.
IsValid
)
return
null
;
Image
=
new
Bitmap
(
grabResult
.
Width
,
grabResult
.
Height
,
PixelFormat
.
Format32bppRgb
);
BitmapData
bmpData
=
Image
.
LockBits
(
new
Rectangle
(
0
,
0
,
grabResult
.
Width
,
grabResult
.
Height
),
ImageLockMode
.
ReadWrite
,
PixelFormat
.
Format32bppRgb
);
IntPtr
ptrBmp
=
bmpData
.
Scan0
;
int
picSize
=
bmpData
.
Stride
*
grabResult
.
Height
;
PixelDataConverter
conv
=
new
PixelDataConverter
();
conv
.
OutputPixelFormat
=
PixelType
.
BGRA8packed
;
conv
.
Convert
(
ptrBmp
,
picSize
,
grabResult
);
Buffer
=
new
byte
[
picSize
];
System
.
Runtime
.
InteropServices
.
Marshal
.
Copy
(
ptrBmp
,
Buffer
,
0
,
picSize
);
Image
.
UnlockBits
(
bmpData
);
return
Image
;
//cameraCur.StreamGrabber.Stop();
}
/// <summary>
/// 抓取连续图像,触发GrabImage事件
/// </summary>
public
void
GrabContinuous
()
{
cameraCur
.
Parameters
[
PLCamera
.
AcquisitionMode
].
SetValue
(
PLCamera
.
AcquisitionMode
.
Continuous
);
cameraCur
.
StreamGrabber
.
Start
(
GrabStrategy
.
OneByOne
,
GrabLoop
.
ProvidedByStreamGrabber
);
}
private
void
OnImageGrabbed
(
object
sender
,
ImageGrabbedEventArgs
e
)
{
try
{
IGrabResult
grabResult
=
e
.
GrabResult
;
if
(!
grabResult
.
IsValid
)
return
;
Image
=
new
Bitmap
(
grabResult
.
Width
,
grabResult
.
Height
,
PixelFormat
.
Format32bppRgb
);
BitmapData
bmpData
=
Image
.
LockBits
(
new
Rectangle
(
0
,
0
,
grabResult
.
Width
,
grabResult
.
Height
),
ImageLockMode
.
ReadWrite
,
PixelFormat
.
Format32bppRgb
);
IntPtr
ptrBmp
=
bmpData
.
Scan0
;
int
picSize
=
bmpData
.
Stride
*
grabResult
.
Height
;
PixelDataConverter
conv
=
new
PixelDataConverter
();
conv
.
OutputPixelFormat
=
PixelType
.
BGRA8packed
;
conv
.
Convert
(
ptrBmp
,
picSize
,
grabResult
);
Buffer
=
new
byte
[
picSize
];
System
.
Runtime
.
InteropServices
.
Marshal
.
Copy
(
ptrBmp
,
Buffer
,
0
,
picSize
);
Image
.
UnlockBits
(
bmpData
);
GrabImage
?.
Invoke
();
}
catch
(
Exception
ex
)
{
ErrInfo
=
ex
.
Message
;
}
finally
{
e
.
DisposeGrabResultIfClone
();
}
}
}
}
CodeLibraryProject/CodeLibrary/hik/HIKCManager.cs
0 → 100644
查看文件 @
21c3fca
using
MvCamCtrl.NET
;
using
System
;
using
System.Collections.Generic
;
using
System.Drawing
;
using
System.Drawing.Imaging
;
using
System.Linq
;
using
System.Runtime.InteropServices
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
CodeLibrary
{
public
class
HIKCManager
{
/// <summary>
/// 当前相机
/// </summary>
// private Camera cameraCur = null;
private
static
Dictionary
<
string
,
HIKCameraBean
>
cameraMap
=
new
Dictionary
<
string
,
HIKCameraBean
>();
/// <summary>
/// 当前相机
/// </summary>
// private MyCamera cameraCurr;
/// <summary>
/// 所有相机列表
/// </summary>
private
static
MyCamera
.
MV_CC_DEVICE_INFO_LIST
cameraAll
;
/// <summary>
/// 所有相机的名称
/// </summary>
private
static
List
<
string
>
cameraName
=
new
List
<
string
>();
/// <summary>
/// 海康相机
/// </summary>
public
HIKCManager
()
{
cameraAll
=
new
MyCamera
.
MV_CC_DEVICE_INFO_LIST
();
Load
();
}
public
static
HIKCameraBean
GetCamera
(
string
cName
)
{
if
(
cameraMap
.
ContainsKey
(
cName
))
{
return
cameraMap
[
cName
];
}
return
null
;
}
public
static
void
AddCamera
(
string
name
,
HIKCameraBean
bean
)
{
if
(
cameraMap
.
ContainsKey
(
name
))
{
cameraMap
.
Remove
(
name
);
}
cameraMap
.
Add
(
name
,
bean
);
}
/// <summary>
/// 错误信息
/// </summary>
public
static
string
ErrInfo
{
set
;
get
;
}
/// <summary>
/// 相机总数
/// </summary>
public
static
int
Count
{
get
{
return
(
int
)
cameraAll
.
nDeviceNum
;
}
}
/// <summary>
/// 相机名称,ModelName,SerialNumber
/// </summary>
public
static
string
[]
CameraName
{
get
{
if
(
cameraName
==
null
)
{
cameraName
=
new
List
<
string
>();
}
return
cameraName
.
ToArray
();
}
}
/// <summary>
/// 当前相机是否打开
/// </summary>
public
static
bool
IsOpen
(
string
cName
)
{
HIKCameraBean
bean
=
GetCamera
(
cName
);
if
(
bean
==
null
||
bean
.
cameraCur
==
null
)
{
return
false
;
}
else
{
return
true
;
}
}
/// <summary>
/// 加载相机
/// </summary>
public
static
void
Load
()
{
try
{
int
rtn
=
MyCamera
.
MV_CC_EnumDevices_NET
(
MyCamera
.
MV_GIGE_DEVICE
|
MyCamera
.
MV_USB_DEVICE
,
ref
cameraAll
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
cameraName
.
Clear
();
string
s
=
""
;
for
(
int
i
=
0
;
i
<
cameraAll
.
nDeviceNum
;
i
++)
{
MyCamera
.
MV_CC_DEVICE_INFO
device
=
(
MyCamera
.
MV_CC_DEVICE_INFO
)
Marshal
.
PtrToStructure
(
cameraAll
.
pDeviceInfo
[
i
],
typeof
(
MyCamera
.
MV_CC_DEVICE_INFO
));
if
(
device
.
nTLayerType
==
MyCamera
.
MV_GIGE_DEVICE
)
{
IntPtr
buffer
=
Marshal
.
UnsafeAddrOfPinnedArrayElement
(
device
.
SpecialInfo
.
stGigEInfo
,
0
);
MyCamera
.
MV_GIGE_DEVICE_INFO
gigeInfo
=
(
MyCamera
.
MV_GIGE_DEVICE_INFO
)
Marshal
.
PtrToStructure
(
buffer
,
typeof
(
MyCamera
.
MV_GIGE_DEVICE_INFO
));
s
=
"GigE:"
+
gigeInfo
.
chModelName
+
" ("
+
gigeInfo
.
chSerialNumber
+
")"
;
}
else
if
(
device
.
nTLayerType
==
MyCamera
.
MV_USB_DEVICE
)
{
IntPtr
buffer
=
Marshal
.
UnsafeAddrOfPinnedArrayElement
(
device
.
SpecialInfo
.
stUsb3VInfo
,
0
);
MyCamera
.
MV_USB3_DEVICE_INFO
usbInfo
=
(
MyCamera
.
MV_USB3_DEVICE_INFO
)
Marshal
.
PtrToStructure
(
buffer
,
typeof
(
MyCamera
.
MV_USB3_DEVICE_INFO
));
s
=
"USB:"
+
usbInfo
.
chModelName
+
" ("
+
usbInfo
.
chSerialNumber
+
")"
;
}
cameraName
.
Add
(
s
);
}
}
catch
(
Exception
ex
)
{
HDLogUtil
.
error
(
"HIK Load Error:"
+
ex
.
StackTrace
);
}
}
/// <summary>
/// 打开指定相机
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public
static
bool
Open
(
string
name
)
{
int
n
=
cameraName
.
FindIndex
(
s
=>
s
==
name
);
if
(
n
==
-
1
)
return
false
;
else
{
if
(
cameraMap
.
ContainsKey
(
name
))
{
if
(
cameraMap
[
name
].
cameraCur
!=
null
)
{
cameraMap
[
name
].
Close
();
}
cameraMap
.
Remove
(
name
);
}
if
(
n
<
0
||
n
>=
cameraAll
.
pDeviceInfo
.
Length
)
{
return
false
;
}
try
{
MyCamera
.
MV_CC_DEVICE_INFO
device
=
(
MyCamera
.
MV_CC_DEVICE_INFO
)
Marshal
.
PtrToStructure
(
cameraAll
.
pDeviceInfo
[
n
],
typeof
(
MyCamera
.
MV_CC_DEVICE_INFO
));
MyCamera
cameraCur
=
new
MyCamera
();
HIKCameraBean
bean
=
new
HIKCameraBean
(
cameraCur
);
bean
.
Open
(
device
);
AddCamera
(
name
,
bean
);
}
catch
(
Exception
ex
)
{
ErrInfo
=
ex
.
Message
;
return
false
;
}
return
true
;
}
}
/// <summary>
/// 关闭当前相机
/// </summary>
public
static
void
Close
(
string
cName
)
{
HIKCameraBean
bean
=
GetCamera
(
cName
);
if
(
bean
!=
null
&&
bean
.
cameraCur
!=
null
)
{
bean
.
cameraCur
.
MV_CC_CloseDevice_NET
();
bean
.
cameraCur
.
MV_CC_DestroyDevice_NET
();
bean
.
cameraCur
=
null
;
}
}
public
static
void
CloseAll
()
{
foreach
(
string
key
in
cameraMap
.
Keys
)
{
Close
(
key
);
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public
static
void
Stop
(
string
cName
)
{
HIKCameraBean
bean
=
GetCamera
(
cName
);
if
(
bean
==
null
||
bean
.
cameraCur
!=
null
)
{
return
;
}
if
(
bean
.
cameraCur
==
null
)
return
;
int
rtn
=
bean
.
cameraCur
.
MV_CC_StopGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
}
/// <summary>
/// 抓取一张图像
/// </summary>
public
static
Bitmap
GrabOne
(
string
cName
)
{
HIKCameraBean
bean
=
GetCamera
(
cName
);
if
(
bean
==
null
||
bean
.
cameraCur
!=
null
)
{
return
null
;
}
return
bean
.
GrabOne
();
}
/// <summary>
/// 抓取连续图像,触发GrabImage事件
/// </summary>
/// <param name="hWnd"></param>
public
static
void
GrabContinuous
(
string
cName
,
IntPtr
hWnd
)
{
HIKCameraBean
bean
=
GetCamera
(
cName
);
if
(
bean
==
null
||
bean
.
cameraCur
!=
null
)
{
return
;
}
int
rtn
=
bean
.
cameraCur
.
MV_CC_StartGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
rtn
=
bean
.
cameraCur
.
MV_CC_Display_NET
(
hWnd
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
}
}
}
CodeLibraryProject/CodeLibrary/hik/HIKCameraBean.cs
0 → 100644
查看文件 @
21c3fca
using
MvCamCtrl.NET
;
using
System
;
using
System.Collections.Generic
;
using
System.Drawing
;
using
System.Drawing.Imaging
;
using
System.Linq
;
using
System.Runtime.InteropServices
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
CodeLibrary
{
public
class
HIKCameraBean
{
/// <summary>
/// 当前相机
/// </summary>
public
MyCamera
cameraCur
;
/// <summary>
/// 海康相机
/// </summary>
internal
HIKCameraBean
(
MyCamera
cameraCur
)
{
this
.
cameraCur
=
cameraCur
;
}
/// <summary>
/// 错误信息
/// </summary>
public
string
ErrInfo
{
set
;
get
;
}
/// <summary>
/// 当前相机是否打开
/// </summary>
public
bool
IsOpen
{
get
{
if
(
cameraCur
==
null
)
return
false
;
else
return
true
;
}
}
/// <summary>
/// 相机图像宽度
/// </summary>
public
int
Width
{
set
;
get
;
}
/// <summary>
/// 相机图像高度
/// </summary>
public
int
Height
{
set
;
get
;
}
/// <summary>
/// 相机32位缓存
/// </summary>
public
byte
[]
Buffer
{
get
;
private
set
;
}
/// <summary>
/// 相机32位图像
/// </summary>
public
Bitmap
Image
{
get
;
private
set
;
}
/// <summary>
/// 打开指定相机
/// </summary>
/// <param name="idx">索引</param>
/// <returns></returns>
public
bool
Open
(
MyCamera
.
MV_CC_DEVICE_INFO
device
)
{
if
(
cameraCur
==
null
)
return
false
;
int
nRet
=
cameraCur
.
MV_CC_CreateDevice_NET
(
ref
device
);
if
(
nRet
!=
MyCamera
.
MV_OK
)
return
false
;
nRet
=
cameraCur
.
MV_CC_OpenDevice_NET
();
if
(
nRet
!=
MyCamera
.
MV_OK
)
{
cameraCur
.
MV_CC_DestroyDevice_NET
();
return
false
;
}
if
(
device
.
nTLayerType
==
MyCamera
.
MV_GIGE_DEVICE
)
{
int
nPacketSize
=
cameraCur
.
MV_CC_GetOptimalPacketSize_NET
();
if
(
nPacketSize
>
0
)
nRet
=
cameraCur
.
MV_CC_SetIntValue_NET
(
"GevSCPSPacketSize"
,
(
uint
)
nPacketSize
);
}
cameraCur
.
MV_CC_SetEnumValue_NET
(
"AcquisitionMode"
,
2
);
//工作在连续模式
cameraCur
.
MV_CC_SetEnumValue_NET
(
"TriggerMode"
,
0
);
//连续模式
MyCamera
.
MVCC_INTVALUE
pstValue
=
new
MyCamera
.
MVCC_INTVALUE
();
nRet
=
cameraCur
.
MV_CC_GetWidth_NET
(
ref
pstValue
);
Width
=
(
int
)
pstValue
.
nCurValue
;
nRet
=
cameraCur
.
MV_CC_GetHeight_NET
(
ref
pstValue
);
Height
=
(
int
)
pstValue
.
nCurValue
;
return
true
;
}
/// <summary>
/// 关闭当前相机
/// </summary>
public
void
Close
()
{
if
(
cameraCur
!=
null
)
{
cameraCur
.
MV_CC_CloseDevice_NET
();
cameraCur
.
MV_CC_DestroyDevice_NET
();
cameraCur
=
null
;
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public
void
Stop
()
{
if
(
cameraCur
==
null
)
return
;
int
rtn
=
cameraCur
.
MV_CC_StopGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
}
/// <summary>
/// 抓取一张图像
/// </summary>
public
Bitmap
GrabOne
()
{
int
rtn
=
cameraCur
.
MV_CC_StartGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
null
;
MyCamera
.
MVCC_INTVALUE
stParam
=
new
MyCamera
.
MVCC_INTVALUE
();
rtn
=
cameraCur
.
MV_CC_GetIntValue_NET
(
"PayloadSize"
,
ref
stParam
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
null
;
uint
dataSize
=
stParam
.
nCurValue
;
byte
[]
dataArr
=
new
byte
[
dataSize
];
uint
buffSize
=
dataSize
*
3
+
2048
;
byte
[]
buffArr
=
new
byte
[
buffSize
];
IntPtr
pData
=
Marshal
.
UnsafeAddrOfPinnedArrayElement
(
dataArr
,
0
);
MyCamera
.
MV_FRAME_OUT_INFO_EX
stFrameInfo
=
new
MyCamera
.
MV_FRAME_OUT_INFO_EX
();
rtn
=
cameraCur
.
MV_CC_GetOneFrameTimeout_NET
(
pData
,
dataSize
,
ref
stFrameInfo
,
100000
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
null
;
MyCamera
.
MvGvspPixelType
enDstPixelType
=
stFrameInfo
.
enPixelType
;
switch
(
stFrameInfo
.
enPixelType
)
{
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono8
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono10
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono10_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono12
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono12_Packed
:
enDstPixelType
=
stFrameInfo
.
enPixelType
;
break
;
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGR8
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerRG8
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGB8
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerBG8
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGR10
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerRG10
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGB10
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerBG10
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGR12
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerRG12
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGB12
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerBG12
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGR10_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerRG10_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGB10_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerBG10_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGR12_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerRG12_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerGB12_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_BayerBG12_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_RGB8_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_YUV422_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_YUV422_YUYV_Packed
:
case
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_YCBCR411_8_CBYYCRYY
:
enDstPixelType
=
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_RGB8_Packed
;
break
;
}
IntPtr
pImage
=
Marshal
.
UnsafeAddrOfPinnedArrayElement
(
buffArr
,
0
);
MyCamera
.
MV_PIXEL_CONVERT_PARAM
stConverPixelParam
=
new
MyCamera
.
MV_PIXEL_CONVERT_PARAM
();
stConverPixelParam
.
nWidth
=
stFrameInfo
.
nWidth
;
stConverPixelParam
.
nHeight
=
stFrameInfo
.
nHeight
;
stConverPixelParam
.
pSrcData
=
pData
;
stConverPixelParam
.
nSrcDataLen
=
stFrameInfo
.
nFrameLen
;
stConverPixelParam
.
enSrcPixelType
=
stFrameInfo
.
enPixelType
;
stConverPixelParam
.
enDstPixelType
=
enDstPixelType
;
stConverPixelParam
.
pDstBuffer
=
pImage
;
stConverPixelParam
.
nDstBufferSize
=
buffSize
;
rtn
=
cameraCur
.
MV_CC_ConvertPixelType_NET
(
ref
stConverPixelParam
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
null
;
if
(
enDstPixelType
==
MyCamera
.
MvGvspPixelType
.
PixelType_Gvsp_Mono8
)
{
Image
=
new
Bitmap
(
stFrameInfo
.
nWidth
,
stFrameInfo
.
nHeight
,
stFrameInfo
.
nWidth
*
1
,
PixelFormat
.
Format8bppIndexed
,
pImage
);
ColorPalette
cp
=
Image
.
Palette
;
for
(
int
i
=
0
;
i
<
256
;
i
++)
cp
.
Entries
[
i
]
=
Color
.
FromArgb
(
i
,
i
,
i
);
Image
.
Palette
=
cp
;
int
picSize
=
Image
.
Width
*
Image
.
Height
;
Buffer
=
new
byte
[
picSize
];
Array
.
Copy
(
buffArr
,
Buffer
,
picSize
);
//Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height);
//BitmapData bmpData = Image.LockBits(rect, ImageLockMode.ReadWrite, Image.PixelFormat);
//IntPtr iPtr = bmpData.Scan0;
//int picSize = Image.Width * Image.Height;
//Buffer = new byte[picSize];
//Marshal.Copy(iPtr, Buffer, 0, picSize);
//Image.UnlockBits(bmpData);
}
else
{
for
(
int
i
=
0
;
i
<
stFrameInfo
.
nHeight
;
i
++)
{
for
(
int
j
=
0
;
j
<
stFrameInfo
.
nWidth
;
j
++)
{
byte
chRed
=
buffArr
[
i
*
stFrameInfo
.
nWidth
*
3
+
j
*
3
];
buffArr
[
i
*
stFrameInfo
.
nWidth
*
3
+
j
*
3
]
=
buffArr
[
i
*
stFrameInfo
.
nWidth
*
3
+
j
*
3
+
2
];
buffArr
[
i
*
stFrameInfo
.
nWidth
*
3
+
j
*
3
+
2
]
=
chRed
;
}
}
Image
=
new
Bitmap
(
stFrameInfo
.
nWidth
,
stFrameInfo
.
nHeight
,
stFrameInfo
.
nWidth
*
3
,
PixelFormat
.
Format24bppRgb
,
pImage
);
int
picSize
=
Image
.
Width
*
Image
.
Height
*
3
;
Buffer
=
new
byte
[
picSize
];
Array
.
Copy
(
buffArr
,
Buffer
,
picSize
);
return
Image
;
}
rtn
=
cameraCur
.
MV_CC_StopGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
{
}
return
null
;
}
/// <summary>
/// 抓取连续图像,触发GrabImage事件
/// </summary>
/// <param name="hWnd"></param>
public
void
GrabContinuous
(
IntPtr
hWnd
)
{
int
rtn
=
cameraCur
.
MV_CC_StartGrabbing_NET
();
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
rtn
=
cameraCur
.
MV_CC_Display_NET
(
hWnd
);
if
(
rtn
!=
MyCamera
.
MV_OK
)
return
;
}
}
}
CodeLibraryProject/编译好的DLL/CodeLibrary.dll
查看文件 @
21c3fca
此文件类型无法预览
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论