forked from zhuohengfeng/OpenCVFFmpegRtmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXVideoCapture.cpp
More file actions
113 lines (91 loc) · 2.68 KB
/
XVideoCapture.cpp
File metadata and controls
113 lines (91 loc) · 2.68 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
//
// Created by hengfeng zhuo on 2019-08-05.
//
#include <opencv2/highgui.hpp>
#include "XVideoCapture.h"
#include "main.h"
using namespace cv;
class CXVideoCapture : public XVideoCapture {
public:
/**
* 打开本地摄像头
* @param camIndex
* @return
*/
bool Init(int camIndex = 0) override {
// 打开本地摄像头
cam.open(camIndex);
if (!cam.isOpened()) {
std::cout << "[Error] 打开摄像头失败" << std::endl;
return true;
}
// 得到本地相机参数
this->width = cam.get(CAP_PROP_FRAME_WIDTH);
this->height = cam.get(CAP_PROP_FRAME_HEIGHT);
this->fps = cam.get(CAP_PROP_FPS);
std::cout << "打开摄像头成功 width=" << width << ", height=" << height << ", fps=" << fps << std::endl;
if (fps <= 0) {
fps = 25;
}
return true;
}
/**
* 打开流媒体
* @param url
* @return
*/
bool Init(const char *url) override {
cam.open(url);
if (!cam.isOpened()) {
std::cout << "[Error] 打开流媒体失败" << url << std::endl;
return true;
}
// 得到流媒体的参数
this->width = cam.get(CAP_PROP_FRAME_WIDTH);
this->height = cam.get(CAP_PROP_FRAME_HEIGHT);
this->fps = cam.get(CAP_PROP_FPS);
std::cout << "打开流媒体成功 width=" << width << ", height=" << height << ", fps=" << fps << std::endl;
if (fps <= 0) {
fps = 25;
}
return true;
}
void Stop() override {
// 停止线程
XDataThread::Stop();
// 是否camera
if (cam.isOpened()) {
cam.release();
}
}
protected:
VideoCapture cam; // 本地摄像头
/**
* 这个是QThread的线程执行体
*/
void run() override {
std::cout << "----开始执行video capture线程----" << std::endl;
Mat frame;
while(!isExit) {
// 读取一帧
if (!cam.read(frame) || frame.empty()) {
msleep(1); // 如果没有读取到,等待1ms
continue;
}
// 生成一个XData
int size = frame.rows * frame.cols * frame.elemSize();
std::cout << "得到一帧数据, 大小=" << size << std::endl;
XData d((char*)frame.data, size);
Push(d);
}
std::cout << "---退出video capture线程----" << std::endl;
}
};
XVideoCapture *XVideoCapture::Get(unsigned char index) {
static CXVideoCapture xc[255];
return &xc[0];
}
XVideoCapture::XVideoCapture() {
}
XVideoCapture::~XVideoCapture() {
}