C3032
2025-12-19 6bb231b53dcb5e21585d54e26ec872e3026fc8fb
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
using MVSDK_Net;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
 
namespace LB_VisionProcesses.Cameras
{
    /// <summary>
    /// 华睿2D工业相机实现类
    /// 基于MVSDK_Net SDK封装
    /// </summary>
    public class HRCamera : BaseCamera
    {
        #region 私有字段
        
        private MyCamera _camera; // 华睿相机设备对象
        private Thread _grabThread; // 图像采集线程
        private bool _isGrabbing; // 采集状态标志
        private bool _threadRunning; // 线程运行标志
        private Thread _callbackThread; // 回调处理线程
        private List<IMVDefine.IMV_Frame> _frameList; // 图像缓存列表
        private readonly object _frameLock = new object(); // 帧缓存锁
        
        // CopyMemory API声明
        [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
        private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
        
        #endregion
 
        #region 构造函数和析构函数
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public HRCamera()
        {
            _camera = new MyCamera();
            _frameList = new List<IMVDefine.IMV_Frame>();
            Brand = CameraBrand.HRCamera;
            _isGrabbing = false;
            _threadRunning = false;
        }
        
        /// <summary>
        /// 析构函数
        /// </summary>
        ~HRCamera()
        {
            Dispose(false);
        }
        
        #endregion
 
        #region 设备管理操作
        
        /// <summary>
        /// 获取相机SN枚举列表
        /// </summary>
        /// <returns>相机SN列表</returns>
        public override List<string> GetListEnum()
        {
            List<string> cameraList = new List<string>();
            
            try
            {
                // 枚举所有设备
                IMVDefine.IMV_DeviceList deviceList = new IMVDefine.IMV_DeviceList();
                IMVDefine.IMV_EInterfaceType interfaceType = IMVDefine.IMV_EInterfaceType.interfaceTypeAll;
                
                int result = MyCamera.IMV_EnumDevices(ref deviceList, (uint)interfaceType);
                
                if (result == IMVDefine.IMV_OK && deviceList.nDevNum > 0)
                {
                    for (int i = 0; i < deviceList.nDevNum; i++)
                    {
                        IMVDefine.IMV_DeviceInfo deviceInfo = (IMVDefine.IMV_DeviceInfo)
                            Marshal.PtrToStructure(
                                deviceList.pDevInfo + Marshal.SizeOf(typeof(IMVDefine.IMV_DeviceInfo)) * i,
                                typeof(IMVDefine.IMV_DeviceInfo));
                        
                        string cameraInfo = $"{deviceInfo.cameraName}[{deviceInfo.serialNumber}]";
                        cameraList.Add(cameraInfo); 
                    }
                }
                else
                {
                    // 记录日志或抛出异常
                    throw new Exception($"枚举设备失败,错误码:{result}");
                }
            }
            catch (Exception ex)
            { 
                // 记录错误日志
                System.Diagnostics.Debug.WriteLine($"获取相机列表失败:{ex.Message}");
                throw;
            }
            
            return cameraList;
        }
        
        /// <summary>
        /// 初始化相机设备
        /// </summary>
        /// <param name="SN">相机序列号</param>
        /// <param name="Handle">窗口句柄(可选)</param>
        /// <returns>初始化是否成功</returns>
        public override bool InitDevice(string SN, object Handle = null)
        {
            try
            {
                // 如果相机已打开,先关闭
                if (_camera != null && _camera.IMV_IsOpen())
                {
                    CloseDevice();
                }
                
                // 枚举设备并匹配SN
                List<string> cameraList = GetListEnum();
                int cameraIndex = -1;
                
                for (int i = 0; i < cameraList.Count; i++)
                {
                    if (cameraList[i].Contains(SN))
                    {
                        cameraIndex = i;
                        break;
                    }
                }
                
                if (cameraIndex == -1)
                {
                    throw new Exception($"未找到序列号为 {SN} 的相机");
                }
                
                // 创建设备句柄
                int result = _camera.IMV_CreateHandle(IMVDefine.IMV_ECreateHandleMode.modeByIndex, cameraIndex);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"创建设备句柄失败,错误码:{result}");
                }
                
                // 打开设备
                result = _camera.IMV_Open();
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"打开相机失败,错误码:{result}");
                }
                
