轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 06c627ec032b3f3876fd7db8a3ff0ff1a6614fa2
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
using Microsoft.VisualBasic.ApplicationServices;
using LB_VisionProcesses;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
using MVAPI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Dynamic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using LB_VisionProcesses.Cameras;
 
namespace LB_VisionProcesses.Cameras.MicroCameras
{
    public class MicroCamera : BaseCamera
    {
        #region variable
        IntPtr m_hCam = IntPtr.Zero;// 设备对象 | device object
        private int camIndex = -1;
        IntPtr m_hImage = IntPtr.Zero;
        MVAPI.MV_PixelFormatEnums m_PixelFormat;
        int m_nWidth;
        int m_nHeight;
        UInt32 m_nCapture;
        UInt32 m_nCapCntBase;
        UInt16 m_nLastID;
        UInt32 m_nReceive;
 
        MVAPI.MV_SNAPPROC StreamCBDelegate = null;
 
        public delegate int InvokeDraw();
        InvokeDraw invokeDraw = null;
 
        IAsyncResult ia = null;
        #endregion
 
        public MicroCamera() : base()
        {
            Brand = CameraBrand.MicroCamera;
            //MVGigE.MVInitLib();
        }
 
        public override bool AutoBalanceWhite()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
 
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
                res = MVGigE.MVSetBalanceWhiteAuto(m_hCam, BalanceWhiteAutoEnums.BalanceWhiteAuto_Once);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                {
                    Debug.WriteLine($"【{DateTime.Now:HH:mm:ss.fff}】MVSetBalanceWhiteAuto failed! ErrorCode[{res}]");
                    return false;
                }
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool CloseDevice()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return true;
 
                // 保存相机属性配置文件
                // Save configuration of the device
                SaveUserConfiguration();
                StreamCBDelegate -= new MVAPI.MV_SNAPPROC(StreamCB);
                StopGrabbing();
 
