第三步,实现获取视频数据的功能。
C:\Users\Minwei\Projects\Qt\Monitor\monitordialog.h:
xxxxxxxxxx
261...
2
3
4
5
6...
7class MonitorDialog : public QDialog
8{
9 ...
10signals:
11 void recognize(QImage const& frame);
12 ...
13private slots:
14 ...
15 void on_m_streamReply_readyRead();
16 ...
17private:
18 ...
19 QNetworkAccessManager* m_access;
20 QNetworkReply* m_streamReply;
21 QByteArray m_streamData;
22 Face* m_face;
23 QThread* m_thread;
24 ...
25};
26...
C:\Users\Minwei\Projects\Qt\Monitor\monitordialog.cpp:
xxxxxxxxxx
621...
2
3...
4MonitorDialog::MonitorDialog(QWidget *parent)
5 ...
6 , m_access(new QNetworkAccessManager(this))
7 , m_streamReply(Q_NULLPTR)
8 , m_face(new Face)
9 , m_thread(new QThread(this))
10 ...
11{
12 ...
13 connect(this, SIGNAL(recognize(QImage const&)),
14 m_face, SLOT(recognize(QImage const&)));
15 m_face->moveToThread(m_thread);
16 m_thread->start();
17 ...
18}
19
20MonitorDialog::~MonitorDialog()
21{
22 ...
23 m_thread->terminate();
24 m_thread->wait();
25 ...
26}
27
28void MonitorDialog::on_m_btnStream_clicked()
29{
30 m_streamReply = m_access->get(QNetworkRequest(QUrl(
31 "http://192.168.0.111:8080?action=stream")));
32
33 connect(m_streamReply, SIGNAL(readyRead()),
34 this, SLOT(on_m_streamReply_readyRead()));
35}
36...
37void MonitorDialog::on_m_streamReply_readyRead()
38{
39 m_streamData += m_streamReply->readAll();
40
41 int begin = m_streamData.indexOf("\xff\xd8", 0);
42 if (begin == -1)
43 {
44 m_streamData.clear();
45 return;
46 }
47 int end = m_streamData.indexOf("\xff\xd9", begin + 2);
48 if (end == -1)
49 return;
50
51 QImage frame;
52 if (!frame.loadFromData(m_streamData.mid(begin, end + 2), "JPG"))
53 {
54 m_streamData.clear();
55 return;
56 }
57
58 m_streamData.clear();
59 ui->m_labStream->setPixmap(QPixmap::fromImage(frame));
60 emit recognize(frame);
61}
62...
运行效果如图所示: