在windows cpp 动态库读取test.mp4视频流到Windows窗口的离屏表面,且不卡UI,在Qt调用该动态库。完整源码
时间: 2023-12-06 20:42:07 浏览: 90
以下是一个简单的示例代码,可以将test.mp4视频流读取到Windows窗口的离屏表面,并在Qt中调用该动态库:
1. 创建动态链接库项目,并添加以下代码:
```cpp
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include <iostream>
#include <fstream>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
// 视频宽度和高度
#define VIDEO_WIDTH 640
#define VIDEO_HEIGHT 480
// 全局变量
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DSURFACE9 g_pSurface = NULL;
LPD3DXSPRITE g_pSprite = NULL;
// 初始化Direct3D
bool InitD3D(HWND hWnd)
{
// 创建Direct3D对象
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (g_pD3D == NULL)
return false;
// 配置显示模式
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// 创建Direct3D设备
if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
{
return false;
}
// 创建表面
if (FAILED(g_pd3dDevice->CreateOffscreenPlainSurface(
VIDEO_WIDTH, VIDEO_HEIGHT, D3DFMT_X8R8G8B8,
D3DPOOL_SYSTEMMEM, &g_pSurface, NULL)))
{
return false;
}
// 创建Sprite
if (FAILED(D3DXCreateSprite(g_pd3dDevice, &g_pSprite)))
{
return false;
}
return true;
}
// 从视频文件中读取数据到表面
bool ReadSurfaceFromFile(std::string filePath)
{
// 打开文件
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open())
{
return false;
}
// 读取数据
char* pBuffer = new char[VIDEO_WIDTH * VIDEO_HEIGHT * 4];
file.read(pBuffer, VIDEO_WIDTH * VIDEO_HEIGHT * 4);
// 创建Surface
IDirect3DSurface9* pDestSurface = NULL;
if (FAILED(g_pd3dDevice->CreateOffscreenPlainSurface(
VIDEO_WIDTH, VIDEO_HEIGHT, D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT, &pDestSurface, NULL)))
{
delete[] pBuffer;
return false;
}
// 锁定表面
D3DLOCKED_RECT lockedRect;
if (FAILED(g_pSurface->LockRect(&lockedRect, NULL, D3DLOCK_DISCARD)))
{
pDestSurface->Release();
delete[] pBuffer;
return false;
}
// 将数据复制到表面
char* pSrc = pBuffer;
char* pDest = (char*)lockedRect.pBits;
for (int i = 0; i < VIDEO_HEIGHT; i++)
{
memcpy(pDest, pSrc, VIDEO_WIDTH * 4);
pSrc += VIDEO_WIDTH * 4;
pDest += lockedRect.Pitch;
}
// 解锁表面
g_pSurface->UnlockRect();
// 绘制表面
if (SUCCEEDED(g_pd3dDevice->GetRenderTarget(0, &pDestSurface)))
{
g_pd3dDevice->StretchRect(g_pSurface, NULL, pDestSurface, NULL, D3DTEXF_NONE);
pDestSurface->Release();
}
// 释放资源
delete[] pBuffer;
pDestSurface->Release();
return true;
}
// 渲染函数
void Render()
{
// 清空屏幕
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
// 开始渲染
if (SUCCEEDED(g_pd3dDevice->BeginScene()))
{
// 设置Sprite
g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
// 绘制表面
g_pSprite->Draw(g_pSurface, NULL, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));
// 结束渲染
g_pSprite->End();
g_pd3dDevice->EndScene();
}
// 显示画面
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
// 释放资源
void Cleanup()
{
if (g_pSprite != NULL)
g_pSprite->Release();
if (g_pSurface != NULL)
g_pSurface->Release();
if (g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if (g_pD3D != NULL)
g_pD3D->Release();
}
// DLL入口函数
extern "C" __declspec(dllexport) bool PlayVideo(HWND hWnd, std::string filePath)
{
// 初始化Direct3D
if (!InitD3D(hWnd))
return false;
// 读取数据到表面
if (!ReadSurfaceFromFile(filePath))
return false;
// 渲染画面
Render();
// 释放资源
Cleanup();
return true;
}
```
2. 在Qt中调用该动态库:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <Windows.h>
typedef bool(*PlayVideoFunc)(HWND hWnd, std::string filePath);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 加载动态库
HINSTANCE hDll = LoadLibrary(TEXT("MyVideoPlayer.dll"));
if (hDll == NULL)
{
QMessageBox::warning(this, "Error", "Failed to load DLL.");
return;
}
// 获取函数指针
PlayVideoFunc PlayVideo = (PlayVideoFunc)GetProcAddress(hDll, "PlayVideo");
if (PlayVideo == NULL)
{
QMessageBox::warning(this, "Error", "Failed to get function.");
return;
}
// 调用函数
if (!PlayVideo((HWND)ui->widget->winId(), "test.mp4"))
{
QMessageBox::warning(this, "Error", "Failed to play video.");
return;
}
}
```
注意:上述代码仅为示例代码,具体实现可能需要根据实际情况进行调整。同时,还需要在项目中添加相关的头文件和库文件,并将test.mp4放置在程序运行目录下。
阅读全文