Skip to content

Commit 0cad002

Browse files
committed
优化代码
1 parent 4d643c1 commit 0cad002

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/Galaxy/include/GalaxyCamera.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ class GxCamera {
4343
int64_t _payload{};
4444
cv::Mat _map1{};
4545
cv::Mat _map2{};
46+
47+
static int _instanceNum;
4648
};
4749
} // namespace D5R

lib/Galaxy/src/GalaxyCamera.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace D5R {
1515

16+
int GxCamera::_instanceNum = 0;
17+
1618
/**
1719
* @brief Construct a new Gx Camera:: Gx Camera object
1820
*
@@ -37,10 +39,14 @@ const char *GxCamera::GetGxError() {
3739
*
3840
*/
3941
void GxCamera::Init() {
42+
GX_STATUS status;
43+
4044
// 初始化Lib
41-
auto status = GXInitLib();
42-
if (status != GX_STATUS_SUCCESS) {
43-
throw RobotException(ErrorCode::CameraInitError, "In GxCamera::Init, Failed to init the lib, error: " + std::string(GetGxError()));
45+
if (GxCamera::_instanceNum++ == 0) { // 若 _instanceNum != 0, 代表已经 InitLib 则跳过这一步
46+
status = GXInitLib();
47+
if (status != GX_STATUS_SUCCESS) {
48+
throw RobotException(ErrorCode::CameraInitError, "In GxCamera::Init, Failed to init the lib, error: " + std::string(GetGxError()));
49+
}
4450
}
4551
// 枚举相机
4652
uint32_t nums{};
@@ -209,7 +215,10 @@ void GxCamera::Release() {
209215
if (status != GX_STATUS_SUCCESS) {
210216
ERROR_("Failed to close camera, error: %s", GetGxError());
211217
}
212-
GXCloseLib();
218+
219+
if (--GxCamera::_instanceNum == 0) { // 如果全部相机都已注销,则 CloseLib
220+
GXCloseLib();
221+
}
213222
}
214223

215224
GxCamera::~GxCamera() { Release(); }

0 commit comments

Comments
 (0)