HIK.cs
41.8 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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Xml.Linq;
using HalconDotNet;
using MvCamCtrl.NET;
namespace CodeLibrary
{
partial class HIK : ClsCamera
{
/// <summary>
/// 当前相机
/// </summary>
private MyCamera[] cameraCurr;
/// <summary>
/// 所有相机列表
/// </summary>
private MyCamera.MV_CC_DEVICE_INFO_LIST cameraAll;
public override void Close(int index)
{
lock (cameraCurr)
{
if (cameraCurr[index] != null)
{
_isOpen[index] = false;
cameraCurr[index].MV_CC_CloseDevice_NET();
cameraCurr[index].MV_CC_DestroyDevice_NET();
cameraCurr[index] = null;
}
}
}
public override void Close(string name)
{
lock (cameraCurr)
{
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
_errInfo = "Not find";
return;
}
if (cameraCurr[index] != null)
{
_isOpen[index] = false;
cameraCurr[index].MV_CC_CloseDevice_NET();
cameraCurr[index].MV_CC_DestroyDevice_NET();
cameraCurr[index] = null;
}
}
}
public override void CloseAll()
{
if (cameraCurr == null)
return;
lock (cameraCurr)
{
HDLogUtil.info(" cameraCurr.Length : " + cameraCurr.Length.ToString());
for (int i = 0; i < cameraCurr.Length; i++)
{
if (cameraCurr[i] != null)
{
HDLogUtil.info($" cameraCurr[{i}] ");
_isOpen[i] = false;
cameraCurr[i].MV_CC_CloseDevice_NET();
cameraCurr[i].MV_CC_DestroyDevice_NET();
cameraCurr[i] = null;
}
}
}
}
public override Bitmap GrabOne(int index, PixelType pt = PixelType.Default)
{
if (cameraCurr[index] == null)
{
_errInfo = "Camera null";
return null;
}
try
{
for (int i = 0; i < 2; i++)
{
if (i > 0)
{
Close(index);
Open(index);
}
if (pt == PixelType.RGB8)
{
var nRet = cameraCurr[index].MV_CC_SetPixelFormat_NET((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetPixelFormat_NET PixelType_Gvsp_RGB8_Packed nRet: " + nRet);
continue;
}
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningHorizontal", 2);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningHorizontal nRet: " + nRet);
continue;
}
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningVertical", 2);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningVertical nRet: " + nRet);
continue;
}
return ImageCapture(index);
}
else
{
var nRet = cameraCurr[index].MV_CC_SetPixelFormat_NET((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetPixelFormat_NET PixelType_Gvsp_Mono8 nRet: " + nRet);
continue;
}
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningHorizontal", 1);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningHorizontal nRet: " + nRet);
continue;
}
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningVertical", 1);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningVertical nRet: " + nRet);
continue;
}
return ImageCapture2(index);
}
}
_errInfo = "OK";
return null;
}
catch (Exception ex)
{
_errInfo = ex.Message;
return null;
}
finally
{
cameraCurr[index].MV_CC_StopGrabbing_NET();
}
}
public override Bitmap GrabOne(string name, PixelType pt = PixelType.Default)
{
int idx = Array.FindIndex(_name, s => s == name);
if (idx == -1)
return null;
else
return GrabOne(idx, pt);
}
public override bool Load()
{
try
{
cameraAll = new MyCamera.MV_CC_DEVICE_INFO_LIST();
int rtn = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref cameraAll);
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Load failed";
HDLogUtil.error(" Load camera error : " + _errInfo);
return false;
}
cameraName = new List<string>();
List<IntPtr> list = new List<IntPtr>();
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 + ")";
//Marshal.FreeHGlobal(buffer);
}
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 + ")";
//Marshal.FreeHGlobal(buffer);
}
if (!s.Contains("MV-ID"))
{
cameraName.Add(s);
list.Add(cameraAll.pDeviceInfo[i]);
}
}
cameraAll.pDeviceInfo= list.ToArray();
cameraAll.nDeviceNum = (uint)list.Count;
_name = cameraName.ToArray();
_count = cameraName.Count;
_isOpen = new bool[_count];
_width = new int[_count];
_height = new int[_count];
cameraCurr = new MyCamera[_count];
_errInfo = "OK";
return true;
}
catch (Exception ex)
{
_errInfo = ex.Message;
return false;
}
}
public override bool Open(int index)
{
lock (cameraCurr)
{
// _index = index;
if (index < 0 || index >= _count)
{
_errInfo = "Not find";
HDLogUtil.error("open camera " + index + " error : " + _errInfo);
return false;
}
if (cameraCurr[index] != null) Close(index);
try
{
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(cameraAll.pDeviceInfo[index], typeof(MyCamera.MV_CC_DEVICE_INFO));
cameraCurr[index] = new MyCamera();
int nRet = cameraCurr[index].MV_CC_CreateDevice_NET(ref device);
if (nRet != MyCamera.MV_OK) return false;
nRet = cameraCurr[index].MV_CC_OpenDevice_NET();
if (nRet != MyCamera.MV_OK)
{
cameraCurr[index].MV_CC_DestroyDevice_NET();
_errInfo = "Can not open";
HDLogUtil.error("open camera " + index + " error : " + _errInfo);
return false;
}
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = cameraCurr[index].MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0) nRet = cameraCurr[index].MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
}
//cameraCurr[index].MV_CC_SetEnumValue_NET("AcquisitionMode", 2); //工作在连续模式
cameraCurr[index].MV_CC_SetEnumValue_NET("TriggerMode", 0); //连续模式
MyCamera.MVCC_INTVALUE pstValue = new MyCamera.MVCC_INTVALUE();
nRet = cameraCurr[index].MV_CC_GetWidth_NET(ref pstValue);
_width[index] = (int)pstValue.nCurValue;
nRet = cameraCurr[index].MV_CC_GetHeight_NET(ref pstValue);
_height[index] = (int)pstValue.nCurValue;
/* C#
设置心跳时间:一般推荐设置3s-6s,设置太小,网络环境不稳定的情况下,会出现
掉线
在 MV_CC_Startgrab 接口调用前设置*/
//nRet = cameraCurr[index].MV_CC_SetIntValue_NET("GevHeartbeatTimeout", 3000);
nRet = cameraCurr[index].MV_CC_SetIntValueEx_NET("GevHeartbeatTimeout", 3000);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.info("Set GevHeartbeatTimeout failed:"+ nRet);
}
_isOpen[index] = true;
_errInfo = "OK";
HDLogUtil.info("open camera " + index + _errInfo);
return true;
}
catch (Exception ex)
{
HDLogUtil.error("打开相机出错:" + ex.ToString());
_errInfo = ex.Message;
return false;
}
}
}
public override bool Open(string name)
{
int n = cameraName.FindIndex(s => s == name);
if (n == -1)
{
_errInfo = name + " Not find";
return false;
}
else
return Open(n);
}
public override Bitmap GrabOneImage(string name, PixelType pt = PixelType.Default)
{
int n = cameraName.FindIndex(s => s == name);
if (n == -1)
{
_errInfo = name + " Not find";
HDLogUtil.error("GrabOneImage [" + name + "] error : " + _errInfo);
return null;
}
if (cameraCurr[n] != null)
{
}
else
{
Open(name);
}
return GrabOne(name, pt);
}
public override bool OpenAll()
{
lock (cameraCurr)
{
bool rtn = true;
for (int i = 0; i < cameraName.Count; i++)
{
rtn = Open(i);
if (!rtn) break;
}
return rtn;
}
}
private Bitmap _ImageCapture(int index)
{
int rtn = cameraCurr[index].MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
return null;
}
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
rtn = cameraCurr[index].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 = cameraCurr[index].MV_CC_GetOneFrameTimeout_NET(pData, dataSize, ref stFrameInfo, 3000);
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 = cameraCurr[index].MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
//if (rtn != MyCamera.MV_OK) return null;
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{
Bitmap _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;
//byte[] _buffer = new byte[picSize];
//Array.Copy(buffArr, _buffer, picSize);
return _image;
}
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;
}
}
Bitmap _image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
//int picSize = _image.Width * _image.Height * 3;
//byte[] _buffer = new byte[picSize];
//Array.Copy(buffArr, _buffer, picSize);
return DeepClone(_image);
}
}
private Bitmap ImageCapture(int index)
{
lock (cameraCurr[index])
{
MyCamera.MV_FRAME_OUT FrameInfo = new MyCamera.MV_FRAME_OUT();
try
{
int rtn = cameraCurr[index].MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
HDLogUtil.error("CaptureOnImage [" + index + "] error : " + _errInfo);
return null;
}
int nRet = cameraCurr[index].MV_CC_GetImageBuffer_NET(ref FrameInfo, 5000);
var enDstPixelType = FrameInfo.stFrameInfo.enPixelType;
// ch:获取一帧图像 | en:Get one image
if (MyCamera.MV_OK != nRet)
{
return null;
}
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{
Bitmap _image = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, FrameInfo.pBufAddr);
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;
//byte[] _buffer = new byte[picSize];
//Array.Copy(buffArr, _buffer, picSize);
return _image;
}
else
{
var buffArr = new byte[FrameInfo.stFrameInfo.nWidth * FrameInfo.stFrameInfo.nHeight * 3];
Marshal.Copy(FrameInfo.pBufAddr, buffArr, 0, buffArr.Length);
for (int i = 0; i < FrameInfo.stFrameInfo.nHeight; i++)
{
for (int j = 0; j < FrameInfo.stFrameInfo.nWidth; j++)
{
byte chRed = buffArr[i * FrameInfo.stFrameInfo.nWidth * 3 + j * 3];
buffArr[i * FrameInfo.stFrameInfo.nWidth * 3 + j * 3] = buffArr[i * FrameInfo.stFrameInfo.nWidth * 3 + j * 3 + 2];
buffArr[i * FrameInfo.stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
}
}
IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffArr, 0);
Bitmap _image = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
//int picSize = _image.Width * _image.Height * 3;
//byte[] _buffer = new byte[picSize];
//Array.Copy(buffArr, _buffer, picSize);
//return DeepClone(_image);
return DeepClone(_image);
}
}
catch
{
return null;
}
finally
{
cameraCurr[index].MV_CC_StopGrabbing_NET();
try
{
cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
}
catch (Exception ex)
{
HDLogUtil.error(" [" + index + "] MV_CC_FreeImageBuffer_NET 出错:" + ex.ToString());
}
}
}
}
private Bitmap ImageCapture2(int index)
{
MyCamera.MV_FRAME_OUT FrameInfo = new MyCamera.MV_FRAME_OUT();
try
{
int rtn = cameraCurr[index].MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
HDLogUtil.error("CaptureOnImage [" + index + "] error : " + _errInfo);
return null;
}
int nRet = cameraCurr[index].MV_CC_GetImageBuffer_NET(ref FrameInfo, 5000);
// ch:获取一帧图像 | en:Get one image
if (MyCamera.MV_OK == nRet)
{
HDLogUtil.debug("Get Image Buffer:" + "Width[" + Convert.ToString(FrameInfo.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.stFrameInfo.nHeight)
+ "] , FrameNum[" + Convert.ToString(FrameInfo.stFrameInfo.nFrameNum) + "]");
Bitmap bmpcopy=null;
try
{
Bitmap bmp;
HDLogUtil.info(FrameInfo.stFrameInfo.enPixelType.ToString());
bmp = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, FrameInfo.pBufAddr);
ColorPalette pal = bmp.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;
bmpcopy = DeepClone(bmp);
bmp.Dispose();
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
if (FrameInfo.pBufAddr != IntPtr.Zero)
{
nRet = cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
if (nRet != MyCamera.MV_OK)
{
HDLogUtil.error(" [" + index + "] Free Image Buffer fail:" + nRet);
}
}
//return ho_Imagetemp;
return bmpcopy;
}
else
{
HDLogUtil.error(" [" + index + "] MV_CC_GetImageBuffer_NET No data: " + nRet);
}
}
catch (Exception ex)
{
_errInfo = ex.Message;
return null;
}
finally
{
try
{
cameraCurr[index].MV_CC_StopGrabbing_NET();
cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
cameraCurr[index].MV_CC_CloseDevice_NET();
}
catch (Exception ex)
{
HDLogUtil.error(" [" + index + "] MV_CC_FreeImageBuffer_NET 出错:" + ex.ToString());
}
}
return null;
}
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)
{
HDLogUtil.error("DeepClone" + e.ToString());
return default;
}
}
public override HObject CaptureOnImage(string name, out List<CodeInfo> cc)
{
cc = null;
var r = CaptureOnImage(name, out Bitmap bmp,out List<CodeInfo> code);
bmp.Dispose();
return r;
}
public override HObject CaptureOnImage(string name, out Bitmap bmp,out List<CodeInfo> codeInfos, bool nohalcon=false)
{
codeInfos = null;
HObject hoImage = null;
bmp = null;
Bitmap bmpcopy;
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
_errInfo = name + "Not find";
HDLogUtil.error("CaptureOnImage [" + name + "] error : " + _errInfo);
return hoImage;
}
if (cameraCurr[index] != null)
{
HDLogUtil.info("CaptureOnImage [" + name + "] 相机已打开 ");
}
else
{
HDLogUtil.info("CaptureOnImage [" + name + "] 先打开相机 ");
if (!Open(name)) {
CloseAll();
Thread.Sleep(100);
if (!Open(name))
{
HDLogUtil.info("CaptureOnImage [" + name + "] 第二次打开相机失败 ");
return null;
}
}
}
lock (cameraCurr[index])
{
int nRet = 0;
nRet = cameraCurr[index].MV_CC_SetPixelFormat_NET((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8);
if (MyCamera.MV_OK != nRet)
{
HDLogUtil.error("MV_CC_SetPixelFormat_NET nRet: " + nRet);
}
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningHorizontal", 1);
if (MyCamera.MV_OK != nRet)
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningHorizontal nRet: " + nRet);
nRet = cameraCurr[index].MV_CC_SetEnumValue_NET("BinningVertical", 1);
if (MyCamera.MV_OK != nRet)
HDLogUtil.error("MV_CC_SetEnumValue_NET BinningVertical nRet: " + nRet);
MyCamera.MV_FRAME_OUT FrameInfo = new MyCamera.MV_FRAME_OUT();
try
{
int rtn = cameraCurr[index].MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
HDLogUtil.error("CaptureOnImage [" + name + "] error : " + _errInfo);
return hoImage;
}
//cameraCurr[index].MV_CC_ClearImageBuffer_NET();
var preLostFrameInfo = GetLostFrame(cameraCurr[index]);
HDLogUtil.info("prelost:" + preLostFrameInfo.ToString());
HDLogUtil.info("CaptureOnImage [" + name + "] MV_CC_StartGrabbing_NET ");
nRet = cameraCurr[index].MV_CC_GetImageBuffer_NET(ref FrameInfo, 6000);
HDLogUtil.info("CaptureOnImage [" + name + "] MV_CC_StartGrabbing_NET:" + nRet);
var pLostFrameInfo = GetLostFrame(cameraCurr[index]);
HDLogUtil.info("prelost:" + pLostFrameInfo.ToString());
// ch:获取一帧图像 | en:Get one image
if (MyCamera.MV_OK == nRet)
{
HDLogUtil.debug("Get Image Buffer:" + "Width[" + Convert.ToString(FrameInfo.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.stFrameInfo.nHeight)
+ "] , FrameNum[" + Convert.ToString(FrameInfo.stFrameInfo.nFrameNum) + "]");
//HObject ho_Imagetemp;
if (!nohalcon)
HOperatorSet.GenImage1(out hoImage, "byte", FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.pBufAddr);
try
{
HDLogUtil.info(FrameInfo.stFrameInfo.enPixelType.ToString());
bmpcopy = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, FrameInfo.pBufAddr);
ColorPalette pal = bmpcopy.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(i, i, i);
}
bmpcopy.Palette = pal;
bmp = DeepClone(bmpcopy);
bmpcopy.Dispose();
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
if (FrameInfo.pBufAddr != IntPtr.Zero)
{
nRet = cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
if (nRet != MyCamera.MV_OK)
{
HDLogUtil.error(" [" + name + "] Free Image Buffer fail:" + nRet);
}
}
//return ho_Imagetemp;
return hoImage;
}
else
{
HDLogUtil.error(" [" + name + "] MV_CC_GetImageBuffer_NET No data: " + nRet);
}
}
catch (Exception ex)
{
_errInfo = ex.Message;
HDLogUtil.error(ex.ToString());
return hoImage;
}
finally
{
cameraCurr[index].MV_CC_StopGrabbing_NET();
try
{
cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
}
catch (Exception ex)
{
HDLogUtil.error(" [" + name + "] MV_CC_FreeImageBuffer_NET 出错:" + ex.ToString());
}
}
}
return hoImage;
}
public override bool ProcessOnImage(string name, out object[] result, params Func<string,Bitmap, object>[] func)
{
result = new object[func.Length];
int index = cameraName.FindIndex(s => s == name);
if (index == -1)
{
_errInfo = name + "Not find";
HDLogUtil.error("ProcessOnImage [" + name + "] error : " + _errInfo);
return false;
}
if (cameraCurr[index] != null)
{
}
else
{
HDLogUtil.info("ProcessOnImage [" + name + "] 先打开相机 ");
Open(name);
}
MyCamera.MV_FRAME_OUT FrameInfo = new MyCamera.MV_FRAME_OUT();
try
{
int rtn = cameraCurr[index].MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK)
{
_errInfo = "Can not grab one : " + rtn;
HDLogUtil.error("ProcessOnImage [" + name + "] error : " + _errInfo);
return false;
}
//cameraCurr[index].MV_CC_ClearImageBuffer_NET();
var preLostFrameInfo = GetLostFrame(cameraCurr[index]);
HDLogUtil.info("prelost:"+preLostFrameInfo.ToString());
int nRet = cameraCurr[index].MV_CC_GetImageBuffer_NET(ref FrameInfo, 3000);
// ch:获取一帧图像 | en:Get one image
if (MyCamera.MV_OK == nRet)
{
var LostFrameInfo = GetLostFrame(cameraCurr[index]);
HDLogUtil.info("currentlost:" + LostFrameInfo.ToString());
HDLogUtil.debug("Get Image Buffer:" + "Width[" + Convert.ToString(FrameInfo.stFrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.stFrameInfo.nHeight)
+ "] , FrameNum[" + Convert.ToString(FrameInfo.stFrameInfo.nFrameNum) + "]");
try
{
//HDLogUtil.info(FrameInfo.stFrameInfo.enPixelType.ToString());
Bitmap bmp = new Bitmap(FrameInfo.stFrameInfo.nWidth, FrameInfo.stFrameInfo.nHeight, FrameInfo.stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, FrameInfo.pBufAddr);
ColorPalette pal = bmp.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = pal;
for (int i = 0; i > func.Length; i++) {
try
{
HDLogUtil.info("$func:{i} process");
result[i] = func[i](name,bmp);
}
catch (Exception ex)
{
HDLogUtil.error("$func:{i},"+ex.ToString());
}
}
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
if (FrameInfo.pBufAddr != IntPtr.Zero)
{
nRet = cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
if (nRet != MyCamera.MV_OK)
{
HDLogUtil.error(" [" + name + "] Free Image Buffer fail:" + nRet);
return false;
}
}
}
else
{
HDLogUtil.error(" [" + name + "] MV_CC_GetImageBuffer_NET No data: " + nRet);
return false;
}
}
catch (Exception ex)
{
_errInfo = ex.Message;
return false;
}
finally
{
cameraCurr[index].MV_CC_StopGrabbing_NET();
try
{
cameraCurr[index].MV_CC_FreeImageBuffer_NET(ref FrameInfo);
}
catch (Exception ex)
{
HDLogUtil.error(" [" + name + "] MV_CC_FreeImageBuffer_NET 出错:" + ex.ToString());
}
Close(index);
}
return true;
}
private LostFrameInfo GetLostFrame(MyCamera _myCamera)
{
MyCamera.MV_ALL_MATCH_INFO pstInfo = new MyCamera.MV_ALL_MATCH_INFO();
LostFrameInfo lostFrameInfo = new LostFrameInfo();
MyCamera.MV_MATCH_INFO_NET_DETECT MV_NetInfo = new MyCamera.MV_MATCH_INFO_NET_DETECT();
pstInfo.nInfoSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MyCamera.MV_MATCH_INFO_NET_DETECT));
pstInfo.nType = MyCamera.MV_MATCH_TYPE_NET_DETECT;
int size = Marshal.SizeOf(MV_NetInfo);
try
{
pstInfo.pInfo = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(MV_NetInfo, pstInfo.pInfo, false);
_myCamera.MV_CC_GetAllMatchInfo_NET(ref pstInfo);
MV_NetInfo = (MyCamera.MV_MATCH_INFO_NET_DETECT)Marshal.PtrToStructure(pstInfo.pInfo, typeof(MyCamera.MV_MATCH_INFO_NET_DETECT));
lostFrameInfo.SetPara(MV_NetInfo);
}
catch (Exception ex)
{
HDLogUtil.error(ex.ToString());
}
finally
{
Marshal.FreeHGlobal(pstInfo.pInfo);
}
return lostFrameInfo;
}
class LostFrameInfo
{
/// <summary>
/// 已接收数据大小 [统计StartGrabbing和StopGrabbing之间的数据量]
/// </summary>
public long nReviceDataSize;
//Gige
/// <summary>
/// 丢失的包数量
/// </summary>
public long nLostPacketCount;
/// <summary>
/// 丢帧数量
/// </summary>
public uint nLostFrameCount;
public uint nNetRecvFrameCount;
/// <summary>
/// 请求重发包数
/// </summary>
public long nRequestResendPacketCount;
/// <summary>
/// 重发包数
/// </summary>
public long nResendPacketCount;
public override string ToString()
{
return $"nReviceDataSize:{nReviceDataSize}; nLostPacketCount:{nLostPacketCount}; nLostFrameCount:{nLostFrameCount}; nNetRecvFrameCount:{nNetRecvFrameCount}" +
$"; RequestResendPacketCount:{nRequestResendPacketCount}; nResendPacketCount:{nResendPacketCount}";
}
/// <summary>
/// 跟前一个LostFrameInfo做对比
/// </summary>
/// <param name="preLostFrameInfo"></param>
/// <returns></returns>
public bool IsLostFrame(LostFrameInfo preLostFrameInfo)
{
return this.nLostFrameCount > preLostFrameInfo.nLostFrameCount;
}
public bool IsLostFrame()
{
return this.nLostFrameCount > 0 || nLostPacketCount > 0;
}
/// <summary>
/// 跟前一个LostFrameInfo做对比,重影问题就是这个值突然变大
/// </summary>
/// <param name="preLostFrameInfo"></param>
/// <returns></returns>
public bool IsLostPacket(LostFrameInfo preLostFrameInfo)
{
return this.nLostPacketCount > preLostFrameInfo.nLostPacketCount;
}
public bool IsLostPacket()
{
return this.nLostPacketCount > 0 || nLostPacketCount > 0;
}
public void SetPara(MyCamera.MV_MATCH_INFO_NET_DETECT MV_NetInfo)
{
this.nReviceDataSize = MV_NetInfo.nReviceDataSize;
this.nLostPacketCount = MV_NetInfo.nLostPacketCount;
this.nLostFrameCount = MV_NetInfo.nLostFrameCount;
this.nNetRecvFrameCount = MV_NetInfo.nNetRecvFrameCount;
this.nRequestResendPacketCount = MV_NetInfo.nRequestResendPacketCount;
this.nResendPacketCount = MV_NetInfo.nResendPacketCount;
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);
HalconDotNet.HalconAPI.HClearProcCallBack callback = __OnFreeCallBack;
private static void __OnFreeCallBack(IntPtr pImg)
{
Marshal.FreeHGlobal(pImg);
}
public override List<CodeInfo> GetImageAndIdentifyCode(out Bitmap bitmap)
{
List<CodeInfo> codeInfos= new List<CodeInfo>();
bitmap = null;
HDLogUtil.error("获取图片相机名称:" + cameraName);
if (cameraName==null|| cameraName.Count==0)
{
return codeInfos;
}
bitmap = ImageCapture(0);
return codeInfos;
}
}
}