轮胎外观检测添加思谋语义分割模型检测工具
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
using LB_SmartVisionCameraDevice.PHM6000;
using LB_SmartVisionCameraSDK.PHM6000;
using LB_SmartVisionCommon;
using LB_VisionProcesses.Cameras;
using OpenVinoSharp.Extensions.model;
using SharpCompress.Common;
using Sunny.UI.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace LB_VisionProcesses.Cameras.LBCameras
{
    public class LBCameraEventArgs : CameraEventArgs
    {
        public bool IsComplete { get; set; }
        public LBCameraEventArgs(string sn, Bitmap bitmap, bool isComplete) : base(sn, bitmap)
        {
            IsComplete = isComplete;
        }
    }
 
    /// <summary>
    /// LB3D工业相机实现类
    /// 基于PHM6000系列封装
    /// </summary>
    public class LBCamera : BaseCamera
    {
        private IntPtr _cameraHandle = IntPtr.Zero;
        private PHM6000SensorConfig _sensorConfig;
        // 采集回调
        private AcquisitionCallbackZA _acquisitionCallback;
        private AcquisitionCompletedCallback _acquisitionCompletedCallback;
 
        public bool _isConnected = false;
        private int _frameCount = 0; // 采集帧计数
 
        // 图像缓冲
        private byte[] _rawPixelBuffer = null; // 用于存储整张图的像素数据 (8bpp)
        private int _currentBitmapHeight = 0;
        private int _currentBitmapWidth = 0;
        private int _currentLineCount = 0;
        private object _bufferLock = new object();
        private bool _isBufferReady = false;
 
        // 临时行缓冲,用于接收回调数据
        private byte[] _tempLineBuffer = null;
        private bool _isContinuous = false;
        // 新增:CollectedImages操作锁,保证线程安全
        private readonly object _collectedImagesLock = new object();
 
        public LBCamera()
        {
            Brand = CameraBrand.LBCamera;
            _sensorConfig = new PHM6000SensorConfig();
        }
 
        #region ICamera Implementation
 
        public override bool InitDevice(string sn, object handle = null)
        {
            // 如果已连接,直接返回true
            if (_isConnected && _cameraHandle != IntPtr.Zero)
            {
                return true;
            }
 
            IntPtr tempHandle = IntPtr.Zero;
            try
            {
                SN = sn;
                // 1. 创建临时句柄用于发现设备
                tempHandle = PHM6000Profiler.CreateCameraEntry();
                if (tempHandle == IntPtr.Zero) return false;
 
                // 2. 发现相机
                int cameraCount = PHM6000Profiler.DiscoverCameras(tempHandle);
                if (cameraCount <= 0)
                {
                    PHM6000Profiler.DestroyCameraEntry(tempHandle);
                    return false;
                }
 
                string targetIp = string.Empty;
                int targetPort = 0;
                bool found = false;
 
                // 3. 遍历相机寻找匹配的SN
                for (int i = 0; i < cameraCount; i++)
                {
                    byte[] moduleTypeBytes = new byte[64];
                    byte[] serialNumberBytes = new byte[64];
 
                    if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
                    {
                        string currentSn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
 
                        // 匹配SN或IP
                        if (currentSn == sn || sn.Contains(currentSn))
                        {
                            byte[] addressBytes = new byte[64];
                            int port = 0;
                            if (PHM6000Profiler.GetCameraAddress(tempHandle, i, addressBytes, ref port) == 0)
                            {
                                targetIp = Encoding.UTF8.GetString(addressBytes).TrimEnd('\0');
                                targetPort = port;
                                found = true;
                                break;
                            }
                        }
                    }
                }
 
                PHM6000Profiler.DestroyCameraEntry(tempHandle);
                tempHandle = IntPtr.Zero;
 
                if (!found)
                {
                    if (System.Net.IPAddress.TryParse(sn, out _))
                    {
                        targetIp = sn;
                        targetPort = 5577;
                    }
                    else
                    {
                        AsyncLogHelper.Error($"LBCamera: 未找到SN为 {sn} 的相机");
                        return false;
                    }
                }
 
                // 4. 创建正式相机句柄并连接
                _cameraHandle = PHM6000Profiler.CreateCameraEntry();
                if (_cameraHandle == IntPtr.Zero) return false;
 
                var addr = Encoding.ASCII.GetBytes(targetIp);
                int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
 
                if (result == 0)
                {
                    _isConnected = true;
                    // 加载相机当前参数到 _sensorConfig
                    SyncConfigFromCamera();
 
                    // 初始化并注册采集回调 (获取数据用)
                    _acquisitionCallback = new AcquisitionCallbackZA(OnAcquisitionCallbackZA);
                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
 
                    // 初始化并注册采集完成回调 (状态通知用)
                    _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
 
                    // 强制应用当前配置(确保触发模式等参数正确,避免相机处于未知状态)
                    UpdateSensorConfig(_sensorConfig);
 
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Connected and initialized successfully (Manual Data Mode)");
 
                    return true;
                }
                else
                {
                    AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
                }
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: InitDevice异常 - {ex.Message}");
                if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle);
            }
            return false;
        }
 
        public override bool CloseDevice()
        {
            if (_isConnected && _cameraHandle != IntPtr.Zero)
            {
                StopGrabbing();
                PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
                _cameraHandle = IntPtr.Zero;
                _isConnected = false;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Closed");
            }
            return true;
        }
 
        public override List<string> GetListEnum()
        {
            List<string> cameraList = new List<string>();
            IntPtr tempHandle = IntPtr.Zero;
            try
            {
                tempHandle = PHM6000Profiler.CreateCameraEntry();
                if (tempHandle != IntPtr.Zero)
                {
                    int count = PHM6000Profiler.DiscoverCameras(tempHandle);
                    for (int i = 0; i < count; i++)
                    {
                        byte[] moduleTypeBytes = new byte[64];
                        byte[] serialNumberBytes = new byte[64];
                        if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
                        {
                            string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
                            if (!string.IsNullOrEmpty(sn) && sn.Contains("L"))
                            {
                                cameraList.Add(sn);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: 获取设备列表异常 - {ex.Message}");
            }
            finally
            {
                if (tempHandle != IntPtr.Zero)
                    PHM6000Profiler.DestroyCameraEntry(tempHandle);
            }
            return cameraList;
        }
 
        private void InitBuffer()
        {
            lock (_bufferLock)
            {
                _currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
                // 宽度在第一行数据到达时确定
                _currentBitmapWidth = 0;
                _rawPixelBuffer = null;
                _currentLineCount = 0;
                _isBufferReady = false;
            }
        }
 
        public override bool StartGrabbing()
        {
            // 默认连续模式
            return StartSingleGrab();
        }
 
        public bool StartSingleGrab()
        {
            if (!_isConnected) return false;
 
            _isContinuous = false;
            InitBuffer();
            AsyncLogHelper.Info($"LBCamera[{SN}]: 开始单次采集");
 
            // 1=扫描模式, 0=单次
            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 0);
            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
 
            if (result == 0)
            {
                isGrabbing = true;
                return true;
            }
            return false;
        }
 
        public override bool StartContinuousGrab()
        {
            if (!_isConnected) return false;
 
            _isContinuous = true;
            InitBuffer();
            AsyncLogHelper.Info($"LBCamera[{SN}]:开始连续采集");
 
            // 1=扫描模式, 1=连续
            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 1);
            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
 
            if (result == 0)
            {
                isGrabbing = true;
                return true;
            }
            return false;
        }
 
        public override bool StopGrabbing()
        {
            _isContinuous = false;
            if (!_isConnected) return true;
            PHM6000Profiler.StopAcquisition(_cameraHandle);
 
            // 停止时如果有未显示的缓存数据,将其显示出来(支持显示不完整的帧)
            lock (_bufferLock)
            {
                if (_currentLineCount > 0)
                {
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Flushing partial buffer ({_currentLineCount} lines) on stop");
                    CreateAndFireBitmap();
                    _currentLineCount = 0;
                }
            }
 
            isGrabbing = false;
            return true;
        }
 
        public override bool StartWith_SoftTriggerModel() => StartContinuousGrab();
 
        public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0) => StartSingleGrab();
 
        public override bool SoftTrigger() => true;
 
        #region 参数设置映射
 
        public override bool SetExpouseTime(double value) => SetParam(EnumNameId.ExposureTime, (float)value);
        public override bool GetExpouseTime(out double value) { float v; bool r = GetParam(EnumNameId.ExposureTime, out v); value = v; return r; }
        public override bool SetGain(double gain) => SetParam(EnumNameId.AnalogGain, (int)gain);
        public override bool GetGain(out double gain) { int v; bool r = GetParam(EnumNameId.AnalogGain, out v); gain = v; return r; }
 
        public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0)
        {
            if (!_isConnected) return false;
 
            if (triggerEnum == TriggerSource.Software)
            {
                _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.固定频率;
                _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.软触发;
            }
            else
            {
                _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.编码器;
                _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.外部触发;
            }
            UpdateSensorConfig(_sensorConfig);
            return true;
        }
 
        public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
        {
            mode = TriggerMode.On;
            source = _sensorConfig.DataAcquisitionTriggerSource == EnumDataAcquisitionTriggerSource.软触发 ? TriggerSource.Software : TriggerSource.Line0;
            return true;
        }
        public override bool SetTriggerPolarity(TriggerPolarity polarity) => true;
        public override bool GetTriggerPolarity(out TriggerPolarity polarity) { polarity = TriggerPolarity.RisingEdge; return true; }
        public override bool SetTriggerFliter(double flitertime) => true;
        public override bool GetTriggerFliter(out double flitertime) { flitertime = 0; return true; }
        public override bool SetTriggerDelay(double delay) => true;
        public override bool GetTriggerDelay(out double delay) { delay = 0; return true; }
        public override bool SetLineMode(IOLines line, LineMode mode) => true;
        public override bool SetLineStatus(IOLines line, LineStatus linestatus) => true;
        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus) { lineStatus = LineStatus.Low; return true; }
        public override bool AutoBalanceWhite() => true;
        public override void SetCamConfig(CameraConfig config) { }
        public override void GetCamConfig(out CameraConfig config) { config = new CameraConfig(null); }
        public override bool GetImage(out Bitmap bitmap, int outtime = 3000) { bitmap = null; return false; }
        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        {
            // 简单实现:软触发等待
            bitmap = null;
            if (!_isConnected) return false;
 
            // 计算理论最小耗时 (仅当使用固定频率触发时)
            int minTime = 0;
            if (_sensorConfig.LineScanTriggerSource == EnumLineScanTriggerSource.固定频率)
            {
                float rate = _sensorConfig.SoftwareTriggerRate > 0 ? _sensorConfig.SoftwareTriggerRate : 1000f;
                int lines = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
                minTime = (int)((lines / rate) * 1000);
            }
 
            // 如果传入超时时间不够,自动延长
            int actualTimeout = outtime;
            if (actualTimeout < minTime + 2000)
            {
                actualTimeout = minTime + 3000; // 预留3秒余量
                AsyncLogHelper.Warn($"LBCamera: Provided timeout {outtime}ms is too short for {minTime}ms scan. Extended to {actualTimeout}ms.");
            }
 
            using (AutoResetEvent waitHandle = new AutoResetEvent(false))
            {
                Bitmap res = null;
                EventHandler<CameraEventArgs> handler = (s, e) =>
                {
                    if (e.Bitmap != null)
                    {
                        res = e.Bitmap.Clone() as Bitmap;
                        waitHandle.Set();
                    }
                };
                ImageGrabbed += handler;
 
                if (StartSingleGrab())
                {
                    if (!waitHandle.WaitOne(actualTimeout))
                    {
                        AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger timeout after {actualTimeout}ms");
                    }
                }
                else
                {
                    AsyncLogHelper.Error("LBCamera: StartSingleGrab failed");
                }
 
                ImageGrabbed -= handler;
 
                // 确保停止采集
                StopGrabbing();
 
                bitmap = res;
                return bitmap != null;
            }
        }
 
        public PHM6000SensorConfig GetSensorConfig()
        {
            SyncConfigFromCamera();
            return _sensorConfig;
        }
 
        public void UpdateSensorConfig(PHM6000SensorConfig config)
        {
            //_sensorConfig = config;
            //if (!_isConnected) return;
            //SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
            //SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
            //if (config.LineScanTriggerSource == EnumLineScanTriggerSource.固定频率)
            //{
            //    PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
            //}
 
            int result = 0;
            var type = config.GetType();
            var propLineScan = type.GetProperty(nameof(config.LineScanTriggerSource));
            var val = (EnumLineScanTriggerSource)propLineScan.GetValue(config);
            var props = config.GetType().GetProperties();
 
            //排除Y轴
            props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.YResolution)).ToArray();
            //排除不需要的项
            if (val == EnumLineScanTriggerSource.固定频率)
            {
                props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.EncoderTriggerDirection) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerInterval) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerSignalCountingMode)).ToArray();
            }
            else
            {
                props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.SoftwareTriggerRate)).ToArray();
            }
            foreach (var p in props)
            {
                //跳过自定义参数
                var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
                if (iscustomAttr != null) continue;
                //判断是6030传感器还是普通传感器
                if (SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGain))
                {
                    continue;
                }
                if (!SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGainFor6030))
                {
                    continue;
                }
                var id = Convert.ToInt32(Enum.Parse(typeof(EnumNameId), p.Name));
                if (p.PropertyType == typeof(int))
                {
                    var value = Convert.ToInt32(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, value, 0, 0);
                }
                else if (p.PropertyType == typeof(float))
                {
                    var value = Convert.ToDouble(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, value, 0);
                }
                else
                {
                    var value = Convert.ToInt32(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, 0, value);
                }
                if (result == -1)
                {
                    var disattr = p.GetCustomAttribute<DisplayNameAttribute>();
                    var name = disattr?.DisplayName ?? p.Name;
                    throw new Exception($"设置参数{name}时不成功!");
                }
            }
            var finalResult = PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
            if (finalResult != 0)
            {
            }
            PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
        }
 
        #endregion
 
        #endregion
 
        #region Callbacks
 
        private void OnAcquisitionCallbackZA(IntPtr pInstance, IntPtr buffer, int points)
        {
            if (buffer == IntPtr.Zero || points <= 0) return;
 
            lock (_bufferLock)
            {
                // 初始化缓冲区
                if (_rawPixelBuffer == null)
                {
                    _currentBitmapWidth = points;
                    if (_currentBitmapHeight <= 0) _currentBitmapHeight = 2000; // 默认防呆
                    _rawPixelBuffer = new byte[_currentBitmapWidth * _currentBitmapHeight];
                    _currentLineCount = 0;
                }
 
                if (_currentLineCount >= _currentBitmapHeight) return; // 缓冲区满,忽略多余数据
 
                // 准备临时缓冲区接收行数据 (LBPointZA = 8 bytes)
                int lineBytes = points * 8;
                if (_tempLineBuffer == null || _tempLineBuffer.Length != lineBytes)
                {
                    _tempLineBuffer = new byte[lineBytes];
                }
 
                // 拷贝非托管内存到托管数组
                Marshal.Copy(buffer, _tempLineBuffer, 0, lineBytes);
 
                // 提取灰度(Intensity/Alpha)数据填充到 _rawPixelBuffer
                // LBPointZA结构: float(4) + res(3) + alpha(1). Alpha在偏移7
                int bufferOffset = _currentLineCount * _currentBitmapWidth;
 
                for (int i = 0; i < points; i++)
                {
                    if (bufferOffset + i < _rawPixelBuffer.Length)
                    {
                        _rawPixelBuffer[bufferOffset + i] = _tempLineBuffer[i * 8 + 7];
                    }
                }
 
                _currentLineCount++;
 
                // 如果达到预定高度,生成图像
                if (_currentLineCount >= _currentBitmapHeight)
                {
                    CreateAndFireBitmap();
 
                    // 重置,准备下一帧 (如果是连续采集)
                    _currentLineCount = 0;
                    // _rawPixelBuffer 可以复用,不需要置空
                }
            }
        }
 
        private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
        {
            // nOption: 0=Batch End, 1=All End(Single), 2=Processing End
            // 此处主要用于日志或状态监控
            // 实际图像生成在 Data Callback 中完成
            if (nOption == 1) // 单次采集结束
            {
                if (_isContinuous && isGrabbing)
                {
                    // 如果在连续模式下收到结束信号,尝试自动重启采集
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
                    Task.Run(() =>
                    {
                        if (_isContinuous && _isConnected)
                        {
                            PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
                        }
                    });
                }
                else
                {
                    isGrabbing = false;
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Single grab completed by SDK");
 
                    // 单次采集结束时,如果有未显示的缓冲数据,立即生成图像
                    // 防止因数据量不足(小于ScanLineCount)导致GetImageWithSoftTrigger一直等待
                    lock (_bufferLock)
                    {
                        if (_currentLineCount > 0)
                        {
                            AsyncLogHelper.Info($"LBCamera[{SN}]: Flushing partial buffer ({_currentLineCount} lines) on completion");
                            CreateAndFireBitmap();
 
                            _currentLineCount = 0;
                        }
                    }
                }
            }
        }
 
        private void CreateAndFireBitmap()
        {
            Bitmap bmp = null;
            BitmapData bmpData = null;
 
            try
            {
                int width = _currentBitmapWidth;
                int height = _currentLineCount;
 
                // 基础合法性校验
                if (width <= 0 || height <= 0 || _rawPixelBuffer == null || _rawPixelBuffer.Length < width * height)
                {
                    AsyncLogHelper.Warn($"LBCamera[{SN}]: 图像参数无效,跳过生成");
                    return;
                }
 
                // 1. 创建8位灰度位图
                bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
 
                // 设置灰度调色板
                ColorPalette palette = bmp.Palette;
                for (int i = 0; i < 256; i++)
                {
                    palette.Entries[i] = Color.FromArgb(i, i, i);
                }
                bmp.Palette = palette;
 
                // 2. 高效内存拷贝(支持Stride对齐,整行复制)
                bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                // 注意:Bitmap Stride 可能不等于 Width,需要逐行拷贝
                int stride = bmpData.Stride;
                IntPtr ptr = bmpData.Scan0;
 
                for (int y = 0; y < height; y++)
                {
                    // 确保不越界
                    if ((y * width) + width <= _rawPixelBuffer.Length)
                    {
                        Marshal.Copy(_rawPixelBuffer, y * width, ptr + y * stride, width);
                    }
                }
 
                bmp.UnlockBits(bmpData);
                bmpData = null; // 标记已解锁
 
                _frameCount++;
                AsyncLogHelper.Info($"LBCamera[{SN}]: 生成第 {_frameCount} 帧 ({width}x{height})");
 
                // 3. 获取/创建线程安全队列
                var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>());
 
                // 4. 队列限流,防止内存溢出
                if (queue.Count >= MAX_QUEUE_CAPACITY)
                {
                    if (queue.TryDequeue(out Bitmap old))
                    {
                        old.Dispose(); // 丢弃最旧帧,释放内存
                        AsyncLogHelper.Warn($"LBCamera[{SN}]: 队列已满,自动丢弃最旧帧");
                    }
                }
 
                // 5. 入队
                queue.Enqueue(bmp);
                AsyncLogHelper.Info($"LBCamera[{SN}]: 图像入队,当前队列:{queue.Count}");
 
                // 6. 启动队列(单例,避免多线程重复)
                StartConsumeQueue();
                //Task.Factory.StartNew(() =>
                //{
                //    CallBackImg = (Bitmap)bitmap.Clone();
                //    if (CallBackImg == null)
                //    {
                //        return;
                //    }
                //    if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                //    {
                //        if (mode == TriggerMode.On && source != TriggerSource.Software)
                //            TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 触发运行事件
                //    }
                //    bitmap.Dispose();
                //});
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera[{SN}]: 创建图像失败 - {ex.Message}", ex);
            }
            finally
            {
                // 强制资源释放,绝对杜绝泄漏
                if (bmpData != null)
                {
                    try { bmp?.UnlockBits(bmpData); } catch { }
                }
                // 注意:bmp 已入队,不能在这里释放,由调用者释放
            }
        }
 
        /// <summary>
        /// 启动队列(保证单线程)
        /// </summary>
        private void StartConsumeQueue()
        {
            // 使用轻量级判断,避免重复启动消费任务
            if (CollectedImages.TryGetValue(SN, out var queue) && !queue.IsEmpty)
            {
                Task.Factory.StartNew(ProcessImageQueue, TaskCreationOptions.LongRunning);
            }
        }
 
        /// <summary>
        /// 处理CollectedImages中的缓存图像
        /// 核心逻辑:遍历取第一个图像 -> 赋值给CallBackImg -> 触发事件 -> 释放并移除
        /// </summary>
        private void ProcessImageQueue()
        {
            try
            {
                if (!CollectedImages.TryGetValue(SN, out var queue) || queue.IsEmpty)
                    return;
 
                // 短锁:仅出队,不阻塞生产
                while (queue.TryDequeue(out Bitmap bitmap))
                {
                    using (bitmap) // 自动释放:using 是最安全的方式
                    {
                        try
                        {
                            // 关键:事件传递克隆对象,绝对安全,不传递原资源
                            using (Bitmap clone = (Bitmap)bitmap.Clone())
                            {
                                // 触发图像事件
                                ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, clone, true));
                                CallBackImg = (Bitmap)clone.Clone();
                            }
 
                            // 触发模式判断
                            if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                            {
                                if (mode == TriggerMode.On && source != TriggerSource.Software)
                                {
                                    TriggerRunMessageReceived?.Invoke(SN, source.ToString());
                                    AsyncLogHelper.Info($"LBCamera[{SN}]: 硬触发事件 - {source}");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            AsyncLogHelper.Error($"LBCamera[{SN}]: 处理单帧图像异常 - {ex.Message}", ex);
                            continue; // 单帧异常,继续处理下一帧
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera[{SN}]: 消费队列异常 - {ex.Message}", ex);
            }
        }
 
 
        private void SyncConfigFromCamera()
        {
            try
            {
                if (!_isConnected) return;
 
                PropertyInfo[] props = _sensorConfig.GetType().GetProperties();
                foreach (PropertyInfo p in props)
                {
                    // 跳过自定义参数
                    var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
                    if (iscustomAttr != null) continue;
 
                    if (Enum.TryParse(typeof(EnumNameId), p.Name, out object nameIdObj))
                    {
                        EnumNameId nameId = (EnumNameId)nameIdObj;
                        int intValue = 0;
                        double doubleValue = 0;
                        int enumValue = 0;
 
                        if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0)
                        {
                            if (p.PropertyType == typeof(int))
                            {
                                p.SetValue(_sensorConfig, intValue);
                            }
                            else if (p.PropertyType == typeof(float))
                            {
                                p.SetValue(_sensorConfig, (float)doubleValue);
                            }
                            else if (p.PropertyType == typeof(double))
                            {
                                p.SetValue(_sensorConfig, doubleValue);
                            }
                            else // Enum or other types
                            {
                                p.SetValue(_sensorConfig, enumValue);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: SyncConfigFromCamera error - {ex.Message}");
            }
        }
 
        private bool SetParam(EnumNameId id, float value)
        {
            if (!_isConnected) return false;
            return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, value, 0) == 0;
        }
 
        private bool SetParam(EnumNameId id, int value)
        {
            if (!_isConnected) return false;
            // 对于枚举类型,通常通过 enumValue (最后一个参数) 传递
            return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, 0, value) == 0;
        }
 
        private bool GetParam(EnumNameId id, out float value)
        {
            value = 0;
            if (!_isConnected) return false;
            int iVal = 0; double dVal = 0; int eVal = 0;
            if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0)
            {
                value = (float)dVal;
                return true;
            }
            return false;
        }
 
        private bool GetParam(EnumNameId id, out int value)
        {
            value = 0;
            if (!_isConnected) return false;
            int iVal = 0; double dVal = 0; int eVal = 0;
            if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0)
            {
                value = eVal; // Assuming it returns in enumValue
                return true;
            }
            return false;
        }
        #endregion
    }
}