HIKCameraBean.cs
10.0 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
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;
}
}
}