C3032
2026-03-23 68a4b459eeb18effb8b3096add3d88c15629ab69
LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs
@@ -59,15 +59,40 @@
            try
            {
                // 先进行一次枚举,确保 SDK 能够发现设备(部分 SDK 要求在打开前必须执行枚举)
                GetDeviceList();
                // 如果之前失败过,重新创建相机对象确保干净状态
                if (_camera == null)
                {
                    _camera = new EidCamera();
                }
                // 确保之前的句柄已释放
                try { _camera.eidReleaseHandle_Net(); } catch { }
                // 创建句柄
                int nRet = _camera.eidCreateDevice_Net(sn, EidCamera.EidDeviceDataType.eidDeviceDataTypeSN);
                if (nRet != EidCamera.eidErrorOK) return false;
                if (nRet != EidCamera.eidErrorOK)
                {
                    _camera = null;
                    return false;
                }
                // 分配固定句柄给委托,防止被 GC 回收
                if (!_callbackHandle.IsAllocated)
                {
                    _callbackHandle = GCHandle.Alloc(_frameCallback);
                }
                // 打开设备
                nRet = _camera.eidOpenDevice_Net();
                if (nRet != EidCamera.eidErrorOK)
                {
                    _camera.eidReleaseHandle_Net();
                    if (_callbackHandle.IsAllocated) _callbackHandle.Free();
                    _camera = null;
                    return false;
                }
@@ -77,6 +102,8 @@
                {
                    _camera.eidCloseDevice_Net();
                    _camera.eidReleaseHandle_Net();
                    if (_callbackHandle.IsAllocated) _callbackHandle.Free();
                    _camera = null;
                    return false;
                }
@@ -86,6 +113,12 @@
            }
            catch
            {
                // 发生异常时确保释放资源
                try { _camera?.eidCloseDevice_Net(); } catch { }
                try { _camera?.eidReleaseHandle_Net(); } catch { }
                if (_callbackHandle.IsAllocated) _callbackHandle.Free();
                // 发生异常时释放相机对象,确保下次创建新实例
                _camera = null;
                return false;
            }
        }
@@ -96,11 +129,28 @@
            try
            {
                StopGrabbing();
                if (IsConnected)
                {
                    StopGrabbing();
                _camera.eidCloseDevice_Net();
                _camera.eidReleaseHandle_Net();
                    _camera?.eidCloseDevice_Net();
                }
                // 无论是否连接,都尝试释放句柄
                _camera?.eidReleaseHandle_Net();
                // 释放回调句柄
                if (_callbackHandle.IsAllocated)
                {
                    _callbackHandle.Free();
                }
                // 释放相机对象引用,确保SDK资源完全释放
                _camera = null;
                this.IsConnected = false;
                this.SN = string.Empty;
                this.IsGrabbing = false;
                return true;
            }
            catch
@@ -166,7 +216,7 @@
        {
            try
            {
                List<string> barcodes = new List<string>();
                List<BarcodeInfo> barcodeInfos = new List<BarcodeInfo>();
                // 解析条码
                for (int i = 0; i < frameInfo.codeNum; i++)
                {
@@ -177,11 +227,23 @@
                    string data = Marshal.PtrToStringAnsi(codeInfo.data);
                    if (!string.IsNullOrEmpty(data))
                    {
                        barcodes.Add(data);
                        BarcodeInfo info = new BarcodeInfo
                        {
                            Text = data,
                            Points = new Point[]
                            {
                                new Point((int)codeInfo.position[0].x, (int)codeInfo.position[0].y),
                                new Point((int)codeInfo.position[1].x, (int)codeInfo.position[1].y),
                                new Point((int)codeInfo.position[2].x, (int)codeInfo.position[2].y),
                                new Point((int)codeInfo.position[3].x, (int)codeInfo.position[3].y)
                            }
                        };
                        barcodeInfos.Add(info);
                    }
                }
                // 转换图像 (如果需要)
                // 转换图像
                Bitmap bitmap = null;
                if (frameInfo.imageDataLen > 0 && frameInfo.imageData != IntPtr.Zero)
                {
@@ -199,7 +261,7 @@
                }
                // 触发事件
                OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodes, bitmap));
                OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodeInfos, bitmap));
            }
            catch (Exception ex)
            {