From d1444a09d4a4811a9ea7b86cb264d753ef14088c Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 08 四月 2026 08:31:59 +0800
Subject: [PATCH] 运行目录添加对应相机dll文件以及修复相机取图。
---
LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs | 121 ++++++++++++++++++----------------------
1 files changed, 54 insertions(+), 67 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
index f176eab..f5bfb15 100644
--- a/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
+++ b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
@@ -2,7 +2,10 @@
using LB_SmartVisionCommon;
using LB_VisionProcesses.Cameras.LBCameras;
using MVSDK_Net;
+using Sunny.UI;
using System;
+using System.Collections;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
@@ -231,20 +234,26 @@
AsyncLogHelper.Warn(SN + "甯ц浆鎹负Bitmap澶辫触锛岃烦杩囧鐞�");
return;
}
- // 3. 绾跨▼瀹夊叏鍦板皢Bitmap娣诲姞鍒癈ollectedImages瀛楀吀
- lock (_collectedImagesLock)
+ // 3. 鑾峰彇/鍒涘缓绾跨▼瀹夊叏闃熷垪
+ var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>());
+ AsyncLogHelper.Info(SN + $"鍥惧儚宸插姞鍏ョ紦瀛橈紝褰撳墠缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
+ // 4. 闃熷垪闄愭祦锛岄槻姝㈠唴瀛樻孩鍑�
+ if (queue.Count >= MAX_QUEUE_CAPACITY)
{
- // 纭繚褰撳墠鐩告満SN瀵瑰簲鐨勫垪琛ㄥ瓨鍦�
- if (!CollectedImages.ContainsKey(SN))
+ if (queue.TryDequeue(out Bitmap old))
{
- CollectedImages[SN] = new List<Bitmap>();
+ old.Dispose(); // 涓㈠純鏈�鏃у抚锛岄噴鏀惧唴瀛�
+ AsyncLogHelper.Warn($"HRCamera[{SN}]: 闃熷垪宸叉弧锛岃嚜鍔ㄤ涪寮冩渶鏃у抚");
}
- CollectedImages[SN].Add(bitmap);
- AsyncLogHelper.Info(SN + $"鍥惧儚宸插姞鍏ョ紦瀛橈紝褰撳墠缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
}
- // 4. 澶勭悊CollectedImages涓殑鍥惧儚锛氶亶鍘嗘秷璐瑰垪琛ㄧ涓�涓厓绱犵洿鍒颁负绌�
- ProcessCollectedImages();
+
+ // 5. 鍏ラ槦
+ queue.Enqueue(bitmap);
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 鍥惧儚鍏ラ槦锛屽綋鍓嶉槦鍒楋細{queue.Count}");
+
+ // 6. 鍚姩闃熷垪锛堝崟渚嬶紝閬垮厤澶氱嚎绋嬮噸澶嶏級
+ StartConsumeQueue();
//Task.Factory.StartNew(() =>
//{
// CallBackImg = (Bitmap)bitmap.Clone();
@@ -265,87 +274,65 @@
}
/// <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 ProcessCollectedImages()
+ private void ProcessImageQueue()
{
- Task.Factory.StartNew(() =>
+ try
{
- // 鍔犻攣淇濊瘉绾跨▼瀹夊叏锛岄槻姝㈠绾跨▼鍚屾椂鎿嶄綔鍒楄〃
- lock (_collectedImagesLock)
+ if (!CollectedImages.TryGetValue(SN, out var queue) || queue.IsEmpty)
+ return;
+
+ // 鐭攣锛氫粎鍑洪槦锛屼笉闃诲鐢熶骇
+ while (queue.TryDequeue(out Bitmap bitmap))
{
- // 鏍¢獙褰撳墠鐩告満鐨勫浘鍍忓垪琛ㄦ槸鍚﹀瓨鍦ㄤ笖鏈夋暟鎹�
- if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0)
- {
- AsyncLogHelper.Info(SN + "褰撳墠鏃犵紦瀛樺浘鍍忥紝璺宠繃澶勭悊");
- return;
- }
- // 寰幆澶勭悊锛氱洿鍒板垪琛ㄤ负绌�
- while (CollectedImages[SN].Count > 0)
+ using (bitmap) // 鑷姩閲婃斁锛歶sing 鏄渶瀹夊叏鐨勬柟寮�
{
try
{
- // 1 鍙栧垪琛ㄧ涓�涓储寮曠殑鍥惧儚璧嬪�肩粰CallBackImg
- Bitmap firstBitmap = CollectedImages[SN][0];
- ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true));
- CallBackImg = (Bitmap)firstBitmap.Clone(); // 鍏嬮殕閬垮厤鍘熷璞¤閲婃斁鍚庡紩鐢ㄥけ鏁�
+ // 鍏抽敭锛氫簨浠朵紶閫掑厠闅嗗璞★紝缁濆瀹夊叏锛屼笉浼犻�掑師璧勬簮
+ using (Bitmap clone = (Bitmap)bitmap.Clone())
+ {
+ // 瑙﹀彂鍥惧儚浜嬩欢
+ ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, clone, true));
+ CallBackImg = (Bitmap)clone.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());
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
}
}
- else
- {
- AsyncLogHelper.Warn(SN + "鑾峰彇瑙﹀彂妯″紡澶辫触锛岃烦杩囦簨浠惰Е鍙�");
- }
-
- // 3 閲婃斁绗竴涓浘鍍忚祫婧愬苟浠庡垪琛ㄧЩ闄�
- // 鍏堥噴鏀綛itmap鍐呭瓨锛屽啀绉婚櫎鍒楄〃鍏冪礌
- 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. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
- if (CallBackImg != null)
- {
- CallBackImg.Dispose();
- CallBackImg = null;
- }
- continue;
- }
- // 4. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
- if (CallBackImg != null)
- {
- CallBackImg.Dispose();
- CallBackImg = null;
+ AsyncLogHelper.Error($"LBCamera[{SN}]: 澶勭悊鍗曞抚鍥惧儚寮傚父 - {ex.Message}", ex);
+ continue; // 鍗曞抚寮傚父锛岀户缁鐞嗕笅涓�甯�
}
}
}
- });
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera[{SN}]: 娑堣垂闃熷垪寮傚父 - {ex.Message}", ex);
+ }
}
/// <summary>
--
Gitblit v1.9.3