-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideostreamer.cpp
92 lines (66 loc) · 2.36 KB
/
videostreamer.cpp
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
#include "videostreamer.h"
#include "ui_videostreamer.h"
#include <QPainter>
#include <QDebug>
#include <QByteArray>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
VideoStreamer::VideoStreamer(v4l2_device_param param, bool mainCamera, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::VideoStreamer),
_capture(new V4L2Device(param))
{
ui->setupUi(this);
_width = (int) _capture->getWidth();
_height = (int) _capture->getHeight();
_stride = (int) _capture->getStride();
if (mainCamera) {
_capture->setCallback([&](const Buffer& buffer, const struct v4l2_buffer& buffer_info) {
QImage img = QImage(_width, _height, QImage::Format_RGB888);
unsigned char *rgb_data = img.bits();
v4lconvert_yuyv_to_rgb24((const unsigned char*) buffer.data, rgb_data, _width, _height, _stride);
emit renderedImage(img);
});
} else {
_capture->setCallback([&](const Buffer& buffer, const struct v4l2_buffer& buffer_info) {
cv::Mat bayer8 = cv::Mat(_height, _width, CV_8UC1, (uchar*) buffer.data);
cv::Mat rgb8 = cv::Mat(_height, _width, CV_8UC3);
cv::cvtColor(bayer8, rgb8, CV_BayerGB2RGB);
cv::Mat result = cv::Mat(_height, _width, CV_8UC3);
cv::cvtColor(rgb8, result, CV_BGR2RGB);
QImage img = QImage((const uchar*) result.data, result.cols,
result.rows, result.step1(), QImage::Format_RGB888);
img.bits();
emit renderedImage(img);
});
}
// connect rendered image signal to set picture slot
connect(this, SIGNAL(renderedImage(QImage)), this, SLOT(setPicture(QImage)));
setAutoFillBackground(true);
}
void VideoStreamer::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setPen(Qt::white);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
painter.drawPixmap(this->rect(), pixmap);
}
void VideoStreamer::setPicture(const QImage& image) {
pixmap = QPixmap::fromImage(image);
update();
qApp->processEvents();
}
VideoStreamer::~VideoStreamer()
{
delete ui;
}
void VideoStreamer::on_streamButton_clicked()
{
_capture->changeState();
if (_capture->isCapturing()) {
ui->streamButton->setText("Stop");
} else {
ui->streamButton->setText("Start");
}
}