C3204
2025-12-31 596389540a602329116956dc7a7c8dfa53cda659
LB_VisionProcesses/Cameras/CameraConfig.cs
@@ -1,6 +1,9 @@
using Newtonsoft.Json;
using 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;