From 987241c46c5f76996cc3c77e570d795fdc984113 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期五, 09 一月 2026 10:10:36 +0800
Subject: [PATCH] 增强相机管理和日志记录功能
---
LB_VisionProcesses/Cameras/CameraConfig.cs | 78 ++++++++++++++++++++++++++++++++++-----
1 files changed, 68 insertions(+), 10 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/CameraConfig.cs b/LB_VisionProcesses/Cameras/CameraConfig.cs
index fa0bb79..4bec6f7 100644
--- a/LB_VisionProcesses/Cameras/CameraConfig.cs
+++ b/LB_VisionProcesses/Cameras/CameraConfig.cs
@@ -1,6 +1,9 @@
-锘縰sing Newtonsoft.Json;
+锘縰sing HalconDotNet;
+using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenCvSharp;
+using OpenCvSharp.Extensions;
+using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
@@ -190,7 +193,7 @@
Mat src = Cv2.ImRead(SN);
if (src != null && !src.Empty())
- OutputImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);
+ OutputImage = src.ToBitmap();
}
}
}
@@ -206,12 +209,24 @@
return Result;
}
- if (Camera == null || Camera.SN != SN)
+ // 濮嬬粓妫�鏌ョ紦瀛樼殑Camera瀵硅薄鏄惁涓庡叏灞�瀛楀吀涓殑涓�鑷�
+ // 闃叉鐩告満琚垹闄ゅ苟閲嶅缓鍚�(SN鐩稿悓), 浠嶇劧寮曠敤鏃х殑宸查攢姣佸璞�
+ if (dicCameras.ContainsKey(SN))
{
- if (dicCameras.ContainsKey(SN))
- Camera = dicCameras[SN];
- else
- Camera = null;
+ var currentCam = dicCameras[SN];
+ if (Camera != currentCam)
+ {
+ Camera = currentCam;
+ // 濡傛灉鍒囨崲浜嗙浉鏈哄疄渚嬶紝涓旀湭寮�濮嬮噰闆嗭紝灏濊瘯寮�鍚噰闆�
+ if (Camera != null && !Camera.isGrabbing)
+ {
+ try { Camera.StartGrabbing(); } catch { }
+ }
+ }
+ }
+ else
+ {
+ Camera = null;
}
if (Camera == null)
@@ -244,12 +259,17 @@
{
times--;
if (TriggerSource == TriggerSource.Software)
+ {
Camera.GetImageWithSoftTrigger(out bitmap, timeout);
+ }
else
+ {
Camera.GetImage(out bitmap, timeout);
-
+ }
if (bitmap != null)
+ {
break;
+ }
else if (isRegrab)
{
Debug.WriteLine($"鍙栧浘澶辫触,閲嶆柊鍙栧浘,鍓╀綑娆℃暟:{times}");
@@ -257,8 +277,9 @@
} while (times > 0 && isRegrab);
if (isUpParams)
+ {
Camera.SetCamConfig(oriConfig);
-
+ }
OutputImage = bitmap;
}
@@ -281,14 +302,49 @@
{
Result = true;
Msg = "";
- OutputImage = null;
+ if (OutputImage != null)
+ {
+ if (OutputImage is HObject)
+ ((HObject)OutputImage).Dispose();
+ else if (OutputImage is Mat)
+ ((Mat)OutputImage).Dispose();
+ else if (OutputImage is Bitmap)
+ ((Bitmap)OutputImage).Dispose();
+ OutputImage = null;
+ }
if (Record != null)
+ {
Record.Dispose();
+ Record = null;
+ }
}
public override void Dispose()
{
+ if (InputImage != null)
+ {
+ if (InputImage is HObject)
+ ((HObject)InputImage).Dispose();
+ else if (InputImage is Mat)
+ ((Mat)InputImage).Dispose();
+ else if (InputImage is Bitmap)
+ ((Bitmap)InputImage).Dispose();
+
+ InputImage = null;
+ }
+
+ if (OutputImage != null)
+ {
+ if (OutputImage is HObject)
+ ((HObject)OutputImage).Dispose();
+ else if (OutputImage is Mat)
+ ((Mat)OutputImage).Dispose();
+ else if (OutputImage is Bitmap)
+ ((Bitmap)OutputImage).Dispose();
+
+ OutputImage = null;
+ }
return;
}
@@ -302,7 +358,9 @@
if (OutputImage != null)
{
if (OutputImage is Bitmap)
+ {
obj.OutputImage = ((Bitmap)OutputImage).Clone();
+ }
}
return obj;
--
Gitblit v1.9.3