                MVGigE.MVCloseCam(m_hCam);
                MVAPI.MVImage.MVImageRelease(m_hImage);
                m_hCam = IntPtr.Zero;
                return true;
            }
            catch { return false; }
        }
 
        private bool SaveUserConfiguration()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                // 设置UserSet1配置为下次相机启动时使用的默认配置
                res = MVGigE.MVSetDefaultUserSet(m_hCam, UserSetSelectorEnums.UserSetSelector_UserSet1);
 
                res = MVGigE.MVSaveUserSet(m_hCam, UserSetSelectorEnums.UserSetSelector_UserSet1);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSaveUserSet failed! ErrorCode[{res}]");
                return true;
            }
            catch { return false; }
        }
 
        private bool LoadUserConfiguration()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVLoadUserSet(m_hCam, UserSetSelectorEnums.UserSetSelector_UserSet1);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVLoadUserSet failed! ErrorCode[{res}]");
 
                // 设置UserSet1配置为下次相机启动时使用的默认配置
                res = MVGigE.MVSetDefaultUserSet(m_hCam, UserSetSelectorEnums.UserSetSelector_UserSet1);
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetExpouseTime(out double value)
        {
            value = 0;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetExposureTime(m_hCam, out double pExposuretime);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVGetExposureTime failed! ErrorCode[{res}]");
 
                value = Convert.ToDouble(pExposuretime);
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetGain(out double value)
        {
            value = 0;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetGain(m_hCam, out double pGain);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVGetGain failed! ErrorCode[{res}]");
 
                value = Convert.ToDouble(pGain);
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
        {
            lineStatus = LineStatus.Low;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                return true;
            }
            catch { return false; }
        }
 
        public override List<string> GetListEnum()
        {
            List<string> snList = new List<string>();
            try
            {
                bool isfirst = true;
                MVGigE.MVUpdateCameraList();
 
                if (MVGigE.MVGetNumOfCameras(out int nCameraCount) == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                {
                    for (int i = 0; i < nCameraCount; i++)
                    {
                        MVAPI.MVCamInfo info;
                        MVGigE.MVGetCameraInfo((byte)i, out info);
 
                        IntPtr hCam;
                        string str = "否";
                        if (MVGigE.MVOpenCamByIndex((byte)i, out hCam) == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                        {
                            str = "是";
                            MVGigE.MVCloseCam(hCam);
                            if (isfirst == true)
                            {
                                isfirst = false;
                            }
                        }
 
                        ListViewItem lvi = new ListViewItem();
                        lvi.Tag = i;
                        lvi.Text = str;
                        lvi.SubItems.Add(info.mModelName);
                        lvi.SubItems.Add(info.mSerialNumber);
                        snList.Add($"{info.mSerialNumber}-{info.mUserDefinedName}");
                    }
                }
                else
                    Debug.WriteLine("无法刷新列表");
            }
            catch { }
            return snList;
        }
 
        public override bool GetTriggerDelay(out double delay)
        {
            delay = 0;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetTriggerDelay(m_hCam, out uint time);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVGetTriggerDelay failed! ErrorCode[{res}]");
 
                delay = Convert.ToDouble(time);
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetTriggerFliter(out double flitertime)
        {
            flitertime = 0;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetTimerDuration(m_hCam, out uint Duration);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVGetTimerDuration failed! ErrorCode[{res}]");
 
                flitertime = Convert.ToDouble(Duration);
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
        {
            mode = TriggerMode.Off;
            source = TriggerSource.Software;
            MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                res = MVGigE.MVGetTriggerMode(m_hCam, out MVAPI.TriggerModeEnums pMode);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                {
                    Debug.WriteLine($"MVGetTriggerMode failed! ErrorCode[{res}]");
                    return false;
                }
                switch (pMode)
                {
                    case MVAPI.TriggerModeEnums.TriggerMode_Off:
                        mode = TriggerMode.Off;
                        break;
                    case MVAPI.TriggerModeEnums.TriggerMode_On:
                        mode = TriggerMode.On;
                        break;
                }
                res = MVGigE.MVGetTriggerSource(m_hCam, out MVAPI.TriggerSourceEnums pSource);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                {
                    Debug.WriteLine($"MVGetTriggerSource failed! ErrorCode[{res}]");
                    return false;
                }
                switch (pSource)
                {
                    case MVAPI.TriggerSourceEnums.TriggerSource_Software:
                        source = TriggerSource.Software;
                        break;
                    case MVAPI.TriggerSourceEnums.TriggerSource_Line1:
                        source = TriggerSource.Line1;
                        break;
                }
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
        {
            polarity = TriggerPolarity.RisingEdge;
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetTriggerActivation(m_hCam, out TriggerActivationEnums pAct);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVGetTriggerActivation failed! ErrorCode[{res}]");
 
                switch (pAct)
                {
                    case TriggerActivationEnums.TriggerActivation_RisingEdge:
                        polarity = TriggerPolarity.RisingEdge;
                        break;
                    case TriggerActivationEnums.TriggerActivation_FallingEdge:
                        polarity = TriggerPolarity.FallingEdge;
                        break;
                }
                return true;
            }
            catch { return false; }
        }
 
        public override bool InitDevice(string CamSN, object handle)
        {
            try
            {
                bool isFind = false;
                bool isfirst = true;
                MVGigE.MVUpdateCameraList();
 
                if (MVGigE.MVGetNumOfCameras(out int nCameraCount) == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                {
                    for (int i = 0; i < nCameraCount; i++)
                    {
                        MVAPI.MVCamInfo info;
                        MVGigE.MVGetCameraInfo((byte)i, out info);
 
                        string str = "否";
                        if (MVGigE.MVOpenCamByIndex((byte)i, out IntPtr hCam) == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                        {
                            str = "是";
                            MVGigE.MVCloseCam(hCam);
                            if (isfirst == true)
                            {
                                isfirst = false;
                            }
                        }
 
                        ListViewItem lvi = new ListViewItem();
                        lvi.Tag = i;
                        lvi.Text = str;
                        lvi.SubItems.Add(info.mModelName);
                        lvi.SubItems.Add(info.mSerialNumber);
                        if (CamSN == $"{info.mSerialNumber}-{info.mUserDefinedName}")
                        {
                            SN = CamSN;
                            camIndex = i;
                        }
                    }
                }
 
                if (camIndex != -1)
                {
                    // 创建设备句柄
                    // Create Device Handle
                    // open device 
                    MVAPI.MVSTATUS_CODES res = MVGigE.MVOpenCamByIndex((byte)camIndex, out m_hCam);
 
                    if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    {
                        MessageBox.Show($"MVOpenCamByIndex failed! ErrorCode[{res}]", "异常");
                        return false;
                    }
 
                    int w, h;
                    res = MVGigE.MVGetWidth(m_hCam, out w);
                    if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                        Debug.WriteLine($"MVGetWidth failed! ErrorCode[{res}]");
 
                    res = MVGigE.MVGetHeight(m_hCam, out h);
                    if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                        Debug.WriteLine($"MVGetHeight failed! ErrorCode[{res}]");
 
                    res = MVGigE.MVGetPixelFormat(m_hCam, out m_PixelFormat);
                    if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                        Debug.WriteLine($"MVGetPixelFormat failed! ErrorCode[{res}]");
 
                    if (res == MVSTATUS_CODES.MVST_SUCCESS)
                    {
                        m_nWidth = w;
                        m_nHeight = h;
                        if (m_PixelFormat == MVAPI.MV_PixelFormatEnums.PixelFormat_Mono8)
                            m_hImage = MVAPI.MVImage.MVImageCreate(m_nWidth, m_nHeight, 8);
                        else
                            m_hImage = MVAPI.MVImage.MVImageCreate(m_nWidth, m_nHeight, 24);
                    }
 
                    // 设置为Off会一直触发回调
                    SetTriggerMode(TriggerMode.On, TriggerSource.Software);
 
                    //LoadUserConfiguration();
                    StreamCBDelegate += new MVAPI.MV_SNAPPROC(StreamCB);
 
                    // 开始拉流 
                    // Start grabbing 
                    if (!StartGrabbing())
                    {
                        Debug.WriteLine("开始采集失败");
                        return false;
                    }
 
                    return true;
                }
                else
                {
                    MessageBox.Show("相机未找到,请检查连接或SN是否正确!", "异常");
                    return false;
                }
            }
            catch { return false; }
        }
 
        private ColorPalette m_MonoColorPalette = null;
 
        public Bitmap ImageData2Bitmap(IntPtr hImage)
        {
            try
            {
                Bitmap bitmap = null;
                IntPtr ptrSrc = MVAPI.MVImage.MVImageGetBits(hImage);
                int nWidth = MVAPI.MVImage.MVImageGetWidth(hImage);
                int nHeight = MVAPI.MVImage.MVImageGetHeight(hImage);
                int bpp = MVImage.MVImageGetBPP(hImage);
                if (bpp == 8)
                {
                    bitmap = new Bitmap(nWidth, nHeight, nWidth,
                    System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ptrSrc);
                    if (m_MonoColorPalette == null)
                    {
                        Bitmap monoBitmap = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
                        m_MonoColorPalette = monoBitmap.Palette;
                        for (int i = 0; i < 256; i++)
                            m_MonoColorPalette.Entries[i] = Color.FromArgb(i, i, i);
                    }
                    bitmap.Palette = m_MonoColorPalette;
                }
                else
                {
                    bitmap = new Bitmap(nWidth, nHeight, 3 * nWidth,
                   System.Drawing.Imaging.PixelFormat.Format24bppRgb, ptrSrc);
                }
                return bitmap;
            }
            catch { return null; }
        }
 
        // 数据帧回调函数
        private int StreamCB(ref MVAPI.IMAGE_INFO pInfo, IntPtr UserVal)
        {
            //[FieldOffset(0)]
            //public UInt64 nTimeStamp;   ///< 时间戳,采集到图像的时刻,精度为0.01us ;
            //[FieldOffset(8)]
            //public ushort nBlockId;     ///< 帧号,从开始采集开始计数
            //[FieldOffset(28)]
            //public byte nMissingPackets;///< 传输过程中丢掉的包数量
            Debug.WriteLine($"StreamCB callback success!" +
                $"[时间戳:{pInfo.nTimeStamp}][帧号:{pInfo.nBlockId}][丢包数:{pInfo.nMissingPackets}]");
            UInt16 id = pInfo.nBlockId;
            if (id < m_nLastID)
            {
                if ((id < 100) && (m_nLastID > 60000))
                {
                    m_nCapCntBase += 65535;
                }
            }
            m_nLastID = id;
            m_nCapture = m_nCapCntBase + id;
            m_nReceive++;
 
            MVGigE.MVInfo2Image(m_hCam, ref pInfo, m_hImage);
 
            try
            {
                if (this.ImageGrabbed != null)
                    this.ImageGrabbed(this, new CameraEventArgs(SN, ImageData2Bitmap(m_hImage)));
 
                CallBackImg = ImageData2Bitmap(m_hImage);
 
                try
                {
                    if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                    {
                        if (mode == TriggerMode.On && source != TriggerSource.Software)
                            TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 触发运行事件
                    }
                }
                catch { }
            }
            catch { return -1; }
 
            return 0;
        }
 
        public override bool SetExpouseTime(double value)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
                res = MVGigE.MVGetExposureTimeRange(m_hCam, out double minExp, out double maxExp);
 
                if (value >= minExp && value <= maxExp && res == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    res = MVGigE.MVSetExposureTime(m_hCam, value);
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetExposureTime failed! ErrorCode[{res}]");
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetGain(double gain)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVGetGainRange(m_hCam, out double minGaim, out double maxGain);
 
                if (gain >= minGaim && gain <= maxGain && res == MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    res = MVGigE.MVSetGain(m_hCam, gain);
                else
                    Debug.WriteLine($"Gain value out of range! Min: {minGaim}, Max: {maxGain}");
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetGain failed! ErrorCode[{res}]");
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetLineMode(IOLines line, LineMode mode)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetTriggerDelay(double delay)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVSetTriggerDelay(m_hCam, Convert.ToUInt16(delay));
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetTriggerDelay failed! ErrorCode[{res}]");
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetTriggerFliter(double flitertime)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVSetTimerDuration(m_hCam, Convert.ToUInt16(flitertime));
 
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetTimerDuration failed! ErrorCode[{res}]");
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool SetTriggerMode(TriggerMode mode, TriggerSource source = TriggerSource.None)
        {
            if (IntPtr.Zero == m_hCam)
                return false;
            MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
            // 设置触发模式 
            // Set trigger mode to On 
            res = MVGigE.MVSetTriggerMode(m_hCam, mode == TriggerMode.On ?
                MVAPI.TriggerModeEnums.TriggerMode_On : MVAPI.TriggerModeEnums.TriggerMode_Off);
 
            if (mode != TriggerMode.On)
            {
                double fps = 0;
                double fpsMin = 0;
                int t = 0;
 
                MVGigE.MVGetFrameRateRange(m_hCam, out fpsMin, out fps);
                t = (int)(1500 / fps);
                Thread.Sleep(t);
            }
 
            if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                Debug.WriteLine($"MVSetTriggerMode failed! ErrorCode[{res}]");
 
            res = MVGigE.MVSetTriggerSource(m_hCam, source == TriggerSource.Software ?
                MVAPI.TriggerSourceEnums.TriggerSource_Software : MVAPI.TriggerSourceEnums.TriggerSource_Line1);
 
            if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                Debug.WriteLine($"MVSetTriggerSource failed! ErrorCode[{res}]");
 
            if (source != TriggerSource.Software)
            {
                res = MVGigE.MVSetStrobeSource(m_hCam, MVAPI.LineSourceEnums.LineSource_ExposureActive);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetStrobeSource failed! ErrorCode[{res}]");
                MVGigE.MVSetStrobeInvert(m_hCam, true);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetStrobeInvert failed! ErrorCode[{res}]");
            }
 
            return true;
        }
 
        public override bool SetTriggerPolarity(TriggerPolarity polarity)
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVSetTriggerActivation(m_hCam, TriggerActivationEnums.TriggerActivation_FallingEdge);
                switch (polarity)
                {
                    case TriggerPolarity.RisingEdge:
                        res = MVGigE.MVSetTriggerActivation(m_hCam, TriggerActivationEnums.TriggerActivation_RisingEdge);
                        break;
                    case TriggerPolarity.FallingEdge:
                        res = MVGigE.MVSetTriggerActivation(m_hCam, TriggerActivationEnums.TriggerActivation_FallingEdge);
                        break;
                    default:
                        res = MVSTATUS_CODES.MVST_ERROR;
                        break;
                }
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVSetTriggerActivation failed! ErrorCode[{res}]");
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool SoftTrigger()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return false;
 
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
 
                res = MVGigE.MVTriggerSoftware(m_hCam);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVTriggerSoftware failed! ErrorCode[{res}]");
                //else
                //    Debug.WriteLine("MVTriggerSoftware success!");
 
                //StreamCBDelegate -= new MVAPI.MV_SNAPPROC(StreamCB);
                //res = MVGigE.MVSingleGrab(m_hCam, m_hImage, 1000);
                //Bitmap bitmap = ImageData2Bitmap(m_hImage);
                //if (bitmap == null)
                //    Debug.WriteLine("ImageData2Bitmap failed!");
 
                //CallBackImg = bitmap.Clone() as Bitmap;
                //if (this.ImageGrabbed != null)
                //    this.ImageGrabbed(this, new CameraEventArgs(SN, CallBackImg));
                //StreamCBDelegate += new MVAPI.MV_SNAPPROC(StreamCB);
 
                return true;
            }
            catch { return false; }
        }
 
        public override bool StartGrabbing()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                {
                    Debug.WriteLine($"m_hCam is NULL!");
                    return false;
                }
 
                CallBackImg = null;
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
                res = MVGigE.MVStartGrab(m_hCam, StreamCBDelegate, IntPtr.Zero);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVStartGrab failed! ErrorCode[{res}]");
 
                isGrabbing = true;
                return true;
            }
            catch { return false; }
        }
 
        public override bool StopGrabbing()
        {
            try
            {
                if (IntPtr.Zero == m_hCam)
                    return true;
 
                // 停止拉流 
                // Stop grabbing 
                MVAPI.MVSTATUS_CODES res = MVSTATUS_CODES.MVST_SUCCESS;
                res = MVGigE.MVStopGrab(m_hCam);
                if (res != MVAPI.MVSTATUS_CODES.MVST_SUCCESS)
                    Debug.WriteLine($"MVStopGrab failed! ErrorCode[{res}]");
 
                isGrabbing = false;
                CallBackImg = null;
                return true;
            }
            catch { return false; }
        }
 
        public override bool GetImage(out Bitmap bitmap, int outtime = 3000)
        {
            bitmap = null;
 
            try
            {
                // 设置超时时间
                DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
                // 判断是否超时
                while (lastTime > DateTime.Now)// 设置超时时间为 3 秒
                {
                    try
                    {
                        if (CallBackImg != null)
                        {
                            // 保存旧 Bitmap 并释放
                            bitmap = CallBackImg.Clone() as Bitmap; // 创建副本
 
                            // 释放旧资源
                            CallBackImg.Dispose();
                            CallBackImg = null;
                            return true;
                        }
                    }
                    catch { }
                    Thread.Sleep(10);
                }
                return false;
            }
            catch { return bitmap == null ? false : true; }
        }
 
        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        {
            if (!isGrabbing)
            {
                StartGrabbing();
            }
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
            if (triggerMode != TriggerMode.On && triggerSource != TriggerSource.Software)
            {
                SetTriggerMode(TriggerMode.On, TriggerSource.Software);
            }
            bitmap = null;
            CallBackImg = null;
            if (!SoftTrigger())
            {
                return false;
            }
            // 开始时间
            DateTime startTime = DateTime.Now; // 当前时间
            // 判断是否超时
            while (DateTime.Now < startTime.AddMilliseconds(outtime))// 设置超时时间为 3 秒
            {
                GetImage(out bitmap, 50);
                if (bitmap != null)
                {
                    break;
                }
                Thread.Sleep(10);
            }
            if (triggerMode != TriggerMode.On)
            {
                SetTriggerMode(TriggerMode.On, triggerSource);
            }
            return (bitmap != null);
        }
 
 
        public override void SetCamConfig(CameraConfig config)
        {
            if (Enum.TryParse(config.Params.Inputs["触发模式"].ToString(), out TriggerMode TriggerMode)
                && Enum.TryParse(config.Params.Inputs["触发方式"].ToString(), out TriggerSource TriggerSource)
                && Enum.TryParse(config.Params.Inputs["触发极性"].ToString(), out TriggerPolarity TriggerPolarity)
                )
            {
                SetTriggerMode(TriggerMode, TriggerSource);
                SetTriggerPolarity(TriggerPolarity);
                //SetTriggerFliter(Convert.ToDouble(config.Params.Inputs["触发消抖"].ToString()));
                //SetTriggerDelay(Convert.ToDouble(config.Params.Inputs["触发延时"].ToString()));
                SetExpouseTime(Convert.ToDouble(config.Params.Inputs["曝光时间"].ToString()));
                SetGain(Convert.ToDouble(config.Params.Inputs["增益"].ToString()));
            }
        }
 
        public override void GetCamConfig(out CameraConfig config)
        {
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
            GetTriggerPolarity(out TriggerPolarity triggerPolarity);
            //GetTriggerFliter(out double triggerfilter);
            //GetTriggerDelay(out double triggerdelay);
            GetExpouseTime(out double expouseTime);
            GetGain(out double gain);
 
            config = new CameraConfig(null);
            config.Params.Inputs.Add("触发模式", triggerMode);
            config.Params.Inputs.Add("触发方式", triggerSource);
            config.Params.Inputs.Add("触发极性", triggerPolarity);
            //config.Params.Inputs.Add("触发消抖", triggerfilter);
            //config.Params.Inputs.Add("触发延时", triggerdelay);
            config.Params.Inputs.Add("曝光时间", expouseTime);
            config.Params.Inputs.Add("增益", gain);
        }
 
        public override bool StartWith_SoftTriggerModel()
        {
            SetTriggerMode(TriggerMode.Off, TriggerSource.Software);
            return StartGrabbing();
        }
 
        public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
        {
            if (hardtriggeritem == TriggerSource.Software) hardtriggeritem = TriggerSource.Line0;
            SetTriggerMode(TriggerMode.On, hardtriggeritem);
            return StartGrabbing();
        }
 
        public override bool StartContinuousGrab()
        {
            throw new NotImplementedException();
        }
    }
}