第10课 利用windows API捕获桌面图像并通过FFmpeg分享

在上一章,我们已经实现了一对一音视频对话功能。在实际应用中,我们常需要把自己的电脑桌面分享给他人以实现桌面共享功能,这种功能在视频会议、在线教学等场景中很常见,这种功能如何实现呢?这节课我们就来解决这个问题。

1.备份demo9并修改demo9为demo10。

2.在fmle.cpp的init函数中添加捕获屏幕的线程:

复制代码
//capCamHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)capCamThread, (LPVOID)this, 0, NULL);
capScrHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)capScrThread, (LPVOID)this, 0, NULL);

3.添加捕获桌面的功能:

cpp 复制代码
cv::Mat hwndToMat(HWND hwnd)
{	
	HDC hwindowDC, hwindowCompatibleDC;
	int height, width, srcheight, srcwidth;
	HBITMAP hbwindow;
	cv::Mat src;
	BITMAPINFOHEADER  bi;
	hwindowDC = GetDC(hwnd);
	hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
	SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
	RECT windowsize; 
	GetClientRect(hwnd, &windowsize);

	srcheight = windowsize.bottom;
	srcwidth = windowsize.right;
	height = windowsize.bottom;
	width = windowsize.right;

	src.create(height, width, CV_8UC4);
	
	hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = width;
	bi.biHeight = -height;
	bi.biPlanes = 1;
	bi.biBitCount = 32;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrUsed = 0;
	bi.biClrImportant = 0;	
	SelectObject(hwindowCompatibleDC, hbwindow);	
	StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY | CAPTUREBLT);
	GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 	
	DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC);

	return src;
}


int fmle::capScr(){
	
	HWND hwnd = GetDesktopWindow();
	cv::Mat scrMat;
	while (true)
	{
		scrMat = hwndToMat(hwnd);		
		if (scrMat.cols != backWidth || scrMat.rows != backHeight){
			resize(scrMat, scrMat, cv::Size(backWidth, backHeight));
		}

		if (scrMat.data&&!scrMat.empty()){		
			cvtColor(scrMat, scrMat, CV_BGRA2BGR);
			mainDlg->drawMatOfPub(scrMat);
			EnterCriticalSection(&videoQueLock);
			tmpVideoQueObj.type = 1;
			tmpVideoQueObj.tmpMat = scrMat;
			tmpVideoQueObj.dataLen = scrMat.cols*scrMat.rows * 3;
			inVideoQue.push(tmpVideoQueObj);
			if (inVideoQue.size() >videoDataArrNum){
				inVideoQue.front().dataLen = 0;
				inVideoQue.front().tmpMat.release();
				inVideoQue.front().dataLen = NULL;
				inVideoQue.pop();
			}
			LeaveCriticalSection(&videoQueLock);

		}
		Sleep(40);
	}

	scrMat.release();	
	return 0;
}

4.调试运行,如能正常发送和播放桌面流则表示成功。

相关推荐
__lost7 分钟前
Python图像变清晰与锐化,调整对比度,高斯滤波除躁,卷积锐化,中值滤波钝化,神经网络变清晰
python·opencv·计算机视觉
pystraf9 分钟前
UOJ 228 基础数据结构练习题 Solution
数据结构·c++·算法·线段树
牙痛不能吃糖,哭14 分钟前
C++面试复习日记(8)2025.4.25,malloc,free和new,delete的区别
开发语言·c++
岫珩16 分钟前
“由于启动计算机时出现了页面文件配置问题,Windows在你的计算机上创建了一个临时页面文件。。。”的问题解决
windows
豆豆42 分钟前
day32 学习笔记
图像处理·笔记·opencv·学习·计算机视觉
李菠菜1 小时前
解决Windows系统下Git克隆时报错“unable to checkout working tree”的方法详解
windows·git
ChoSeitaku1 小时前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
软行2 小时前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
小贾要学习2 小时前
【C++】继承----下篇
android·java·c++
未来可期LJ2 小时前
【Test】单例模式❗
开发语言·c++