C3032
2026-01-08 7279c77f318cd7e38af279dc98a1fecec33f5e30
LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -13,6 +13,15 @@
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系列封装
@@ -21,25 +30,94 @@
    {
        private IntPtr _cameraHandle = IntPtr.Zero;
        private PHM6000SensorConfig _sensorConfig;
        private AcquisitionCallbackZA _acquisitionCallback;
        private AcquisitionCompletedCallback _acquisitionCompletedCallback;
        private bool _isConnected = false;
        private int _frameCount = 0; // 采集帧计数
        // 图像缓冲
        private List<byte[]> _lineDataBuffer = new List<byte[]>();
        private readonly object _bufferLock = new object();
        private int _currentLineCount = 0;
        // 实时显示用的Bitmap
        private Bitmap _currentBitmap = null;
        private byte[] _rawPixelBuffer = null; // 用于存储像素数据,避免频繁LockBits
        private int _currentBitmapHeight = 0;
        private int _currentBitmapWidth = 0;
        private object _bitmapLock = new object();
        private DateTime _lastUpdateTime = DateTime.MinValue;
        // 显示句柄和函数(与LLSystem示例保持一致)
        private IntPtr _lightPic = IntPtr.Zero;
        private IntPtr _deepPic = IntPtr.Zero;
        private IntPtr _pointPic = IntPtr.Zero;
        private IntPtr _outlinePic = IntPtr.Zero;
        // 显示控制函数结构体
        private PILOT2D_FUNC _func2d;
        private VTK3D_FUNC _func3d;
        public LBCamera()
        {
            Brand = CameraBrand.LBCamera;
            _sensorConfig = new PHM6000SensorConfig();
            // 初始化2D显示函数
            _func2d = new PILOT2D_FUNC
            {
                AddBarycentreDataZA = Pilot2D.AddBarycentreDataZA,
                AddDepthData = Pilot2D.AddDepthData,
                AddIntensityData = Pilot2D.AddIntensityData,
                ClearAllPoints = Pilot2D.ClearAllPoints,
                RefreshPilot2D = Pilot2D.RefreshPilot2D,
                SetImageSize = Pilot2D.SetImageSize,
            };
            // 初始化3D显示函数
            _func3d = new VTK3D_FUNC
            {
                AddZAPoints = PHM6000Profiler.AddZAPoints,
                ClearPCLPoints = PHM6000Profiler.ClearPCLPoints,
                GetPointCloudBound = PHM6000Profiler.GetPointCloudBound,
                RenderPCLWindow = PHM6000Profiler.RenderPCLWindow,
                SetLookUpTableRange = PHM6000Profiler.SetLookUpTableRange,
                ShowCubeAxes = PHM6000Profiler.ShowCubeAxes,
                ShowLookUpTable = PHM6000Profiler.ShowLookUpTable,
                UpdatePCLPointColors = PHM6000Profiler.UpdatePCLPointColors,
            };
        }
        #region ICamera Implementation
        public override bool InitDevice(string sn, object handle = null)
        {
            // 如果已连接,仅检查是否需要更新显示句柄
            if (_isConnected && _cameraHandle != IntPtr.Zero)
            {
                if (handle != null && handle is IntPtr hPtr && hPtr != IntPtr.Zero)
                {
                    // 销毁旧的显示句柄
                    if (_lightPic != IntPtr.Zero)
                    {
                        Pilot2D.ClearAllPoints(_lightPic);
                        Pilot2D.DestroyPilot2DEntry(_lightPic);
                    }
                    // 创建新的显示句柄
                    _lightPic = Pilot2D.CreatePilot2DEntry(hPtr);
                    if (_lightPic != IntPtr.Zero)
                    {
                        var config = GetSensorConfig();
                        Pilot2D.SetImageSize(_lightPic, 4096, config.ScanLineCount > 0 ? config.ScanLineCount : 5000);
                        // 更新相机绑定的显示句柄
                        PHM6000Profiler.SetShowHandles(_cameraHandle, _lightPic, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                        AsyncLogHelper.Info($"LBCamera[{SN}]: Display handle updated");
                    }
                }
                return true;
            }
            IntPtr tempHandle = IntPtr.Zero;
            try
            {
@@ -93,7 +171,7 @@
                if (!found)
                {
                    // 如果没找到但 sn 本身看起来像 IP,尝试直接连接(兼容旧逻辑)
                    // 如果没找到但 sn 本身看起来像 IP,尝试直接连接
                    if (System.Net.IPAddress.TryParse(sn, out _))
                    {
                        targetIp = sn;
@@ -119,14 +197,41 @@
                    // 加载相机当前参数到 _sensorConfig
                    SyncConfigFromCamera();
                    // 创建亮度图显示句柄(类似LLSystem示例)
                    IntPtr hPtr = IntPtr.Zero;
                    if (handle is IntPtr p) hPtr = p;
                    if (hPtr != IntPtr.Zero)
                    {
                        _lightPic = Pilot2D.CreatePilot2DEntry(hPtr);
                        if (_lightPic != IntPtr.Zero)
                        {
                            // 设置图像尺寸,宽度4096,高度根据配置
                            var config = GetSensorConfig();
                            Pilot2D.SetImageSize(_lightPic, 4096, config.ScanLineCount > 0 ? config.ScanLineCount : 5000);
                        }
                    }
                    // 初始化回调
                    _acquisitionCallback = new AcquisitionCallbackZA(OnLineReceived);
                    _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
                    
                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
                    // 注册采集完成回调(类似LLSystem示例)
                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
                    // 设置2D和3D显示函数(类似LLSystem示例)
                    PHM6000Profiler.SetPilot2dFunc(_cameraHandle, _func2d);
                    PHM6000Profiler.SetVTK3dFunc(_cameraHandle, _func3d);
                    // 设置显示句柄(类似LLSystem示例的SetShowHandles)
                    PHM6000Profiler.SetShowHandles(_cameraHandle, _lightPic, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Connected and initialized successfully with SDK auto-display mode");
                    return true;
                }
                else
                {
                    AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
                }
            }
            catch (Exception ex) 
@@ -142,9 +247,20 @@
            if (_isConnected && _cameraHandle != IntPtr.Zero)
            {
                StopGrabbing();
                // 销毁显示句柄
                if (_lightPic != IntPtr.Zero)
                {
                    Pilot2D.ClearAllPoints(_lightPic);
                    Pilot2D.DestroyPilot2DEntry(_lightPic);
                    _lightPic = IntPtr.Zero;
                }
                PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
                _cameraHandle = IntPtr.Zero;
                _isConnected = false;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Closed and cleaned up");
            }
            return true;
        }
@@ -188,22 +304,90 @@
            return cameraList;
        }
        public override bool StartGrabbing()
        private void InitBuffer()
        {
            if (!_isConnected) return false;
            lock (_bufferLock)
            lock (_bitmapLock)
            {
                _lineDataBuffer.Clear();
                _currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
                // 宽度暂时未知,将在第一行数据到达时初始化
                _currentBitmapWidth = 0;
                if (_currentBitmap != null)
                {
                    _currentBitmap.Dispose();
                    _currentBitmap = null;
                }
                _rawPixelBuffer = null;
                _currentLineCount = 0;
            }
        }
            // 设置采集模式:1=扫描模式,1=连续模式
            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 1);
        public override bool StartGrabbing()
        {
            // 线扫相机默认使用连续采集模式
            // 参考LLSystem示例的ContinuousScan方法:SetAcquisitionMode(1, 1)
            return StartSingleGrab();
        }
        /// <summary>
        /// 单次采集模式(适用于线扫相机)
        /// 参考LLSystem示例的StartScan方法:SetAcquisitionMode(1, 0)
        /// 使用SDK自动显示模式,不手动处理数据
        /// </summary>
        public bool StartSingleGrab()
        {
            if (!_isConnected) return false;
            AsyncLogHelper.Info($"LBCamera[{SN}]: Starting single grab mode");
            // 使用SDK自动显示模式,类似LLSystem示例
            // 不设置AcquisitionCallbackZA,让SDK自动处理图像显示
            PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
            // 设置采集模式:1=扫描模式,0=单次模式(与LLSystem示例保持一致)
            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 0);
            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
            if (result == 0)
            {
                isGrabbing = true;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Single grab started successfully");
                return true;
            }
            else
            {
                AsyncLogHelper.Error($"LBCamera[{SN}]: Failed to start single grab, result={result}");
            }
            return false;
        }
        /// <summary>
        /// 连续采集模式(适用于线扫相机)
        /// 参考LLSystem示例的ContinuousScan方法:SetAcquisitionMode(1, 1)
        /// 使用SDK自动显示模式,不手动处理数据
        /// </summary>
        public override bool StartContinuousGrab()
        {
            if (!_isConnected) return false;
            AsyncLogHelper.Info($"LBCamera[{SN}]: Starting continuous grab mode");
            // 使用SDK自动显示模式,类似LLSystem示例
            // 不设置AcquisitionCallbackZA,让SDK自动处理图像显示
            PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
            // 设置采集模式:1=扫描模式,1=连续模式(与LLSystem示例保持一致)
            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 1);
            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
            if (result == 0)
            {
                isGrabbing = true;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous grab started successfully");
                return true;
            }
            else
            {
                AsyncLogHelper.Error($"LBCamera[{SN}]: Failed to start continuous grab, result={result}");
            }
            return false;
        }
