#include <d3d11.h>
#include <dxgi1_2.h>
class VideoDXGICaptor
{
public:
VideoDXGICaptor();
~VideoDXGICaptor();
public:
BOOL Init();
VOID Deinit();
public:
virtual BOOL CaptureImage(RECT &rect, void *pData, INT &nLen);
virtual BOOL CaptureImage(void *pData, INT &nLen);
virtual BOOL ResetDevice();
private:
BOOL AttatchToThread(VOID);
BOOL QueryFrame(void *pImgData, INT &nImgSize);
BOOL QueryFrame(void *pImgData, INT &nImgSize, int z);
private:
IDXGIResource *zhDesktopResource;
DXGI_OUTDUPL_FRAME_INFO zFrameInfo;
ID3D11Texture2D *zhAcquiredDesktopImage;
IDXGISurface *zhStagingSurf;
private:
BOOL m_bInit;
int m_iWidth, m_iHeight;
ID3D11Device *m_hDevice;
ID3D11DeviceContext *m_hContext;
IDXGIOutputDuplication *m_hDeskDupl;
DXGI_OUTPUT_DESC m_dxgiOutDesc;
};
#include "stdafx.h"
#include "DXGICaptor.h"
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#define RESET_OBJECT(obj) { if(obj) obj->Release(); obj = NULL; }
static BOOL g_bAttach = FALSE;
VideoDXGICaptor::VideoDXGICaptor()
{
m_bInit = FALSE;
m_hDevice = NULL;
m_hContext = NULL;
m_hDeskDupl = NULL;
ZeroMemory(&m_dxgiOutDesc, sizeof(m_dxgiOutDesc));
}
VideoDXGICaptor::~VideoDXGICaptor()
{
Deinit();
}
BOOL VideoDXGICaptor::Init()
{
HRESULT hr = S_OK;
if (m_bInit)
{
return FALSE;
}
// Driver types supported
D3D_DRIVER_TYPE DriverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
// Feature levels supported
D3D_FEATURE_LEVEL FeatureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1
};
UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
D3D_FEATURE_LEVEL FeatureLevel;
/
/
// Create D3D device
//
for (UINT DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
{
hr = D3D11CreateDevice(NULL, DriverTypes[DriverTypeIndex], NULL, 0, FeatureLevels, NumFeatureLevels, D3D11_SDK_VERSION, &m_hDevice, &FeatureLevel, &m_hContext);
if (SUCCEEDED(hr))
{
break;
}
}
if (FAILED(hr))
{
return FALSE;
}
// Get DXGI device
//
IDXGIDevice *hDxgiDevice = NULL;
hr = m_hDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&hDxgiDevice));
if (FAILED(hr))
{
return FALSE;
}
//
// Get DXGI adapter
//
IDXGIAdapter *hDxgiAdapter = NULL;
hr = hDxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&hDxgiAdapter));
RESET_OBJECT(hDxgiDevice);
if (FAILED(hr))
{
return FALSE;
}
/
/
// Get output
//
INT nOutput = 0;
IDXGIOutput *hDxgiOutput = NULL;
hr = hDxgiAdapter->EnumOutputs(nOutput, &hDxgiOutput);
RESET_OBJECT(hDxgiAdapter);
if (FAILED(hr))
{
return FALSE;
}
/
/
// get output description struct
//
hDxgiOutput->GetDesc(&m_dxgiOutDesc);
//
// QI for Output 1
//
IDXGIOutput1 *hDxgiOutput1 = NULL;
hr = hDxgiOutput->QueryInterface(__uuidof(hDxgiOutput1), reinterpret_cast<void**>(&hDxgiOutput1)); RESET_OBJECT(hDxgiOutput);
if (FAILED(hr))
{
return FALSE;
}
// Create desktop duplication
//
hr = hDxgiOutput1->DuplicateOutput(m_hDevice, &m_hDeskDupl); RESET_OBJECT(hDxgiOutput1);
if (FAILED(hr))
{
return FALSE;
}
// 初始化成功
m_bInit = TRUE;
return TRUE;
// #else
// ⼩于vs2012,此功能不能实现
return FALSE;
// #endif
}
VOID VideoDXGICaptor::Deinit()
{
if (!m_bInit)
{
return;
}
m_bInit = FALSE;
if (m_hDeskDupl)
{
m_hDeskDupl->Release();
m_hDeskDupl = NULL;
}
if (m_hDevice)
{
m_hDevice->Release();
m_hDevice = NULL;
}
if (m_hContext)
{
m_hContext->Release();
m_hContext = NULL;
}
// #endif
}
BOOL VideoDXGICaptor::AttatchToThread(VOID)
{
if (g_bAttach)
{
return TRUE;
}
HDESK hCurrentDesktop = OpenInputDesktop(0, FALSE, GENERIC_ALL); if (!hCurrentDesktop)
苹果11怎么截图{
return FALSE;
}
// Attach desktop to this thread
BOOL bDesktopAttached = SetThreadDesktop(hCurrentDesktop);
CloseDesktop(hCurrentDesktop);
hCurrentDesktop = NULL;
g_bAttach = TRUE;
return bDesktopAttached;
}
BOOL VideoDXGICaptor::CaptureImage(RECT &rect, void *pData, INT &nLen) {
return QueryFrame(pData, nLen);
}
BOOL VideoDXGICaptor::CaptureImage(void *pData, INT &nLen)
{
return QueryFrame(pData, nLen);
}
BOOL VideoDXGICaptor::ResetDevice()
{
Deinit();
return Init();
}
BOOL VideoDXGICaptor::QueryFrame(void *pImgData, INT &nImgSize)
{
if (!m_bInit || !AttatchToThread())
{
return FALSE;
}
nImgSize = 0;
发布评论