帮我写一个 qt+opencv的录屏软件 分辨率可选的,帧率30,视频格式mp4 的程序代码
时间: 2024-04-29 10:25:19 浏览: 7
好的,以下是程序代码:
```cpp
#include <QCoreApplication>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Create GUI elements
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *buttonLayout = new QHBoxLayout;
QLabel *resolutionLabel = new QLabel("Resolution:");
QLabel *fpsLabel = new QLabel("FPS:");
QComboBox *resolutionComboBox = new QComboBox;
QComboBox *fpsComboBox = new QComboBox;
QPushButton *startButton = new QPushButton("Start");
QPushButton *stopButton = new QPushButton("Stop");
// Add options to resolution and fps comboboxes
resolutionComboBox->addItem("320x240");
resolutionComboBox->addItem("640x480");
resolutionComboBox->addItem("1280x720");
resolutionComboBox->addItem("1920x1080");
fpsComboBox->addItem("15");
fpsComboBox->addItem("25");
fpsComboBox->addItem("30");
fpsComboBox->addItem("60");
// Add GUI elements to layout
buttonLayout->addWidget(startButton);
buttonLayout->addWidget(stopButton);
layout->addWidget(resolutionLabel);
layout->addWidget(resolutionComboBox);
layout->addWidget(fpsLabel);
layout->addWidget(fpsComboBox);
layout->addLayout(buttonLayout);
widget->setLayout(layout);
// Show the GUI
widget->show();
// Set up video capture
VideoCapture cap(0);
int frame_width = 0, frame_height = 0;
double fps = 0;
VideoWriter writer;
// Connect start button
QObject::connect(startButton, &QPushButton::clicked, [&]() {
// Get selected resolution and fps
QString resolutionText = resolutionComboBox->currentText();
QString fpsText = fpsComboBox->currentText();
// Parse resolution text
QStringList resolutionList = resolutionText.split("x");
if (resolutionList.size() != 2) {
QMessageBox::warning(widget, "Error", "Invalid resolution");
return;
}
frame_width = resolutionList[0].toInt();
frame_height = resolutionList[1].toInt();
// Parse fps text
fps = fpsText.toDouble();
if (fps <= 0) {
QMessageBox::warning(widget, "Error", "Invalid FPS");
return;
}
// Open video writer
QString fileName = QFileDialog::getSaveFileName(widget, "Save video", "", "MP4 (*.mp4)");
if (fileName.isEmpty()) {
return;
}
writer.open(fileName.toStdString(), cv::VideoWriter::fourcc('m', 'p', '4', 'v'), fps, cv::Size(frame_width, frame_height), true);
if (!writer.isOpened()) {
QMessageBox::warning(widget, "Error", "Failed to open video writer");
return;
}
// Start capturing frames
Mat frame;
cap.set(CAP_PROP_FRAME_WIDTH, frame_width);
cap.set(CAP_PROP_FRAME_HEIGHT, frame_height);
cap.set(CAP_PROP_FPS, fps);
while (true) {
cap.read(frame);
if (frame.empty()) {
break;
}
writer.write(frame);
imshow("Recording", frame);
if (waitKey(1) == 27) {
break;
}
}
// Release resources
cap.release();
writer.release();
destroyAllWindows();
});
// Connect stop button
QObject::connect(stopButton, &QPushButton::clicked, [&]() {
cap.release();
writer.release();
destroyAllWindows();
});
return a.exec();
}
```
希望能够帮助到你!