@@ -214,6 +398,22 @@
            PHM6000Profiler.StopAcquisition(_cameraHandle);
            isGrabbing = false;
            return true;
        }
        public override bool StartWith_SoftTriggerModel()
        {
            // 对于LBCamera(线扫相机),软件触发连续采集
            // 参考LLSystem示例的ContinuousScan方法:SetAcquisitionMode(1, 1)
            return StartContinuousGrab();
        }
        public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
        {
            // 对于LBCamera(线扫相机),硬件触发也使用连续采集模式
            // 参考LLSystem示例的ContinuousScan方法:SetAcquisitionMode(1, 1)
            // 外部硬件信号会触发相机开始采集
            return StartSingleGrab();
        }
        public override bool SoftTrigger()
@@ -242,6 +442,59 @@
        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;
            // 对于线扫相机,使用SDK自动显示模式
            // 注意:SDK自动显示模式下,我们无法直接获取Bitmap
            // 但SDK会自动将图像显示到_lightPic句柄对应的控件上
            // 我们只需要启动单次采集,等待采集完成即可
            using (AutoResetEvent waitHandle = new AutoResetEvent(false))
            {
                bool captured = false;
                EventHandler<CameraEventArgs> handler = (s, e) =>
                {
                    // 对于线扫相机,仅当IsComplete为true时才表示采集完成
                    if (e is LBCameraEventArgs args && args.IsComplete)
                    {
                        captured = true;
                        waitHandle.Set();
                    }
                };
                this.ImageGrabbed += handler;
                try
                {
                    // 使用单次采集模式(与LLSystem示例保持一致)
                    if (StartSingleGrab())
                    {
                        // 等待采集完成
                        if (waitHandle.WaitOne(outtime))
                        {
                            // 由于SDK自动显示,我们无法返回Bitmap
                            // 但采集已完成,图像已显示到界面上
                            bitmap = null;  // SDK自动显示模式下不返回Bitmap
                            return captured;
                        }
                    }
                }
                finally
                {
                    this.ImageGrabbed -= handler;
                    StopGrabbing();
                }
            }
            return false;
        }
        public PHM6000SensorConfig GetSensorConfig()
        {
@@ -252,80 +505,57 @@
        public void UpdateSensorConfig(PHM6000SensorConfig config)
        {
            _sensorConfig = config;
            // 简单示例:设置曝光和增益
            SetExpouseTime(config.ExposureTime);
            SetGain((double)config.AnalogGain);
            // 更多参数同步逻辑应在此处实现
            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);
            }
            // 确保参数生效
            PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
        }
        #endregion
        #endregion
        #region Private Callback & Helpers
        private void OnLineReceived(IntPtr pInstance, IntPtr buffer, int points)
        {
            // 实时回调处理:累积行数据
            if (!isGrabbing) return;
            int lineSize = points * Marshal.SizeOf(typeof(LBPointZA));
            byte[] lineData = new byte[lineSize];
            Marshal.Copy(buffer, lineData, 0, lineSize);
            lock (_bufferLock)
            {
                _lineDataBuffer.Add(lineData);
                _currentLineCount++;
            }
        }
        private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
        {
            // nOption: 0=一批数据结束, 1=全部完成, 2=点云就绪
            if (nOption == 1 || nOption == 0)
            // nOption含义:
            // 0 = 一批数据结束
            // 1 = 全部采集完成(仅单次模式)
            // 2 = 对应数据处理完成(每次采集完成都会触发)
            // 记录回调信息
            AsyncLogHelper.Info($"LBCamera[{SN}]: OnAcquisitionCompleted called, nOption={nOption}");
            if (nOption == 2)
            {
                GenerateIntensityMap();
            }
        }
                // 连续采集模式下,每完成一帧SDK都会自动继续采集下一帧
                // SDK会自动处理图像显示到_lightPic句柄
                // 我们只需要统计采集次数
                _frameCount++;
        private void GenerateIntensityMap()
        {
            if (_cameraHandle == IntPtr.Zero) return;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} completed (continuous mode, SDK auto-display)");
            int width = 0;
            int height = 0;
            // 直接从 SDK 获取合并后的强度数据指针 (unsigned char*)
            IntPtr pIntensity = PHM6000Profiler.GetIntensityData(_cameraHandle, ref width, ref height);
            if (pIntensity == IntPtr.Zero || width <= 0 || height <= 0) return;
            try
            {
                Bitmap 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;
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                // 高性能内存拷贝
                int size = width * height;
                byte[] managedData = new byte[size];
                Marshal.Copy(pIntensity, managedData, 0, size);
                Marshal.Copy(managedData, 0, bmpData.Scan0, size);
                bmp.UnlockBits(bmpData);
                // 触发事件通知 UI 更新亮度图
                ImageGrabbed?.Invoke(this, new CameraEventArgs(SN, bmp));
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: 生成亮度图异常 - {ex.Message}");
                // 触发事件通知UI更新(但不传递Bitmap,因为SDK自动显示)
                ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, null, true));
            }
        }
@@ -360,7 +590,6 @@
            }
            return false;
        }
        #endregion
    }
}