| | |
| | | using LB_SmartVisionCameraSDK.PHM6000; |
| | | using LB_SmartVisionCommon; |
| | | using LB_VisionProcesses.Cameras; |
| | | using OpenCvSharp; |
| | | using OpenCvSharp.Extensions; |
| | | 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; |
| | |
| | | { |
| | | private IntPtr _cameraHandle = IntPtr.Zero; |
| | | private PHM6000SensorConfig _sensorConfig; |
| | | |
| | | // 采集回调 |
| | | private AcquisitionCallbackZA _acquisitionCallback; |
| | | private AcquisitionCompletedCallback _acquisitionCompletedCallback; |
| | | |
| | | private bool _isConnected = false; |
| | | |
| | | public bool _isConnected = false; |
| | | private int _frameCount = 0; // 采集帧计数 |
| | | |
| | | // 图像缓冲 |
| | |
| | | { |
| | | 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)) |
| | | { |
| | |
| | | if (System.Net.IPAddress.TryParse(sn, out _)) |
| | | { |
| | | targetIp = sn; |
| | | targetPort = 5577; |
| | | targetPort = 5577; |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | var addr = Encoding.ASCII.GetBytes(targetIp); |
| | | int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort); |
| | | |
| | | |
| | | if (result == 0) |
| | | { |
| | | _isConnected = true; |
| | |
| | | |
| | | // 初始化并注册采集回调 (获取数据用) |
| | | _acquisitionCallback = new AcquisitionCallbackZA(OnAcquisitionCallbackZA); |
| | | PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero); |
| | | PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, new IntPtr()); |
| | | |
| | | // 初始化并注册采集完成回调 (状态通知用) |
| | | _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted); |
| | | PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero); |
| | | _acquisitionCompletedCallback += OnAcquisitionCompleted; |
| | | PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, new IntPtr()); |
| | | |
| | | // 强制应用当前配置(确保触发模式等参数正确,避免相机处于未知状态) |
| | | UpdateSensorConfig(_sensorConfig); |
| | |
| | | AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | catch (Exception ex) |
| | | { |
| | | AsyncLogHelper.Error($"LBCamera: InitDevice异常 - {ex.Message}"); |
| | | if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle); |
| | | } |
| | |
| | | if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0) |
| | | { |
| | | string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0'); |
| | | if (!string.IsNullOrEmpty(sn)) |
| | | if (!string.IsNullOrEmpty(sn) && sn.Contains("L")) |
| | | { |
| | | cameraList.Add(sn); |
| | | } |
| | |
| | | public bool StartSingleGrab() |
| | | { |
| | | if (!_isConnected) return false; |
| | | |
| | | |
| | | _isContinuous = false; |
| | | InitBuffer(); |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: 开始单次采集"); |
| | |
| | | _isContinuous = false; |
| | | if (!_isConnected) return true; |
| | | PHM6000Profiler.StopAcquisition(_cameraHandle); |
| | | |
| | | |
| | | // 停止时如果有未显示的缓存数据,将其显示出来(支持显示不完整的帧) |
| | | lock (_bufferLock) |
| | | { |
| | |
| | | _currentLineCount = 0; |
| | | } |
| | | } |
| | | |
| | | |
| | | isGrabbing = false; |
| | | return true; |
| | | } |
| | |
| | | { |
| | | if (!_isConnected) return false; |
| | | |
| | | if (triggerEnum == TriggerSource.Software) |
| | | { |
| | | _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.固定频率; |
| | | _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.软触发; |
| | | } |
| | | else |
| | | { |
| | | _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.编码器; |
| | | _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.外部触发; |
| | | } |
| | | //if (triggerEnum == TriggerSource.Software) |
| | | //{ |
| | | // _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.固定频率; |
| | | // _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.软触发; |
| | | //} |
| | | //else |
| | | //{ |
| | | // _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.编码器; |
| | | // _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.外部触发; |
| | | //} |
| | | UpdateSensorConfig(_sensorConfig); |
| | | return 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) |
| | | public override void GetCamConfig(out CameraConfig config) |
| | | { |
| | | config = new CameraConfig(null); |
| | | //UpdateSensorConfig(config); |
| | | } |
| | | public override bool GetImage(out Bitmap bitmap, int outtime = 14500) |
| | | { |
| | | bitmap = null; |
| | | try |
| | | { |
| | | // 设置超时时间 |
| | | DateTime lastTime = DateTime.Now.AddMilliseconds(outtime); |
| | | // 判断是否超时 |
| | | while (lastTime > DateTime.Now)// 设置超时时间为 3 秒 |
| | | { |
| | | if (CallBackImg != null) |
| | | { |
| | | lock (CallBackImg) |
| | | { |
| | | // 保存旧 Bitmap 并释放 |
| | | bitmap = CallBackImg; // 创建副本 |
| | | } |
| | | |
| | | //// 释放旧资源 |
| | | //CallBackImg.Dispose(); |
| | | //CallBackImg = null; |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | catch { return bitmap == null ? false : true; } |
| | | } |
| | | public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000) |
| | | { |
| | | // 简单实现:软触发等待 |
| | | bitmap = null; |
| | | if(!_isConnected) return false; |
| | | if (!_isConnected) return false; |
| | | |
| | | // 计算理论最小耗时 (仅当使用固定频率触发时) |
| | | int minTime = 0; |
| | |
| | | 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(); |
| | | 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)) |
| | |
| | | } |
| | | |
| | | ImageGrabbed -= handler; |
| | | |
| | | |
| | | // 确保停止采集 |
| | | StopGrabbing(); |
| | | |
| | | |
| | | bitmap = res; |
| | | return bitmap != null; |
| | | } |
| | |
| | | |
| | | 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.固定频率) |
| | | //_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.固定频率) |
| | | { |
| | | PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0); |
| | | 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 |
| | |
| | | // 提取灰度(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) |
| | |
| | | if (_currentLineCount >= _currentBitmapHeight) |
| | | { |
| | | CreateAndFireBitmap(); |
| | | |
| | | |
| | | // 重置,准备下一帧 (如果是连续采集) |
| | | _currentLineCount = 0; |
| | | // _rawPixelBuffer 可以复用,不需要置空 |
| | | } |
| | | } |
| | | } |
| | | private Bitmap bitmap1; |
| | | private int nWidth = 0, nHeight = 0; |
| | | |
| | | private void OnAcquisitionCompleted(IntPtr pInstance, int nOption) |
| | | { |
| | |
| | | { |
| | | // 如果在连续模式下收到结束信号,尝试自动重启采集 |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting..."); |
| | | Task.Run(() => |
| | | Task.Run(() => |
| | | { |
| | | if (_isContinuous && _isConnected) |
| | | { |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | else if (nOption == 2) |
| | | { |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: Processing End..."); |
| | | IntPtr INTPTRImage = PHM6000Profiler.GetIntensityData(_cameraHandle, ref nWidth, ref nHeight); |
| | | bitmap1 = IntensityPtrToMatCloned(INTPTRImage, nWidth, nHeight); |
| | | } |
| | | } |
| | | |
| | | public Bitmap ConvertIntensityToBitmap(IntPtr dataPtr, int width, int height) |
| | | { |
| | | |
| | | if (dataPtr == IntPtr.Zero || width <= 0 || height <= 0) |
| | | throw new InvalidOperationException("获取强度数据失败或尺寸无效。"); |
| | | |
| | | // 创建 8bpp 索引位图 |
| | | Bitmap bitmap = new Bitmap(width, 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, width, height), |
| | | ImageLockMode.WriteOnly, |
| | | PixelFormat.Format8bppIndexed); |
| | | |
| | | try |
| | | { |
| | | int srcStride = width; // 源数据每行字节数(无填充) |
| | | int dstStride = bmpData.Stride; // 位图每行字节数(可能对齐) |
| | | int copyBytesPerRow = Math.Min(srcStride, dstStride); |
| | | |
| | | IntPtr srcRowPtr = dataPtr; |
| | | IntPtr dstRowPtr = bmpData.Scan0; |
| | | |
| | | // 逐行复制,处理可能的步幅差异 |
| | | for (int y = 0; y < height; y++) |
| | | { |
| | | // 使用 Buffer.MemoryCopy 或 Marshal.Copy 按行复制 |
| | | unsafe |
| | | { |
| | | Buffer.MemoryCopy( |
| | | srcRowPtr.ToPointer(), |
| | | dstRowPtr.ToPointer(), |
| | | dstStride, // 目标缓冲区剩余大小(至少一行) |
| | | copyBytesPerRow); // 实际复制字节数 |
| | | } |
| | | |
| | | // 移动到下一行 |
| | | srcRowPtr = IntPtr.Add(srcRowPtr, srcStride); |
| | | dstRowPtr = IntPtr.Add(dstRowPtr, dstStride); |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | bitmap.UnlockBits(bmpData); |
| | | } |
| | | |
| | | return bitmap; |
| | | } |
| | | |
| | | |
| | | public static Bitmap IntensityPtrToMatCloned(IntPtr dataPtr, int width, int height) |
| | | { |
| | | |
| | | if (dataPtr == IntPtr.Zero || width <= 0 || height <= 0) |
| | | { |
| | | throw new Exception("Failed to get intensity data."); |
| | | } |
| | | |
| | | // 先创建引用外部数据的 Mat |
| | | using (Mat temp = Mat.FromPixelData(height, width, MatType.CV_8UC1, dataPtr, width)) |
| | | { |
| | | // 克隆一份独立内存的 Mat |
| | | return temp.ToBitmap(); |
| | | } |
| | | } |
| | | |
| | |
| | | bmp.UnlockBits(bmpData); |
| | | bmpData = null; // 标记已解锁 |
| | | |
| | | |
| | | |
| | | _frameCount++; |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: 生成第 {_frameCount} 帧 ({width}x{height})"); |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: 生成第 {_frameCount} 帧 ({nWidth}x{nHeight})"); |
| | | |
| | | // 3. 获取/创建线程安全队列 |
| | | var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>()); |
| | |
| | | // 强制资源释放,绝对杜绝泄漏 |
| | | if (bmpData != null) |
| | | { |
| | | try { bmp?.UnlockBits(bmpData); } catch { } |
| | | try { bitmap1?.UnlockBits(bmpData); } catch { } |
| | | } |
| | | // 注意:bmp 已入队,不能在这里释放,由调用者释放 |
| | | //注意:bmp 已入队,不能在这里释放,由调用者释放 |
| | | } |
| | | } |
| | | |
| | |
| | | TriggerRunMessageReceived?.Invoke(SN, source.ToString()); |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: 硬触发事件 - {source}"); |
| | | } |
| | | else |
| | | { |
| | | TriggerRunMessageReceived?.Invoke(SN, source.ToString()); |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: 硬触发事件 - {source}"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | |
| | | |
| | | if (Enum.TryParse(typeof(EnumNameId), p.Name, out object nameIdObj)) |
| | | { |
| | | EnumNameId nameId = (EnumNameId)nameIdObj; |
| | | //EnumNameId nameId = (EnumNameId)nameIdObj; |
| | | var nameId = Enum.Parse(typeof(EnumNameId), p.Name); |
| | | int intValue = 0; |
| | | double doubleValue = 0; |
| | | int enumValue = 0; |
| | | |
| | | if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0) |
| | | var id = Convert.ToInt32(nameId); |
| | | var rst = PHM6000Profiler.GetProfilerParameter(_cameraHandle, id, ref intValue, ref doubleValue, ref enumValue); |
| | | |
| | | |
| | | if (rst == 0) |
| | | { |
| | | if (p.PropertyType == typeof(int)) |
| | | { |
| | |
| | | } |
| | | else // Enum or other types |
| | | { |
| | | p.SetValue(_sensorConfig, enumValue); |
| | | if (p.Name.Equals("ROI")) |
| | | { |
| | | |
| | | } |
| | | else |
| | | { |
| | | p.SetValue(_sensorConfig, enumValue); |
| | | } |
| | | } |
| | | } |
| | | } |