用qt+opencv, 写一个电脑录屏的示例代码, 保存格式 h264
时间: 2024-06-10 21:08:27 浏览: 146
可以的,以下是示例代码:
```
#include "opencv2/opencv.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/video.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <chrono>
#include <ctime>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened())
{
std::cout << "Cannot open Camera" << std::endl;
return -1;
}
int frame_width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));
int frame_height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));
VideoWriter writer;
double fps = 25.0;
int codec = VideoWriter::fourcc('H', '2', '6', '4');
writer.open("output.h264", codec, fps, Size(frame_width, frame_height), true);
if (!writer.isOpened())
{
std::cout << "Cannot open file" << std::endl;
return -1;
}
std::cout << "Press q to stop recording" << std::endl;
while (true)
{
Mat frame;
cap >> frame;
writer << frame;
imshow("Frame", frame);
char c = static_cast<char>(waitKey(1));
if (c == 'q' || c == 'Q')
break;
};
cap.release();
writer.release();
destroyAllWindows();
std::cout << "Recording complete" << std::endl;
return 0;
}
```
这是一个使用OpenCV和Qt编写的电脑录屏示例程序,可以将摄像头捕获的视频流编码为H.264格式并保存到本地文件。希望这个示例程序可以帮助你实现你的目标。
阅读全文