| | |
| | | using LB_SmartVisionCameraDevice.PHM6000; |
| | | using LB_SmartVisionCameraSDK.PHM6000; |
| | | using LB_SmartVisionCommon; |
| | | using LB_VisionProcesses.Cameras; |
| | | using Sunny.UI.Win32; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Drawing; |
| | |
| | | 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 |
| | | { |
| | |
| | | // 临时行缓冲,用于接收回调数据 |
| | | private byte[] _tempLineBuffer = null; |
| | | private bool _isContinuous = false; |
| | | // 新增:CollectedImages操作锁,保证线程安全 |
| | | private readonly object _collectedImagesLock = new object(); |
| | | |
| | | public LBCamera() |
| | | { |
| | |
| | | { |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: Flushing partial buffer ({_currentLineCount} lines) on completion"); |
| | | CreateAndFireBitmap(); |
| | | |
| | | _currentLineCount = 0; |
| | | } |
| | | } |
| | |
| | | |
| | | _frameCount++; |
| | | AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} generated ({width}x{height})"); |
| | | |
| | | // 异步触发事件,避免阻塞SDK回调线程 |
| | | Task.Run(() => |
| | | //空值校验:转换失败则直接返回 |
| | | if (bmp == null) |
| | | { |
| | | try |
| | | AsyncLogHelper.Warn(SN + "帧转换为Bitmap失败,跳过处理"); |
| | | return; |
| | | } |
| | | // 线程安全地将Bitmap添加到CollectedImages字典 |
| | | lock (_collectedImagesLock) |
| | | { |
| | | // 确保当前相机SN对应的列表存在 |
| | | if (!CollectedImages.ContainsKey(SN)) |
| | | { |
| | | ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true)); |
| | | CollectedImages[SN] = new List<Bitmap>(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}"); |
| | | bmp.Dispose(); // 异常时释放资源 |
| | | } |
| | | }); |
| | | CollectedImages[SN].Add(bmp); |
| | | AsyncLogHelper.Info(SN + $"图像已加入缓存,当前缓存数量:{CollectedImages[SN].Count}"); |
| | | } |
| | | |
| | | // 处理CollectedImages中的图像:遍历消费列表第一个元素直到为空 |
| | | ProcessCollectedImages(); |
| | | //// 异步触发事件,避免阻塞SDK回调线程 |
| | | //Task.Factory.StartNew(() => |
| | | //{ |
| | | // try |
| | | // { |
| | | // ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true)); |
| | | // CallBackImg = (Bitmap)bmp.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()); // 触发运行事件 |
| | | // } |
| | | // bmp.Dispose(); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}"); |
| | | // bmp.Dispose(); // 异常时释放资源 |
| | | // } |
| | | //}); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | AsyncLogHelper.Error($"LBCamera: CreateBitmap error - {ex.Message}"); |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 处理CollectedImages中的缓存图像 |
| | | /// 核心逻辑:遍历取第一个图像 -> 赋值给CallBackImg -> 触发事件 -> 释放并移除 |
| | | /// </summary> |
| | | private void ProcessCollectedImages() |
| | | { |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | // 加锁保证线程安全,防止多线程同时操作列表 |
| | | lock (_collectedImagesLock) |
| | | { |
| | | // 校验当前相机的图像列表是否存在且有数据 |
| | | if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0) |
| | | { |
| | | AsyncLogHelper.Info(SN + "当前无缓存图像,跳过处理"); |
| | | return; |
| | | } |
| | | // 循环处理:直到列表为空 |
| | | while (CollectedImages[SN].Count > 0) |
| | | { |
| | | try |
| | | { |
| | | // 1 取列表第一个索引的图像赋值给CallBackImg |
| | | Bitmap firstBitmap = CollectedImages[SN][0]; |
| | | ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true)); |
| | | CallBackImg = (Bitmap)firstBitmap.Clone(); // 克隆避免原对象被释放后引用失效 |
| | | |
| | | // 2 获取触发模式并判断是否触发运行事件 |
| | | if (GetTriggerMode(out TriggerMode mode, out TriggerSource source)) |
| | | { |
| | | // 硬触发模式下触发运行事件 |
| | | if (mode == TriggerMode.On && source != TriggerSource.Software) |
| | | { |
| | | AsyncLogHelper.Info(SN + $"触发硬触发事件,触发源:{source}"); |
| | | TriggerRunMessageReceived?.Invoke(SN, source.ToString()); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | AsyncLogHelper.Warn(SN + "获取触发模式失败,跳过事件触发"); |
| | | } |
| | | |
| | | // 3 释放第一个图像资源并从列表移除 |
| | | // 先释放Bitmap内存,再移除列表元素 |
| | | firstBitmap.Dispose(); |
| | | CollectedImages[SN].RemoveAt(0); |
| | | AsyncLogHelper.Info(SN + $"已消费缓存图像,剩余缓存数量:{CollectedImages[SN].Count}"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | AsyncLogHelper.Error(SN + $"处理缓存图像异常:{ex.Message}", ex); |
| | | // 单个图像处理失败时,移除该图像避免阻塞后续处理 |
| | | if (CollectedImages[SN].Count > 0) |
| | | { |
| | | try |
| | | { |
| | | CollectedImages[SN][0]?.Dispose(); // 尝试释放 |
| | | CollectedImages[SN].RemoveAt(0); |
| | | } |
| | | catch (Exception innerEx) |
| | | { |
| | | AsyncLogHelper.Error(SN + $"清理异常图像失败:{innerEx.Message}", innerEx); |
| | | } |
| | | } |
| | | // 单个图像处理失败不终止循环,继续处理下一个 |
| | | // 4. 所有图像处理完成后,清空CallBackImg |
| | | if (CallBackImg != null) |
| | | { |
| | | CallBackImg.Dispose(); |
| | | CallBackImg = null; |
| | | } |
| | | continue; |
| | | } |
| | | // 4. 所有图像处理完成后,清空CallBackImg |
| | | if (CallBackImg != null) |
| | | { |
| | | CallBackImg.Dispose(); |
| | | CallBackImg = null; |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | private void SyncConfigFromCamera() |
| | | { |