                // 设置设备属性
                SN = SN;
                isGrabbing = false;
                
                // 设置缓存个数为8
                _camera.IMV_SetBufferCount(8);
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"初始化相机失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 关闭相机设备
        /// </summary>
        /// <returns>关闭是否成功</returns>
        public override bool CloseDevice()
        {
            try
            {
                if (_camera == null)
                    return true;
                
                // 停止采集
                if (_camera.IMV_IsGrabbing())
                {
                    StopGrabbing();
                }
                
                // 关闭设备
                if (_camera.IMV_IsOpen())
                {
                    int result = _camera.IMV_Close();
                    if (result != IMVDefine.IMV_OK)
                    {
                        System.Diagnostics.Debug.WriteLine($"关闭相机失败,错误码:{result}");
                    }
                }
                
                // 释放资源
                _isGrabbing = false;
                isGrabbing = false;
                
                // 清理帧缓存
                lock (_frameLock)
                {
                    foreach (var frame in _frameList)
                    {
                        var tempFrame = frame; // 创建临时变量
                        _camera.IMV_ReleaseFrame(ref tempFrame);
                    }
                    _frameList.Clear();
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"关闭相机失败:{ex.Message}");
                return false;
            }
        }
        
        #endregion
 
        #region 图像采集操作
        
