using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using LB_SmartVisionCameraSDK.PHM6000; using LB_VisionProcesses.Cameras; using LB_SmartVisionCommon; using LB_SmartVisionCameraDevice.PHM6000; namespace LB_VisionProcesses.Cameras.LBCameras { /// /// LB3D工业相机实现类 /// 基于PHM6000系列封装 /// public class LBCamera : BaseCamera { private IntPtr _cameraHandle = IntPtr.Zero; private PHM6000SensorConfig _sensorConfig; private AcquisitionCallbackZA _acquisitionCallback; private AcquisitionCompletedCallback _acquisitionCompletedCallback; private bool _isConnected = false; // 图像缓冲 private List _lineDataBuffer = new List(); private readonly object _bufferLock = new object(); private int _currentLineCount = 0; // 显示句柄和函数(与LLSystem示例保持一致) private IntPtr _lightPic = IntPtr.Zero; private IntPtr _deepPic = IntPtr.Zero; private IntPtr _pointPic = IntPtr.Zero; private IntPtr _outlinePic = IntPtr.Zero; public LBCamera() { Brand = CameraBrand.LBCamera; _sensorConfig = new PHM6000SensorConfig(); } #region ICamera Implementation public override bool InitDevice(string sn, object handle = null) { 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 地址,则直接尝试匹配 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) { // 如果没找到但 sn 本身看起来像 IP,尝试直接连接 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(OnLineReceived); _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted); //PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero); PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero); return true; } } 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; } return true; } public override List GetListEnum() { List cameraList = new List(); 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'); string type = Encoding.UTF8.GetString(moduleTypeBytes).TrimEnd('\0'); // 格式参考:PHM6000[SN123456] if (!string.IsNullOrEmpty(sn)) { cameraList.Add(sn); } } } } } catch (Exception ex) { AsyncLogHelper.Error($"LBCamera: GetListEnum异常 - {ex.Message}"); } finally { if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle); } return cameraList; } public override bool StartGrabbing() { if (!_isConnected) return false; lock (_bufferLock) { _lineDataBuffer.Clear(); _currentLineCount = 0; } // 禁用行回调 PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero); // 设置采集模式: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 bool StartSingleGrab() { if (!_isConnected) return false; lock (_bufferLock) { _lineDataBuffer.Clear(); _currentLineCount = 0; } // 禁用行回调(与示例一致) PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero); // 设置采集模式: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 bool StartContinuousGrab() { if (!_isConnected) return false; lock (_bufferLock) { _lineDataBuffer.Clear(); _currentLineCount = 0; } // 禁用行回调(与示例一致) PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero); // 设置采集模式: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() { if (!_isConnected) return true; PHM6000Profiler.StopAcquisition(_cameraHandle); isGrabbing = false; return true; } public override bool StartWith_SoftTriggerModel() { // 对于LBCamera(线扫相机),软件触发连续采集使用连续采集模式 return StartContinuousGrab(); } public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0) { // 对于LBCamera(线扫相机),硬件触发也使用连续采集模式 // 外部硬件信号会触发相机开始采集 return StartContinuousGrab(); } public override bool SoftTrigger() { // 线扫相机通常不需要传统软触发,但在某些模式下可模拟 return 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, (float)gain); public override bool GetGain(out double gain) { float v; bool r = GetParam(EnumNameId.AnalogGain, out v); gain = v; return r; } // 其他接口占位实现 public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0) => true; public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source) { mode = TriggerMode.Off; source = TriggerSource.Software; 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; using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { Bitmap captured = null; EventHandler handler = (s, e) => { try { if (e.Bitmap != null) { captured = e.Bitmap.Clone() as Bitmap; } } catch (Exception ex) { AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger clone error - {ex.Message}"); } waitHandle.Set(); }; this.ImageGrabbed += handler; try { if (StartSingleGrab()) { if (waitHandle.WaitOne(outtime)) { bitmap = captured; return bitmap != null; } } } finally { this.ImageGrabbed -= handler; StopGrabbing(); } } return false; } public PHM6000SensorConfig GetSensorConfig() { SyncConfigFromCamera(); return _sensorConfig; } public void UpdateSensorConfig(PHM6000SensorConfig config) { _sensorConfig = config; // 简单示例:设置曝光和增益 SetExpouseTime(config.ExposureTime); SetGain((double)config.AnalogGain); // 更多参数同步逻辑应在此处实现 } #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) { // 根据SDK文档:nOption为0时表示一批数据结束,为1时表示全部采集完成 // 为了兼容性,也处理nOption == 2(点云就绪) // 此时使用主动获取方式替代可能会导致crash的GenerateIntensityMap if (nOption == 0 || nOption == 1 || nOption == 2) { RetrieveDataAndGenerateImage(); } } private void RetrieveDataAndGenerateImage() { if (_cameraHandle == IntPtr.Zero) return; try { List lineBuffers = new List(); ulong index = 0; IntPtr ptr = IntPtr.Zero; // 像示例一样通过索引获取行数据 while ((ptr = PHM6000Profiler.GetLineDataByIndex(_cameraHandle, index)) != IntPtr.Zero) { try { LBLineDataZA lineData = PHM6000Profiler.ConvertToLBLineDataZA(ptr); // 提取强度数据 (Alpha通道) if (lineData.data != null && lineData.data.Length > 0) { int lineWidth = lineData.data.Length; byte[] intensityLine = new byte[lineWidth]; for (int i = 0; i < lineWidth; i++) { intensityLine[i] = lineData.data[i].alpha; } lineBuffers.Add(intensityLine); } } catch (Exception ex) { // 忽略单行转换错误 } index++; } if (lineBuffers.Count == 0) return; int height = lineBuffers.Count; int width = lineBuffers[0].Length; if (width <= 0 || height <= 0) return; 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); try { int stride = bmpData.Stride; IntPtr scan0 = bmpData.Scan0; for (int y = 0; y < height; y++) { if (lineBuffers[y].Length == width) // 确保宽度一致 { Marshal.Copy(lineBuffers[y], 0, scan0 + y * stride, width); } } } finally { bmp.UnlockBits(bmpData); } // 触发事件通知 UI 更新亮度图 ImageGrabbed?.Invoke(this, new CameraEventArgs(SN, bmp)); } catch (Exception ex) { AsyncLogHelper.Error($"LBCamera: 生成图像异常 - {ex.Message}"); } } private void SyncConfigFromCamera() { // 从相机读取所有参数并填充到 _sensorConfig foreach (EnumNameId id in Enum.GetValues(typeof(EnumNameId))) { int iVal = 0; double dVal = 0; int eVal = 0; if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0) { // 实际项目中应使用反射将值写回 _sensorConfig } } } private bool SetParam(EnumNameId id, float value) { if (!_isConnected) return false; return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, value, 0) == 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; } #endregion } }