windows下窗口辱揭、桌面捕捉有多種方式扁凛。如下:
1:使用GDI函數(shù)或者Windows Media API函數(shù)
2:使用DirectX技術(shù)
3:使用api hook技術(shù)(如D3D游戲捕捉)
4:使用圖形驅(qū)動(dòng)技術(shù)
obs的窗口、桌面捕捉在win-capture插件中裁赠。它使用到的是GDI技術(shù)殿漠。
相比于api hook或者圖形驅(qū)動(dòng)技術(shù),GDI在效率與性能方便確實(shí)略有差距佩捞。但確實(shí)不失為一種最簡(jiǎn)單的方法绞幌。
它的本質(zhì)就是抓取窗口或桌面快照(其實(shí)桌面也是一種特殊的窗口,也包含窗口句柄)
一忱。只要有句柄莲蜘,我們就可以得到了設(shè)備上下文(DC)谭确,就可以利用blit(復(fù)制)它的內(nèi)容到我們創(chuàng)建的DC中。
步驟如下:
1:上層枚舉窗口或桌面票渠,將選中的窗口名等屬性傳到此插件中逐哈。
static void update_settings(struct window_capture *wc, obs_data_t *s)
2:根據(jù)參數(shù)找到對(duì)應(yīng)的窗口或桌面。
HWND find_window(enum window_search_mode mode,
enum window_priority priority,
const char *class,
const char *title,
const char *exe)
3:libobs底層不斷調(diào)用static void wc_tick(void *data, float seconds)
來獲取數(shù)據(jù)
问顷,而此函數(shù)調(diào)用了如下函數(shù)
//創(chuàng)建一個(gè)設(shè)備兼容的DC
capture->hdc = CreateCompatibleDC(NULL);
//創(chuàng)建可以直接寫入的鞠眉、與設(shè)備無關(guān)的位圖(DIB)
capture->bmp = CreateDIBSection(capture->hdc, &bi, DIB_RGB_COLORS, (void**)&capture->bits, NULL, 0);
//將位圖選入到DC
capture->old_bmp = SelectObject(capture->hdc, capture->bmp);
//取得桌面窗口的DC
hdc_target = GetDC(window);
//將桌面窗口DC的圖象復(fù)制到兼容DC中
BitBlt(hdc, 0, 0, capture->width, capture->height,hdc_target, capture->x, capture->y, SRCCOPY);
ReleaseDC(NULL, hdc_target);
5:釋放創(chuàng)建的對(duì)象
SelectObject(capture->hdc, capture->old_bmp);
DeleteDC(capture->hdc);
DeleteObject(capture->bmp);
6:當(dāng)然很多時(shí)候需要捕捉鼠標(biāo),需要在此位圖上畫一個(gè)icon
static void draw_cursor(struct dc_capture *capture, HDC hdc, HWND window)
保存圖像
有些特殊需求獲取捕捉的原始圖像择诈,數(shù)據(jù)保存在capture->bits
中,是rgb數(shù)據(jù)出皇。當(dāng)然也可以直接保存為bmp圖片羞芍。如下(例子只保存5張,否則太多):
int nCount = 0;
void WriteBmp(void *data, int width, int height)
{
if (nCount > 5)
return;
FILE *pFile = NULL;
BITMAPFILEHEADER bmpheader;
BITMAPINFO bmpinfo;
char fileName[32];
int bpp = 32;
// open file
sprintf(fileName, "frame%d.bmp", nCount);
pFile = fopen(fileName, "wb");
if (!pFile)
return;
bmpheader.bfType = ('M' << 8) | 'B';
bmpheader.bfReserved1 = 0;
bmpheader.bfReserved2 = 0;
bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpheader.bfSize = bmpheader.bfOffBits + width * height * bpp / 8;
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = width;
bmpinfo.bmiHeader.biHeight = -height; //reverse the image
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = bpp;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 100;
bmpinfo.bmiHeader.biYPelsPerMeter = 100;
bmpinfo.bmiHeader.biClrUsed = 0;
bmpinfo.bmiHeader.biClrImportant = 0;
fwrite(&bmpheader, sizeof(BITMAPFILEHEADER), 1, pFile);
fwrite(&bmpinfo.bmiHeader, sizeof(BITMAPINFOHEADER), 1, pFile);
uint8_t *buffer = (uint8_t *)data;
fwrite(buffer, 1, width * height * bpp / 8, pFile);
fclose(pFile);
nCount++;
}
值得注意的是:
在桌面或窗口捕捉時(shí)郊艘,記得設(shè)置多適配器兼容模式荷科,否則你回經(jīng)常發(fā)現(xiàn)抓的數(shù)據(jù)為空,即為黑色纱注。
上層調(diào)用
既然知道了底層實(shí)現(xiàn)畏浆,那上層如何調(diào)用呢?
ui->pCBoxWindows->clear();
//創(chuàng)建窗口捕捉資源
OBSSource source = obs_source_create("window_capture", "test", NULL, nullptr);
if (source) {
//添加到指定場(chǎng)景中
OBSSceneItem sceneitem;
sceneitem = obs_scene_add(scene, source);
obs_sceneitem_set_visible(sceneitem, true);
}
//通過source獲取屬性
obs_properties_t *pProperties = obs_source_properties(source);
obs_property_t *property = obs_properties_first(pProperties);
while (property) {
//獲取屬性名字
const char *name = obs_property_name(property);
//屬性為窗口
if (strcmp("window", name) == 0){
obs_combo_format format = obs_property_list_format(property);
size_t count = obs_property_list_item_count(property);
for (size_t i = 0; i < count; i++){
//獲取窗口名字
const char *name = obs_property_list_item_name(property, i);
QVariant var;
if (format == OBS_COMBO_FORMAT_STRING) {
var = obs_property_list_item_string(property, i);
ui->pCBoxWindows->addItem(QString::fromUtf8(name), var);
}
}
}
obs_property_next(&property);
}
OBSData settings = obs_source_get_settings(source);
QString strWindow = ui->pCBoxWindows->currentData().toString();
obs_data_set_string(settings, "window", strWindow.toLocal8Bit().data());
obs_data_set_int(settings, "priority", 0);
//設(shè)置多適配器兼容
obs_data_set_bool(settings, "compatibility", true);
//更新設(shè)置狞贱,只有更新刻获,底層才會(huì)生效
obs_source_update(source, settings);
obs_properties_destroy(pProperties);
以上就是obs中Windows窗口,桌面捕捉的實(shí)現(xiàn)瞎嬉。mac的原理類似蝎毡,只是使用了不同的系統(tǒng)API。