-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_sdk.cpp
More file actions
159 lines (138 loc) · 4.5 KB
/
Copy pathcamera_sdk.cpp
File metadata and controls
159 lines (138 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "camera_sdk.h"
#include "camera_controller.h"
#include <iostream>
#include <new> // For std::bad_alloc
/**
* @file camera_sdk.cpp
* @brief 实现了 camera_sdk.h 中声明的 C 语言 API 接口。
*
* 这一层是“桥接层”,它将简单的 C 函数调用转换为对内部 C++ CameraController 对象的成员函数调用。
* 使用 void* handle (不透明指针) 是 C/C++ 混合编程中隐藏实现细节的标准做法。
*/
extern "C"
{
void *camera_sdk_create(const char *device_path)
{
if (!device_path || device_path[0] == '\0')
{
std::cerr << "SDK错误: 设备路径不能为空。" << std::endl;
return nullptr;
}
try
{
// 'new' 一个 C++ 控制器实例
CameraController *controller = new CameraController(device_path);
// 调用其初始化方法
if (!controller->initialize())
{
delete controller; // 初始化失败,清理内存并返回空指针
return nullptr;
}
// 将 C++ 对象指针强制转换为 void* 句柄返回给调用者
return static_cast<void *>(controller);
}
catch (const std::bad_alloc &e)
{
std::cerr << "SDK错误: 内存分配失败: " << e.what() << std::endl;
return nullptr;
}
}
void camera_sdk_destroy(void *handle)
{
if (handle)
{
// 将不透明句柄安全地转换回 C++ 控制器指针
CameraController *controller = static_cast<CameraController *>(handle);
// 'delete' C++ 对象,这将自动调用其析构函数,从而安全地释放所有资源
delete controller;
}
}
int camera_sdk_start_recording(void *handle, const char *resolution)
{
if (handle && resolution)
{
return static_cast<CameraController *>(handle)->start_recording(resolution);
}
return -1;
}
int camera_sdk_stop_recording(void *handle)
{
if (handle)
{
return static_cast<CameraController *>(handle)->stop_recording();
}
return -1;
}
int camera_sdk_start_rtsp_stream(void* handle, const char* url) {
if (handle && url) {
return static_cast<CameraController*>(handle)->start_rtsp_stream(url);
}
return -1;
}
int camera_sdk_stop_rtsp_stream(void* handle) {
if (handle) {
return static_cast<CameraController*>(handle)->stop_rtsp_stream();
}
return -1;
}
int camera_sdk_take_snapshot(void *handle)
{
if (handle)
{
return static_cast<CameraController *>(handle)->take_snapshot();
}
return -1;
}
void camera_sdk_set_osd_enabled(void *handle, bool enabled)
{
if (handle)
{
static_cast<CameraController *>(handle)->set_osd_enabled(enabled);
}
}
void camera_sdk_set_osd_data(void *handle, const camera_sdk_pos_data_t* data)
{
if (handle && data && data->timestamp)
{
// C++ OSD管理器需要的数据结构
OsdManager::PosData pos_data;
pos_data.latitude = data->latitude;
pos_data.longitude = data->longitude;
pos_data.speed_kmh = data->speed_kmh;
pos_data.timestamp = data->timestamp;
// 获取 OSD 管理器并设置数据
auto* controller = static_cast<CameraController*>(handle);
if (controller->get_osd_manager()) {
controller->get_osd_manager()->set_pos_data(pos_data);
}
}
}
void camera_sdk_zoom_in(void *handle)
{
if (handle)
{
static_cast<CameraController *>(handle)->zoom_in();
}
}
void camera_sdk_zoom_out(void *handle)
{
if (handle)
{
static_cast<CameraController *>(handle)->zoom_out();
}
}
void camera_sdk_set_iso(void* handle, int iso)
{
if (handle)
{
static_cast<CameraController*>(handle)->set_iso(iso);
}
}
void camera_sdk_set_ev(void* handle, double ev)
{
if (handle)
{
static_cast<CameraController*>(handle)->set_ev(ev);
}
}
} // extern "C"