| | |
| | | /// </summary> |
| | | private void GrabThreadProc() |
| | | { |
| | | IMVDefine.IMV_Frame frame = new IMVDefine.IMV_Frame(); |
| | | |
| | | while (_threadRunning) |
| | | { |
| | | IMVDefine.IMV_Frame frame = new IMVDefine.IMV_Frame(); |
| | | try |
| | | { |
| | | // 获取图像帧 |
| | | int result = _camera.IMV_GetFrame(ref frame, 100); |
| | | int result = _camera.IMV_GetFrame(ref frame, 1000); |
| | | |
| | | if (result == IMVDefine.IMV_OK) |
| | | { |
| | | // 处理图像帧 |
| | | ProcessFrame(frame); |
| | | } |
| | | else if (result != 0x80000001) // 超时错误代码 |
| | | else |
| | | { |
| | | // 非超时错误 |
| | | System.Diagnostics.Debug.WriteLine($"获取图像帧失败,错误码:{result}"); |
| | | // 即使获取失败,也尝试释放帧,防止SDK内部缓存泄露 |
| | | // 注意:frame是每次新建的,如果GetFrame没填充,这里释放应该是安全的(视SDK实现而定) |
| | | var tempFrame = frame; |
| | | _camera.IMV_ReleaseFrame(ref tempFrame); |
| | | |
| | | if ((uint)result != 0x80000001 && result != -119 && result != -102) // 超时错误代码 |
| | | { |
| | | // 非超时错误 |
| | | System.Diagnostics.Debug.WriteLine($"获取图像帧失败,错误码:{result}"); |
| | | Thread.Sleep(10); // 出错时稍作等待 |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.Diagnostics.Debug.WriteLine($"采集线程异常:{ex.Message}"); |
| | | Thread.Sleep(10); |
| | | } |
| | | |
| | | Thread.Sleep(1); |
| | |
| | | // 更新回调图像 |
| | | CallBackImg = bitmap; |
| | | } |
| | | |
| | | // 释放帧 |
| | | var tempFrame = frame; |
| | | _camera.IMV_ReleaseFrame(ref tempFrame); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.Diagnostics.Debug.WriteLine($"处理图像帧失败:{ex.Message}"); |
| | | } |
| | | finally |
| | | { |
| | | // 确保无论如何都释放帧,防止缓存占满 |
| | | var tempFrame = frame; |
| | | _camera.IMV_ReleaseFrame(ref tempFrame); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |