-
Notifications
You must be signed in to change notification settings - Fork 2
/
videocapture.cpp
241 lines (223 loc) · 7.47 KB
/
videocapture.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "videocapture.h"
#include "QtMultimedia/qmediacapturesession.h"
#include <QCameraDevice>
#include <QMediaDevices>
#include <QLayout>
#include <QEventLoop>
#include <QImageCapture>
VideoCapture::VideoCapture(QObject *parent)
: QObject(parent)
{
metaEnum = QMetaEnum::fromType<PixelFormat>();
// viewfinder = new QVideoWidget();
}
VideoCapture::~VideoCapture()
{
close();
if (viewfinder != Q_NULLPTR) {
viewfinder->deleteLater();
viewfinder = Q_NULLPTR;
}
}
//void VideoCapture::init(QLayout *layout)
//{
//// viewfinder = new QCameraViewfinder(layout->parentWidget());
//// layout->addWidget(viewfinder);
//}
void VideoCapture::open(int index, int cameraFormateIndex, int audioInputIndex)
{
close();
if (cameraList.count() <= 0 || index < 0 || index >= cameraList.count()) {
return;
}
camera = new QCamera(cameraList[index], this->parent());
QList<QCameraFormat> cameraFormats = cameraList[index].videoFormats();
if (cameraFormats.length() > 0 && cameraFormateIndex >= 0 && cameraFormateIndex < cameraFormats.length()) {
camera->setCameraFormat(cameraFormats[cameraFormateIndex]);
}
mediaCaptureSession = new QMediaCaptureSession(this->parent());
mediaCaptureSession->setCamera(camera);
//设置声音输入输出
if (audioInputList.count() > 0 && audioInputIndex >= 0 && audioInputIndex < audioInputList.count()) {
audioInput = new QAudioInput(audioInputList[audioInputIndex], this->parent());
audioOutput = new QAudioOutput(QMediaDevices::defaultAudioOutput(), this->parent());
mediaCaptureSession->setAudioInput(audioInput);
audioInput->setVolume(audioInputVolume / 100.0);
audioInput->setMuted(audioInputMute);
mediaCaptureSession->setAudioOutput(audioOutput);
}
//设置取景器
mediaCaptureSession->setVideoOutput(viewfinder);
//设置截图
imageCapture = new QImageCapture(this->parent());
mediaCaptureSession->setImageCapture(imageCapture);
connect(imageCapture, &QImageCapture::imageAvailable, this, &VideoCapture::imageAvailable);
connect(imageCapture,
QOverload<int, QImageCapture::Error, const QString &>::of(&QImageCapture::errorOccurred),
[=](int /*id*/, QImageCapture::Error /*error*/, const QString &errorString){
qDebug() << errorString;
});
//开启
camera->start();
}
void VideoCapture::close()
{
if (camera != Q_NULLPTR) {
camera->stop();
camera->deleteLater();
camera = Q_NULLPTR;
}
if (audioInput != Q_NULLPTR) {
audioInput->deleteLater();
audioInput = Q_NULLPTR;
}
if (audioOutput != Q_NULLPTR) {
audioOutput->deleteLater();
audioOutput = Q_NULLPTR;
}
if (imageCapture != Q_NULLPTR) {
imageCapture->deleteLater();
imageCapture = Q_NULLPTR;
}
renewViewFinder();
if (mediaCaptureSession != Q_NULLPTR) {
mediaCaptureSession->deleteLater();
mediaCaptureSession = Q_NULLPTR;
}
}
QStringList VideoCapture::refreshCamera(QString defaultSearch, QString &defaultName)
{
defaultName = "";
cameraList.clear();
cameraList = QMediaDevices::videoInputs();
QStringList cameraDes;
for (const QCameraDevice &cameraInfo : cameraList) {
if (defaultName.isEmpty() && cameraInfo.description().contains(defaultSearch)) {
defaultName = cameraInfo.description();
}
cameraDes << cameraInfo.description();
}
return cameraDes;
}
QStringList VideoCapture::refreshAudioInput(QString defaultSearch, QString &defaultName)
{
defaultName = "";
audioInputList.clear();
audioInputList = QMediaDevices::audioInputs();
QStringList audioInputDes;
for (const QAudioDevice &audioInfo : audioInputList) {
if (defaultName.isEmpty() && audioInfo.description().contains(defaultSearch)) {
defaultName = audioInfo.description();
}
audioInputDes << audioInfo.description();
}
return audioInputDes;
}
QStringList VideoCapture::GetCameraFormats(int index, int &defaultIndex, const QStringList &defaultSearch)
{
QStringList list;
QList<QCameraFormat> cameraFormats = cameraList[index].videoFormats();
for (int i = 0; i < cameraFormats.length(); i++) {
QCameraFormat cameraFormat = cameraFormats[i];
QString res = (QString::number(cameraFormat.resolution().width()) + "x" + QString::number(cameraFormat.resolution().height()));
QString frame = (QString::number(cameraFormat.minFrameRate()) + "~" + QString::number(cameraFormat.maxFrameRate()));
QString format = metaEnum.key(cameraFormat.pixelFormat());
QString str = "resolution: " + res + ", frame: " + frame + ", pixel format: " + format;
list << str;
if (defaultIndex < 0 && defaultSearch.length() > 0) {
bool allFind = true;
for (const QString &_defaultSearch : defaultSearch) {
if (!str.contains(_defaultSearch)) {
allFind = false;
break;
}
}
if (allFind) {
defaultIndex = i;
}
}
}
return list;
}
void VideoCapture::moveViewfinder(QLayout *layout)
{
removeViewfinder();
if (viewfinder != Q_NULLPTR) {
layout->addWidget(viewfinder);
}
}
void VideoCapture::removeViewfinder()
{
if (viewfinder != Q_NULLPTR) {
QWidget *parent = (QWidget*)viewfinder->parent();
if (parent != Q_NULLPTR) {
parent->layout()->removeWidget(viewfinder);
}
}
}
const QVideoWidget *VideoCapture::getViewfinder()
{
return viewfinder;
}
QImage *VideoCapture::capture()
{
if (mediaCaptureSession != Q_NULLPTR && imageCapture != Q_NULLPTR && camera != Q_NULLPTR) {
QEventLoop* eventLoop = new QEventLoop();
connect(imageCapture, &QImageCapture::imageAvailable, eventLoop, &QEventLoop::quit);
imageCapture->capture();
eventLoop->exec();
disconnect(imageCapture, &QImageCapture::imageAvailable, eventLoop, &QEventLoop::quit);
eventLoop->deleteLater();
return videoFrame;
}
return Q_NULLPTR;
}
void VideoCapture::setAudioInputVolume(int volume)
{
audioInputVolume = volume;
if (audioInput != Q_NULLPTR) {
audioInput->setVolume(audioInputVolume / 100.0);
}
}
void VideoCapture::setAudioInputMute(bool mute)
{
audioInputMute = mute;
if (audioInput != Q_NULLPTR) {
audioInput->setMuted(audioInputMute);
}
}
void VideoCapture::stopViewfinder(bool b)
{
if (mediaCaptureSession != Q_NULLPTR && viewfinder != Q_NULLPTR) {
if (b) {
mediaCaptureSession->setVideoOutput(Q_NULLPTR);
viewfinder->hide();
} else {
renewViewFinder();
mediaCaptureSession->setVideoOutput(viewfinder);
viewfinder->show();
}
}
}
void VideoCapture::imageAvailable(int /*id*/, const QVideoFrame &frame)
{
// frame.image().save("123.jpg");
if (videoFrame != Q_NULLPTR) {
delete videoFrame;
videoFrame = Q_NULLPTR;
}
videoFrame = new QImage(frame.toImage());
}
void VideoCapture::renewViewFinder()
{
QWidget *viewfinderParent = Q_NULLPTR;
if (viewfinder != Q_NULLPTR) {
viewfinderParent = (QWidget*)viewfinder->parent();
removeViewfinder();
viewfinder->deleteLater();
}
viewfinder = new QVideoWidget();
if (viewfinderParent != Q_NULLPTR) {
moveViewfinder(viewfinderParent->layout());
}
}