-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwindow.cpp
143 lines (120 loc) · 3.92 KB
/
mainwindow.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = NULL;
// 父类为Widget时处理方式
// centre = new QWidget(this);
// this->setCentralWidget(centre);
//摄像头信息获取
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras) {
//if (cameraInfo.deviceName() == "/dev/video0") //Linux下选择方式
camera = new QCamera(cameraInfo);
qDebug()<<cameraInfo.deviceName();
}
//设置摄像头捕获模式
camera->setCaptureMode(QCamera::CaptureStillImage);
//图像回显
viewfinder = new QCameraViewfinder(this);
camera->setViewfinder(viewfinder);
this->setCentralWidget(viewfinder);
//QCameraImageCapture 是获取摄像头捕捉的图片 相关类
imageCapture = new QCameraImageCapture(camera);
//绑定 捕获图片 信号 和 处理图片槽函数
connect(imageCapture,SIGNAL(imageCaptured(int,QImage)),this,SLOT(takeImage(int,QImage)));
//启动摄像头
this->camera->start();
//网络服务器
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,10001);
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(nextConnectionSlot()));
}
MainWindow::~MainWindow()
{
delete ui; delete camera; delete viewfinder; delete imageCapture;
delete tcpServer;
if(timer!=NULL)
delete timer;
}
void MainWindow::nextConnectionSlot()
{
qDebug()<<"debug nextConnectionSlot ...";
if(!tcpServer->hasPendingConnections()){
return;
}
QTcpSocket *socket = tcpServer->nextPendingConnection();
vect.append(socket);
connect(socket,SIGNAL(readyRead()),this,SLOT(readyReadSlot()));
connect(socket,SIGNAL(disconnected()),\
this,SLOT(disConnectSlot()));
}
void MainWindow::readyReadSlot()
{
//准备 报头(HTTP 协议)
// qDebug()<<"socket:"<<socket->readAll();
QByteArray buf = "HTTP/1.0 200 OK\r\n"
"Connection: Keep-Alive\r\n"
"Server: Network camera\r\n"
"Cache-Control: no-cachenno-store, must-revalidate, pre-check=0, "
"max-age=0\r\n"
"Pragma:no-cache\r\n"
"Content-Type:multipart/x-mixed-replace\r\n";
//获取是哪个客户端请求视频,然后发回报头
QTcpSocket *socket = (QTcpSocket*)sender();
socket->write(buf);
//启动定时器,用来触发QCameraImageCapture 捕获图片
timer = new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(timerSlot()));
timer->start(200);
}
void MainWindow::takeImage(int, QImage image)
{
image = image.scaled(450,360);
//准备 帧头
// QString str = "\r\n--Guoqi\r\n"
// "Content-Type:image/jpeg\n"
// "Content-Length:%1\n\n";
QString str = "\r\n--\n\n";
// str = str.arg(image.byteCount());
QByteArray buf;
buf.clear();
buf.append(str);
//将图片存入 缓存中
QPixmap pic = QPixmap::fromImage(image);
QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
pic.save(&buffer,"JPEG");
//将缓存 读入 网络字节流后 发出
QByteArray dataStr;
dataStr.append(buffer.data());
// qDebug()<<"dataStr(size):"<<dataStr.size();
buf.append(dataStr);
//
// qDebug()<<buf.size();
for(int i=0;i<vect.size();i++)
{
vect.at(i)->write(buf);
}
// qDebug()<<buf;
}
void MainWindow::timerSlot()
{
//上锁
camera->searchAndLock();
//捕获图片
imageCapture->capture(QString("tmp_image"));
//解锁
camera->unlock();
}
//释放下线的客户端
void MainWindow::disConnectSlot()
{
QTcpSocket *socket = (QTcpSocket*)sender();
vect.removeOne(socket);
// delete socket;
// socket = NULL;
}