| | |
| | | |
| | | 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; |
| | | } |
| | | |
| | |
| | | { |
| | | _camera.eidCloseDevice_Net(); |
| | | _camera.eidReleaseHandle_Net(); |
| | | if (_callbackHandle.IsAllocated) _callbackHandle.Free(); |
| | | _camera = null; |
| | | return false; |
| | | } |
| | | |
| | |
| | | } |
| | | catch |
| | | { |
| | | // 发生异常时确保释放资源 |
| | | try { _camera?.eidCloseDevice_Net(); } catch { } |
| | | try { _camera?.eidReleaseHandle_Net(); } catch { } |
| | | if (_callbackHandle.IsAllocated) _callbackHandle.Free(); |
| | | // 发生异常时释放相机对象,确保下次创建新实例 |
| | | _camera = null; |
| | | return false; |
| | | } |
| | | } |
| | |
| | | |
| | | 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 |
| | |
| | | try |
| | | { |
| | | List<string> barcodes = new List<string>(); |
| | | List<BarcodeInfo> barcodeInfos = new List<BarcodeInfo>(); |
| | | // 解析条码 |
| | | for (int i = 0; i < frameInfo.codeNum; i++) |
| | | { |
| | |
| | | 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) |
| | | { |
| | |
| | | |
| | | // 触发事件 |
| | | OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodes, bitmap)); |
| | | OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodeInfos, bitmap)); |
| | | } |
| | | catch (Exception ex) |
| | | { |