HIK_VisionCamera.cs
19.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using MvCamCtrl.NET;
using CameraVisionLib.Model;
namespace Asa.HIK
{
/// <summary>
/// 机器视觉面阵相机
/// </summary>
internal class VisionCamera : ICamera
{
private MyCamera.MV_CC_DEVICE_INFO_LIST cameraAll;
private MyCamera[] cameraCurr;
private List<string> cameraName;
private int result;
private byte[] _buffer = null;
private Bitmap _image = null;
private IntPtr _handle = IntPtr.Zero;
private PixelFormat _format = PixelFormat.Format24bppRgb;
/// <summary>
/// 机器视觉面阵相机
/// </summary>
public VisionCamera()
{
cameraName = new List<string>();
}
/// <summary>
/// 相机名称
/// </summary>
public string[] Name { get => cameraName.ToArray(); }
/// <summary>
/// 相机总数
/// </summary>
public int Count { get => cameraName.Count; }
/// <summary>
/// 相机是否打开
/// </summary>
public bool[] IsOpen { private set; get; }
/// <summary>
/// 图像分辨率
/// </summary>
public Size[] Size { private set; get; }
/// <summary>
/// 相机获取到的图像
/// </summary>
public Bitmap Image { private set; get; }
/// <summary>
/// 加载
/// </summary>
/// <returns></returns>
public bool Load()
{
try
{
//获取所有相机
cameraAll = new MyCamera.MV_CC_DEVICE_INFO_LIST();
result = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref cameraAll);
if (result != MyCamera.MV_OK)
{
Common.log.Info("MV_CC_EnumDevices_NET failed," + result);
return false;
}
//相机名称
cameraName.Clear();
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));
cameraName.Add(string.Format("GigE:{0}({1})", 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));
cameraName.Add(string.Format("USB:{0}({1})", usbInfo.chModelName, usbInfo.chSerialNumber));
}
}
IsOpen = new bool[cameraName.Count];
Size = new Size[cameraName.Count];
cameraCurr = new MyCamera[cameraName.Count];
Common.log.Info("Load OK");
return true;
}
catch (Exception ex)
{
Common.log.Error("Load", ex);
return false;
}
}
/// <summary>
/// 释放所有
/// </summary>
public void Dispose()
{
CloseAll();
}
/// <summary>
/// 打开所有相机
/// </summary>
/// <returns></returns>
public bool OpenAll()
{
bool rtn = false;
for (int i = 0; i < cameraName.Count; i++)
{
rtn = OpenDevice(i);
if (!rtn) break;
}
return rtn;
}
/// <summary>
/// 关闭所有相机
/// </summary>
public void CloseAll()
{
for (int i = 0; i < cameraName.Count; i++)
CloseDevice(i);
}
/// <summary>
/// 打开相机
/// </summary>
/// <param name="cameraIndex"></param>
/// <returns></returns>
public bool Open(int cameraIndex)
{
if (cameraIndex < 0)
return false;
else
return OpenDevice(cameraIndex);
}
/// <summary>
/// 打开相机
/// </summary>
/// <param name="cameraName"></param>
/// <returns></returns>
public bool Open(string cameraName)
{
int idx = this.cameraName.FindIndex(s => s == cameraName);
return Open(idx);
}
/// <summary>
/// 关闭相机
/// </summary>
/// <param name="cameraIndex"></param>
public void Close(int cameraIndex)
{
CloseDevice(cameraIndex);
}
/// <summary>
/// 关闭相机
/// </summary>
/// <param name="cameraName"></param>
public void Close(string cameraName)
{
int idx = this.cameraName.FindIndex(s => s == cameraName);
if (idx>=0)
CloseDevice(idx);
}
/// <summary>
/// 抓取所有相机一张图像
/// </summary>
/// <param name="bmp"></param>
public void GrabOne(out Bitmap[] bmp)
{
bmp = new Bitmap[cameraCurr.Length];
for (int i = 0; i < cameraCurr.Length; i++)
GrabDevice(i, out bmp[i]);
}
/// <summary>
/// 抓取一张图像
/// </summary>
/// <param name="cameraIndex">相机索引</param>
/// <returns></returns>
public bool GrabOne(int cameraIndex)
{
bool rtn = GrabDevice(cameraIndex, out Bitmap bmp);
Image = bmp;
return rtn;
}
/// <summary>
/// 抓取一张图像
/// </summary>
/// <param name="cameraIndex">相机索引</param>
/// <param name="bmp">Bitmap图像</param>
/// <returns></returns>
public bool GrabOne(int cameraIndex, out Bitmap bmp)
{
return GrabDevice(cameraIndex, out bmp);
}
/// <summary>
/// 抓取一张图像
/// </summary>
/// <param name="cameraName">相机名称</param>
/// <param name="bmp">Bitmap图像</param>
/// <returns></returns>
public bool GrabOne(string cameraName, out Bitmap bmp)
{
bmp = null;
int index = this.cameraName.FindIndex(match => match.Contains(cameraName));
if (index == -1) return false;
return GrabDevice(index, out bmp);
}
/// <summary>
/// 抓取一张图像字节
/// </summary>
/// <param name="cameraIndex">相机索引</param>
/// <param name="buff">图像字节数组</param>
/// <param name="format"></param>
/// <returns></returns>
public bool GrabOne(int cameraIndex, out byte[] buff, out PixelFormat format)
{
return GrabDevice(cameraIndex, out buff, out format);
}
/// <summary>
/// 抓取一张图像指针
/// </summary>
/// <param name="cameraIndex"></param>
/// <param name="handle"></param>
/// <param name="format"></param>
/// <returns></returns>
public bool GrabOne(int cameraIndex, out IntPtr handle, out PixelFormat format)
{
return GrabDevice(cameraIndex, out handle, out format);
}
//public void PreviewImage(int cameraIndex, IntPtr handle)
//{
//}
public bool PreviewImage(string cameraName, IntPtr handle)
{
return false;
}
private bool OpenDevice(int idx)
{
try
{
if (cameraCurr[idx] != null)
CloseDevice(idx);
//创建设备
cameraCurr[idx] = new MyCamera();
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(cameraAll.pDeviceInfo[idx], typeof(MyCamera.MV_CC_DEVICE_INFO));
result = cameraCurr[idx].MV_CC_CreateDevice_NET(ref device);
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_CreateDevice_NET failed,{1}", cameraName[idx], result));
return false;
}
//打开设备
result = cameraCurr[idx].MV_CC_OpenDevice_NET();
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_OpenDevice_NET failed,{1}", cameraName[idx], result));
cameraCurr[idx].MV_CC_DestroyDevice_NET();
return false;
}
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = cameraCurr[idx].MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0)
result = cameraCurr[idx].MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
}
//cameraCurr[idx].MV_CC_SetEnumValue_NET("AcquisitionMode", 2); //工作在连续模式
cameraCurr[idx].MV_CC_SetEnumValue_NET("TriggerMode", 0); //连续模式
MyCamera.MVCC_INTVALUE pstValue = new();
result = cameraCurr[idx].MV_CC_GetWidth_NET(ref pstValue);
int w = (int)pstValue.nCurValue;
result = cameraCurr[idx].MV_CC_GetHeight_NET(ref pstValue);
int h = (int)pstValue.nCurValue;
Size[idx] = new Size(w, h);
result = cameraCurr[idx].MV_CC_StartGrabbing_NET();
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_StartGrabbing_NET failed,{1}", cameraName[idx], result));
return false;
}
IsOpen[idx] = true;
Common.log.Info(string.Format("{0} OpenDevice OK", cameraName[idx]));
return true;
}
catch (Exception ex)
{
Common.log.Error("OpenDevice", ex);
return false;
}
}
private void CloseDevice(int idx)
{
if (cameraCurr[idx] == null) return;
IsOpen[idx] = false;
cameraCurr[idx].MV_CC_StopGrabbing_NET();
cameraCurr[idx].MV_CC_CloseDevice_NET();
cameraCurr[idx].MV_CC_DestroyDevice_NET();
cameraCurr[idx] = null;
Common.log.Info("Close OK " + cameraName[idx]);
}
private bool GrabDevice(int idx, out Bitmap bmp)
{
bmp = null;
try
{
ImageCapture(idx);
bmp = new(_image);
Common.log.Info(string.Format("{0} GrabDevice OK", cameraName[idx]));
return true;
}
catch (Exception ex)
{
Common.log.Error("GrabDevice", ex);
return false;
}
}
private bool GrabDevice(int idx, out byte[] buff, out PixelFormat format)
{
buff = null;
format = PixelFormat.Undefined;
try
{
ImageCapture(idx);
buff = _buffer;
format = _format;
Common.log.Info(string.Format("{0} GrabDevice OK", cameraName[idx]));
return true;
}
catch (Exception ex)
{
Common.log.Error("GrabDevice", ex);
return false;
}
}
private bool GrabDevice(int idx, out IntPtr handle, out PixelFormat format)
{
handle = IntPtr.Zero;
format = PixelFormat.Undefined;
try
{
ImageCapture(idx);
handle = _handle;
format = _format;
Common.log.Info(string.Format("{0} GrabDevice OK", cameraName[idx]));
return true;
}
catch (Exception ex)
{
Common.log.Error("GrabDevice", ex);
return false;
}
}
private void ImageCapture(int idx)
{
MyCamera.MVCC_INTVALUE stParam = new();
result = cameraCurr[idx].MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_GetIntValue_NET failed,{1}", cameraName[idx], result));
return;
}
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();
result = cameraCurr[idx].MV_CC_GetOneFrameTimeout_NET(pData, dataSize, ref stFrameInfo, 100000);
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_GetOneFrameTimeout_NET failed,{1}", cameraName[idx], result));
return;
}
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()
{
nWidth = stFrameInfo.nWidth,
nHeight = stFrameInfo.nHeight,
pSrcData = pData,
nSrcDataLen = stFrameInfo.nFrameLen,
enSrcPixelType = stFrameInfo.enPixelType,
enDstPixelType = enDstPixelType,
pDstBuffer = pImage,
nDstBufferSize = buffSize
};
result = cameraCurr[idx].MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (result != MyCamera.MV_OK)
{
Common.log.Info(string.Format("{0} MV_CC_ConvertPixelType_NET failed,{1}", cameraName[idx], result));
return;
}
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{
_image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);
_format = PixelFormat.Format8bppIndexed;
_handle = 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);
_format = PixelFormat.Format24bppRgb;
_handle = pImage;
int picSize = _image.Width * _image.Height * 3;
_buffer = new byte[picSize];
Array.Copy(buffArr, _buffer, picSize);
}
}
}
}