        /// <summary>
        /// 开始图像采集
        /// </summary>
        /// <returns>采集启动是否成功</returns>
        public override bool StartGrabbing()
        {
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                // 停止现有采集
                if (_isGrabbing)
                {
                    StopGrabbing();
                }
                
                // 启动采集
                int result = _camera.IMV_StartGrabbing();
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"启动采集失败,错误码:{result}");
                }
                
                _isGrabbing = true;
                isGrabbing = true;
                
                // 启动采集线程
                _threadRunning = true;
                _grabThread = new Thread(GrabThreadProc);
                _grabThread.IsBackground = true;
                _grabThread.Start();
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"启动采集失败:{ex.Message}");
                _isGrabbing = false;
                isGrabbing = false;
                return false;
            }
        }
        
        /// <summary>
        /// 停止图像采集
        /// </summary>
        /// <returns>停止是否成功</returns>
        public override bool StopGrabbing()
        {
            try
            {
                if (_camera == null || !_isGrabbing)
                    return true;
                
                // 停止线程
                _threadRunning = false;
                if (_grabThread != null && _grabThread.IsAlive)
                {
                    _grabThread.Join(1000);
                    _grabThread = null;
                }
                
                // 停止采集
                int result = _camera.IMV_StopGrabbing();
                if (result != IMVDefine.IMV_OK)
                {
                    System.Diagnostics.Debug.WriteLine($"停止采集失败,错误码:{result}");
                }
                
                _isGrabbing = false;
                isGrabbing = false;
                
                // 清理缓存
                lock (_frameLock)
                {
                    foreach (var frame in _frameList)
                    {
                        var tempFrame = frame; // 创建临时变量
                        _camera.IMV_ReleaseFrame(ref tempFrame);
                    }
                    _frameList.Clear();
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"停止采集失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 执行软触发
        /// </summary>
        /// <returns>触发是否成功</returns>
        public override bool SoftTrigger()
        {
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                int result = _camera.IMV_ExecuteCommandFeature("TriggerSoftware");
                return result == IMVDefine.IMV_OK;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"软触发失败:{ex.Message}");
                return false;
            }
        }
        
        #endregion
 
        #region 参数设置和获取
        
        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="mode">触发模式</param>
        /// <param name="source">触发源</param>
        /// <returns>设置是否成功</returns>
        public override bool SetTriggerMode(TriggerMode mode, TriggerSource source = TriggerSource.Line0)
        {
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                // 设置触发模式
                string triggerMode = mode == TriggerMode.On ? "On" : "Off";
                int result = _camera.IMV_SetEnumFeatureSymbol("TriggerMode", triggerMode);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置触发模式失败,错误码:{result}");
                }
                
                // 设置触发源
                if (mode == TriggerMode.On)
                {
                    string triggerSource = GetTriggerSourceString(source);
                    result = _camera.IMV_SetEnumFeatureSymbol("TriggerSource", triggerSource);
                    if (result != IMVDefine.IMV_OK)
                    {
                        throw new Exception($"设置触发源失败,错误码:{result}");
                    }
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置触发模式失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 获取触发模式
        /// </summary>
        /// <param name="mode">触发模式</param>
        /// <param name="source">触发源</param>
        /// <returns>获取是否成功</returns>
        public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
        {
            mode = TriggerMode.Off;
            source = TriggerSource.Software;
            
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                // 获取触发模式
                IMVDefine.IMV_String triggerMode = new IMVDefine.IMV_String();
                int result = _camera.IMV_GetEnumFeatureSymbol("TriggerMode", ref triggerMode);
                if (result == IMVDefine.IMV_OK)
                {
                    mode = triggerMode.str == "On" ? TriggerMode.On : TriggerMode.Off;
                }
                
                // 获取触发源
                IMVDefine.IMV_String triggerSource = new IMVDefine.IMV_String();
                result = _camera.IMV_GetEnumFeatureSymbol("TriggerSource", ref triggerSource);
                if (result == IMVDefine.IMV_OK)
                {
                    source = GetTriggerSourceFromString(triggerSource.str);
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取触发模式失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 设置曝光时间
        /// </summary>
        /// <param name="value">曝光时间(微秒)</param>
        /// <returns>设置是否成功</returns>
        public override bool SetExpouseTime(double value)
        {
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                // 验证曝光时间范围
                double minExp = 0, maxExp = 0;
                int result = _camera.IMV_GetDoubleFeatureMin("ExposureTime", ref minExp);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取曝光时间最小值失败,错误码:{result}");
                }
                
                result = _camera.IMV_GetDoubleFeatureMax("ExposureTime", ref maxExp);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取曝光时间最大值失败,错误码:{result}");
                }
                
                if (value < minExp || value > maxExp)
                {
                    throw new Exception($"曝光时间超出范围,有效范围:{minExp} - {maxExp}");
                }
                
                // 设置曝光时间
                result = _camera.IMV_SetDoubleFeatureValue("ExposureTime", value);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置曝光时间失败,错误码:{result}");
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置曝光时间失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 获取曝光时间
        /// </summary>
        /// <param name="value">曝光时间</param>
        /// <returns>获取是否成功</returns>
        public override bool GetExpouseTime(out double value)
        {
            value = 0;
            
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                int result = _camera.IMV_GetDoubleFeatureValue("ExposureTime", ref value);
                return result == IMVDefine.IMV_OK;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取曝光时间失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 设置增益
        /// </summary>
        /// <param name="gain">增益值</param>
        /// <returns>设置是否成功</returns>
        public override bool SetGain(double gain)
        {
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                // 验证增益范围
                double minGain = 0, maxGain = 0;
                int result = _camera.IMV_GetDoubleFeatureMin("GainRaw", ref minGain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取增益最小值失败,错误码:{result}");
                }
                
                result = _camera.IMV_GetDoubleFeatureMax("GainRaw", ref maxGain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取增益最大值失败,错误码:{result}");
                }
                
                if (gain < minGain || gain > maxGain)
                {
                    throw new Exception($"增益值超出范围,有效范围:{minGain} - {maxGain}");
                }
                
                // 设置增益
                result = _camera.IMV_SetDoubleFeatureValue("GainRaw", gain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置增益失败,错误码:{result}");
                }
                
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置增益失败:{ex.Message}");
                return false;
            }
        }
        
        /// <summary>
        /// 获取增益值
        /// </summary>
        /// <param name="gain">增益值</param>
        /// <returns>获取是否成功</returns>
        public override bool GetGain(out double gain)
        {
            gain = 0;
            
            try
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                }
                
                int result = _camera.IMV_GetDoubleFeatureValue("GainRaw", ref gain);
                return result == IMVDefine.IMV_OK;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取增益失败:{ex.Message}");
                return false;
            }
        }
        
        // 其他参数设置方法类似,这里省略部分实现...
        
        #endregion
 
        #region 辅助方法
        
        /// <summary>
        /// 图像采集线程处理函数
        /// </summary>
        private void GrabThreadProc()
        {
            IMVDefine.IMV_Frame frame = new IMVDefine.IMV_Frame();
            
            while (_threadRunning)
            {
                try
                {
                    // 获取图像帧
                    int result = _camera.IMV_GetFrame(ref frame, 100);
                    
                    if (result == IMVDefine.IMV_OK)
                    {
                        // 处理图像帧
                        ProcessFrame(frame);
                    }
                    else if (result != 0x80000001) // 超时错误代码(常见值)
                    {
                        // 非超时错误
                        System.Diagnostics.Debug.WriteLine($"获取图像帧失败,错误码:{result}");
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"采集线程异常:{ex.Message}");
                }
                
                Thread.Sleep(1);
            }
        }
        
        /// <summary>
        /// 处理图像帧
        /// </summary>
        /// <param name="frame">图像帧</param>
        private void ProcessFrame(IMVDefine.IMV_Frame frame)
        {
            try
            {
                // 将图像数据转换为Bitmap
                Bitmap bitmap = ConvertFrameToBitmap(frame);
                
                if (bitmap != null)
                {
                    // 触发图像采集事件
                    CameraEventArgs args = new CameraEventArgs(SN, bitmap);
                    ImageGrabbed?.Invoke(this, args);
                    
                    // 更新回调图像
                    CallBackImg = bitmap;
                }
                
                // 释放帧
                _camera.IMV_ReleaseFrame(ref frame);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理图像帧失败:{ex.Message}");
            }
        }
        
        /// <summary>
        /// 将图像帧转换为Bitmap
        /// </summary>
        /// <param name="frame">图像帧</param>
        /// <returns>Bitmap对象</returns>
        private Bitmap ConvertFrameToBitmap(IMVDefine.IMV_Frame frame)
        {
            try
            {
                Bitmap bitmap = null;
                
                switch (frame.frameInfo.pixelFormat)
                {
                    case IMVDefine.IMV_EPixelType.gvspPixelMono8:
                        bitmap = CreateMono8Bitmap(frame);
                        break;
                    case IMVDefine.IMV_EPixelType.gvspPixelBGR8:
                        bitmap = CreateBgr8Bitmap(frame);
                        break;
                    default:
                        // 其他格式需要转换
                        bitmap = ConvertToBGR8(frame);
                        break;
                }
                
                return bitmap;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"图像格式转换失败:{ex.Message}");
                return null;
            }
        }
        
        /// <summary>
        /// 创建Mono8格式Bitmap
        /// </summary>
        private Bitmap CreateMono8Bitmap(IMVDefine.IMV_Frame frame)
        {
            Bitmap bitmap = new Bitmap((int)frame.frameInfo.width, (int)frame.frameInfo.height, PixelFormat.Format8bppIndexed);
            
            // 设置灰度调色板
            ColorPalette palette = bitmap.Palette;
            for (int i = 0; i < 256; i++)
            {
                palette.Entries[i] = Color.FromArgb(i, i, i);
            }
            bitmap.Palette = palette;
            
            // 复制图像数据
            BitmapData bmpData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.WriteOnly,
                bitmap.PixelFormat);
            
            // 使用CopyMemory API进行内存复制
            CopyMemory(bmpData.Scan0, frame.pData, (uint)Math.Min(frame.frameInfo.size, (uint)(bmpData.Stride * bitmap.Height)));
            
            bitmap.UnlockBits(bmpData);
            return bitmap;
        }
        
        /// <summary>
        /// 创建BGR8格式Bitmap
        /// </summary>
        private Bitmap CreateBgr8Bitmap(IMVDefine.IMV_Frame frame)
        {
            Bitmap bitmap = new Bitmap((int)frame.frameInfo.width, (int)frame.frameInfo.height, PixelFormat.Format24bppRgb);
            
            BitmapData bmpData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.WriteOnly,
                bitmap.PixelFormat);
            
            // 使用CopyMemory API进行内存复制
            CopyMemory(bmpData.Scan0, frame.pData, (uint)Math.Min(frame.frameInfo.size, (uint)(bmpData.Stride * bitmap.Height)));
            
            bitmap.UnlockBits(bmpData);
            return bitmap;
        }
        
        /// <summary>
        /// 转换为BGR8格式
        /// </summary>
        private Bitmap ConvertToBGR8(IMVDefine.IMV_Frame frame)
        {
            IMVDefine.IMV_PixelConvertParam convertParam = new IMVDefine.IMV_PixelConvertParam
            {
                nWidth = frame.frameInfo.width,
                nHeight = frame.frameInfo.height,
                ePixelFormat = frame.frameInfo.pixelFormat,
                pSrcData = frame.pData,
                nSrcDataLen = frame.frameInfo.size,
                nPaddingX = frame.frameInfo.paddingX,
                nPaddingY = frame.frameInfo.paddingY,
                eBayerDemosaic = IMVDefine.IMV_EBayerDemosaic.demosaicBilinear,
                eDstPixelFormat = IMVDefine.IMV_EPixelType.gvspPixelBGR8,
                nDstBufSize = frame.frameInfo.width * frame.frameInfo.height * 3
            };
            
            IntPtr dstBuffer = Marshal.AllocHGlobal((int)convertParam.nDstBufSize);
            convertParam.pDstBuf = dstBuffer;
            
            try
            {
                int result = _camera.IMV_PixelConvert(ref convertParam);
                if (result == IMVDefine.IMV_OK)
                {
                    return CreateBgr8BitmapFromBuffer(dstBuffer, frame.frameInfo.width, frame.frameInfo.height);
                }
                return null;
            }
            finally
            {
                Marshal.FreeHGlobal(dstBuffer);
            }
        }
        
        /// <summary>
        /// 从缓冲区创建BGR8 Bitmap
        /// </summary>
        private Bitmap CreateBgr8BitmapFromBuffer(IntPtr buffer, uint width, uint height)
        {
            Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format24bppRgb);
            
            BitmapData bmpData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.WriteOnly,
                bitmap.PixelFormat);
            
            // 使用CopyMemory API进行安全的内存复制
            CopyMemory(bmpData.Scan0, buffer, (uint)(width * height * 3));
            
            bitmap.UnlockBits(bmpData);
            return bitmap;
        }
        
        /// <summary>
        /// 将触发源枚举转换为字符串
        /// </summary>
        private string GetTriggerSourceString(TriggerSource source)
        {
            return source switch
            {
                TriggerSource.Software => "Software",
                TriggerSource.Line0 => "Line0",
                TriggerSource.Line1 => "Line1",
                TriggerSource.Line2 => "Line2",
                TriggerSource.Line3 => "Line3",
                TriggerSource.Line4 => "Line4",
                TriggerSource.Line5 => "Line5",
                _ => "Software"
            };
        }
        
        /// <summary>
        /// 将字符串转换为触发源枚举
        /// </summary>
        private TriggerSource GetTriggerSourceFromString(string source)
        {
            return source switch
            {
                "Software" => TriggerSource.Software,
                "Line0" => TriggerSource.Line0,
                "Line1" => TriggerSource.Line1,
                "Line2" => TriggerSource.Line2,
                "Line3" => TriggerSource.Line3,
                "Line4" => TriggerSource.Line4,
                "Line5" => TriggerSource.Line5,
                _ => TriggerSource.Software
            };
        }
        
        #endregion
 
        #region 未实现的抽象方法(简化版实现)
        
        public override bool SetTriggerPolarity(TriggerPolarity polarity)
        {
            // 华睿相机可能不支持该功能,返回成功
            return true;
        }
        
        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
        {
            polarity = TriggerPolarity.RisingEdge;
            return true;
        }
        
        public override bool SetTriggerFliter(double flitertime)
        {
            // 暂不支持
            return true;
        }
        
        public override bool GetTriggerFliter(out double flitertime)
        {
            flitertime = 0;
            return true;
        }
        
        public override bool SetTriggerDelay(double delay)
        {
            // 暂不支持
            return true;
        }
        
        public override bool GetTriggerDelay(out double delay)
        {
            delay = 0;
            return true;
        }
        
        public override bool SetLineMode(IOLines line, LineMode mode)
        {
            // 暂不支持
            return true;
        }
        
        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
        {
            // 暂不支持
            return true;
        }
        
        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
        {
            lineStatus = LineStatus.Low;
            return true;
        }
        
        public override bool AutoBalanceWhite()
        {
            // 暂不支持自动白平衡
            return true;
        }
        
        #endregion
 
        #region IDisposable实现
        
        private bool _disposed = false;
        
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // 释放托管资源
                }
                
                // 释放非托管资源
                CloseDevice();
                
                _disposed = true;
            }
        }
        
        public override void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        
        #endregion
    }
}