-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_capture.cpp
More file actions
68 lines (58 loc) · 1.74 KB
/
frame_capture.cpp
File metadata and controls
68 lines (58 loc) · 1.74 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
//
// Created by pengx on 2026/2/10.
//
#include "frame_capture.hpp"
#include "base_config.hpp"
#include <chrono>
FrameCapture::FrameCapture(const int index, const CameraErrorCallback& error_callback,
const CameraFrameCallback& frame_callback) {
_index = index;
_error_callback = error_callback;
_frame_callback = frame_callback;
}
void FrameCapture::start() {
if (!_cap.open(0, cv::CAP_V4L2)) {
_error_callback("Cannot open camera");
return;
}
// 设置参数
_cap.set(cv::CAP_PROP_FRAME_WIDTH, VIDEO_WIDTH);
_cap.set(cv::CAP_PROP_FRAME_HEIGHT, VIDEO_HEIGHT);
_cap.set(cv::CAP_PROP_FPS, VIDEO_FPS);
_is_running = true;
_thread_ptr = new std::thread(&FrameCapture::capture_loop, this);
}
void FrameCapture::capture_loop() {
// 约 40ms 间隔(25 FPS)
const auto frame_interval = std::chrono::milliseconds(40);
while (_is_running.load()) {
auto start_time = std::chrono::steady_clock::now();
{
cv::Mat frame;
_cap >> frame;
if (!frame.empty()) {
_frame_callback(frame);
}
}
// 计算实际执行时间,确保稳定帧率
auto elapsed = std::chrono::steady_clock::now() - start_time;
auto sleep_time = frame_interval - elapsed;
if (sleep_time > std::chrono::milliseconds(0)) {
std::this_thread::sleep_for(sleep_time);
}
}
}
void FrameCapture::stop() {
_is_running = false;
if (_thread_ptr && _thread_ptr->joinable()) {
_thread_ptr->join();
delete _thread_ptr;
_thread_ptr = nullptr;
}
if (_cap.isOpened()) {
_cap.release();
}
}
FrameCapture::~FrameCapture() {
stop();
}