qt opencv 录屏代码
时间: 2023-11-08 15:10:23 浏览: 88
opencv 代码
可以参考以下代码:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
using namespace cv;
using namespace std;
int main() {
// 获取当前时间
auto now = chrono::system_clock::now();
auto now_c = chrono::system_clock::to_time_t(now);
string path = "screen_capture_" + to_string(now_c) + ".avi";
// 获取屏幕分辨率
int screen_width = GetSystemMetrics(SM_CXSCREEN);
int screen_height = GetSystemMetrics(SM_CYSCREEN);
// 创建视频编码器
VideoWriter writer(path, CV_FOURCC('M', 'J', 'P', 'G'), 30, Size(screen_width, screen_height));
if (!writer.isOpened()) {
cout << "无法打开视频文件" << endl;
return -1;
}
// 录屏
Mat frame;
HDC hScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, screen_width, screen_height);
HGDIOBJ hOld = SelectObject(hDC, hBitmap);
while (true) {
if (!BitBlt(hDC, 0, 0, screen_width, screen_height, hScreen, 0, 0, SRCCOPY)) {
cout << "无法复制位图" << endl;
return -1;
}
frame.create(screen_height, screen_width, CV_8UC4);
GetBitmapBits(hBitmap, screen_width * screen_height * 4, frame.data);
writer.write(frame);
if (waitKey(1) == 27) {
break;
}
}
// 释放资源
SelectObject(hDC, hOld);
DeleteObject(hBitmap);
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);
return 0;
}
```
希望能对您有所帮助。
阅读全文