服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - OpenCV实现简单录屏功能

OpenCV实现简单录屏功能

2022-08-30 13:30蓬 蒿 人 C/C++

这篇文章主要为大家详细介绍了OpenCV实现简单录屏功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了OpenCV实现简单录屏功能的具体代码,供大家参考,具体内容如下

OpenCV中VideoCapture和VideoWriter用于读写视频文件,这里的录屏功能用到VideoWriter,用于将捕获的屏幕的每一帧数据保存到视频文件。

VideoWriter写视频文件的步骤

1、bool open(const String& filename, int fourcc, double fps,Size frameSize, bool isColor = true);
2、void write(InputArray image);或者VideoWriter& operator << (const Mat& image);
3、void release();

下列代码用于获取屏幕的截图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
HDC hdcScreen = GetDC(NULL);
HDC hdcMemDC = CreateCompatibleDC(hdcScreen);
HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen, width, height);
 
BITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = width;
bi.bmiHeader.biHeight = height;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = 0;
bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
SelectObject(hdcMemDC, hbmScreen);
int lineBytes = ((width * bi.bmiHeader.biBitCount + 31) / 32) * 4;//每行字节数必须是4字节的整数倍
int bmpSize = lineBytes * height;
char* lpbitmap = new char[bmpSize];
BitBlt(hdcMemDC, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY);
GetDIBits(hdcMemDC, hbmScreen, 0, height, lpbitmap, &bi, DIB_RGB_COLORS);

lpbitmap为屏幕的像素颜色数据,下列代码将lpbitmap作为一帧写到视频中(假设VideoWriter为已正常打开的VideoWriter实例)

?
1
2
3
4
5
6
7
8
cv::Mat bmpMat(height, width, CV_8UC3);
for (int i = 0; i < height; i++)
{
    int srcIndex = (height-i-1) * lineBytes;
    int destIndex = i * width * 3;
    memcpy(&bmpMat.data[destIndex],&lpbitmap[srcIndex],width*3);
}
videoWriter.write(bmpMat);//或videoWriter << bmpMat;

因为lpbitmap中的数据是从左下角到右上角排列,而视频帧图像的数据是从左上角到右下角排列,所以要将数据按行上下翻转,即lpbitmap第一行对应视频图像的最后一行。另外BMP图像数据每行的字节数必须是4字节的整数倍,而写入视频的Mat数据没有这个要求,即每行数据大小是图像实际宽度乘以每个颜色占用的字节数,所以实际每行拷贝的数据是width*3节字。

下面是一段测试代码,这里只录制100帧,实际使用中可通过命令行参数、快捷键或按钮等自行决定开始和结束时间,帧率这里也设为固定的25,其实也应该根据具体形况设定合适的值。最后别忘了将opencv_ffmpegXXX.dll文件放到可执行文件目录下。

?
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
#include<windows.h>
#include"opencv2/opencv.hpp"
int main()
{
 
    cv::VideoWriter videoWriter;
    double fps = 25;
    int codec = cv::VideoWriter::fourcc('m', 'p', '4', 'v');
    int width =  GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);
 
    time_t seconds = time(0);
    int s = seconds % 60;
    int m = (seconds % 3600) / 60;
    int h = (seconds % (3600 * 24)) / 3600 + 8;
    char timeBuf[128] = { 0 };
    sprintf_s(timeBuf, "CaptureScreen-%d-%d-%d.mp4", h, m, s);
    cv::String filePath = timeBuf;
    videoWriter.open(filePath, codec, fps, cv::Size(width, height), true);
    if (!videoWriter.isOpened())
    {
        return -1;
    }
 
    HDC hdcScreen = GetDC(NULL);
    HDC hdcMemDC = CreateCompatibleDC(hdcScreen);
    HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen, width, height);
 
    BITMAPINFO bi;
    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biWidth = width;
    bi.bmiHeader.biHeight = height;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 24;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = 0;
    bi.bmiHeader.biXPelsPerMeter = 0;
    bi.bmiHeader.biYPelsPerMeter = 0;
    bi.bmiHeader.biClrUsed = 0;
    bi.bmiHeader.biClrImportant = 0;
    SelectObject(hdcMemDC, hbmScreen);
 
    int lineBytes = ((width * bi.bmiHeader.biBitCount + 31) / 32) * 4;
    int bmpSize = lineBytes * height;
    char* lpbitmap = new char[bmpSize];
    cv::Mat bmpMat(height, width, CV_8UC3);
    for (int i=0;i<100;i++)
    {
        if (BitBlt(hdcMemDC, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY))
        {
            GetDIBits(hdcMemDC, hbmScreen, 0, height, lpbitmap, &bi, DIB_RGB_COLORS);
            for (int i = 0; i < height; i++)
            {
                int srcIndex = (height-i-1) * lineBytes;
                int destIndex = i * width * 3;
                memcpy(&bmpMat.data[destIndex],&lpbitmap[srcIndex],width*3);
            }
            videoWriter.write(bmpMat);//videoWriter << bmpMat;
        }
    }
    delete[] lpbitmap;
    if (videoWriter.isOpened())
    {
        videoWriter.release();
    }
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yb0022/article/details/100893722

延伸 · 阅读

精彩推荐
  • C/C++C语言学籍管理系统源代码

    C语言学籍管理系统源代码

    这篇文章主要为大家详细介绍了C语言学籍管理系统源代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    唯一5842021-06-22
  • C/C++C语言光标旋转与倒计时功能实现示例详解

    C语言光标旋转与倒计时功能实现示例详解

    这篇文章主要为大家介绍了C语言实现光标旋转与倒计时功能的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪...

    鹿九丸11632022-03-02
  • C/C++深入讲解Socket原理

    深入讲解Socket原理

    这篇文章深入的讲解Socket原理,并附带实例代码。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    飞_哥10692022-03-08
  • C/C++vscode中配置LeetCode插件的教程(愉快刷题)

    vscode中配置LeetCode插件的教程(愉快刷题)

    这篇文章主要介绍了vscode中配置LeetCode插件的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要...

    TechFlow20199572021-08-29
  • C/C++一篇文章带你了解C++Primer学习日记--处理数据

    一篇文章带你了解C++Primer学习日记--处理数据

    今天小编就为大家分享一篇关于C++对数器的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    weixin_460766627412021-12-15
  • C/C++opencv实现三帧差法解析

    opencv实现三帧差法解析

    这篇文章主要介绍了opencv实现三帧差法的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiao__run10172021-08-27
  • C/C++C++ 常量成员常量返回值详解

    C++ 常量成员常量返回值详解

    这篇文章主要介绍了C++ 常量成员常量返回值详解,需要的朋友可以参考下...

    blues10219262021-05-14
  • C/C++C++实现井字棋游戏

    C++实现井字棋游戏

    这篇文章主要为大家详细介绍了C++实现井字棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    L...P4742021-09-18