-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframe_capture.cpp
More file actions
58 lines (49 loc) · 1.26 KB
/
frame_capture.cpp
File metadata and controls
58 lines (49 loc) · 1.26 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
//
// Created by pengx on 2026/1/9.
//
#include "frame_capture.hpp"
#include "base_config.hpp"
#include <QDebug>
FrameCapture::FrameCapture(const int index) {
_index = index;
// 注册用于跨线程 queued signal 的类型
qRegisterMetaType<cv::Mat>("cv::Mat");
}
void FrameCapture::start() {
if (!_cap.open(0, cv::CAP_V4L2)) {
emit errorOccurred("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);
_isRunning = true;
_timerPtr = std::make_unique<QTimer>(this);
connect(_timerPtr.get(), &QTimer::timeout, this, &FrameCapture::captureOnce);
_timerPtr->start(40);
}
void FrameCapture::captureOnce() {
if (!_isRunning.load()) {
qWarning() << "FrameCapture::captureOnce() called when not running";
return;
}
cv::Mat frame;
_cap >> frame;
if (!frame.empty()) {
emit frameCaptured(frame);
}
}
void FrameCapture::stop() {
_isRunning = false;
if (_timerPtr) {
_timerPtr->stop();
_timerPtr.reset();
}
if (_cap.isOpened()) {
_cap.release();
}
}
FrameCapture::~FrameCapture() {
stop();
}