DirectShow 录制视频时,如何在视频上画线和输出文字

下面的代码说明了如何在录制视频的时候在视频中加入文字或其他的形状


LONG cx, cy;
HRESULT hr;
hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);
if (FAILED(hr))
{
Msg(TEXT("GetNativeVideoSize FAILED!  hr=0x%x\r\n"), hr);
return hr;
}


HDC hdc = GetDC(hwndApp);
HDC hdcBmp = CreateCompatibleDC(hdc);
g_hFont=CreateFont(30, 10,0,0, 10, FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,_T("宋体"));
HFONT hOldFont = (HFONT) SelectObject(hdcBmp, g_hFont);


HPEN hPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
HPEN hOldPen = (HPEN) SelectObject(hdcBmp, hPen);


int nLength, nTextBmpWidth, nTextBmpHeight;
SIZE sz={0};
nLength = (int) _tcslen(szNewText);
GetTextExtentPoint32(hdcBmp, szNewText, nLength, &sz);
nTextBmpHeight = sz.cy;
nTextBmpWidth  = 400/*sz.cx*/;


HBITMAP hbm = CreateCompatibleBitmap(hdc, nTextBmpWidth, nTextBmpHeight);
ReleaseDC(hwndApp, hdc);


BITMAP bm;
HBITMAP hbmOld;
GetObject(hbm, sizeof(bm), &bm);
hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);


RECT rcText;
SetRect(&rcText, 0, 0, nTextBmpWidth, nTextBmpHeight);
SetBkMode(hdcBmp,TRANSPARENT);            //设置透明背景
SetTextColor(hdcBmp, g_rgbColors);      // 文字颜色
HBRUSH hBrush=((HBRUSH)GetStockObject(NULL_BRUSH));
    SelectObject(hdcBmp, hBrush);
// 在位图上画图
//输出文字
//TextOut(hdcBmp, 0, 0, szNewText, nLength);
DrawText(hdcBmp,szNewText,sizeof(szNewText),&rcText,DT_RIGHT);
//画线
MoveToEx(hdcBmp,10,10,NULL);
LineTo(hdcBmp,200,10);
//Ellipse(hdcBmp,0,0,100,nTextBmpHeight);


// Configure the VMR's bitmap structure
VMRALPHABITMAP bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
bmpInfo.dwFlags = VMRBITMAP_HDC;
bmpInfo.hdc = hdcBmp;  
g_nImageWidth = bm.bmWidth;
g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;


bmpInfo.rDest.left  = 0.0f + X_EDGE_BUFFER;
bmpInfo.rDest.right = 1.0f - X_EDGE_BUFFER;
bmpInfo.rDest.top = (float)(cy - bm.bmHeight) / (float)cy - Y_EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - Y_EDGE_BUFFER;
bmpInfo.rSrc = rcText;




bmpInfo.fAlpha = TRANSPARENCY_VALUE;
SetColorRef(bmpInfo);


hr = pBMP->SetAlphaBitmap(&bmpInfo);
if (FAILED(hr))
Msg(TEXT("SetAlphaBitmap FAILED!  hr=0x%x\r\n\r\n%s\0"), hr,
STR_VMR_DISPLAY_WARNING);


// Select the initial objects back into our device context
DeleteObject(SelectObject(hdcBmp, hbmOld));
DeleteObject(SelectObject(hdcBmp, hOldPen));
SelectObject(hdc, hOldFont);
DeleteObject(hbm);
DeleteDC(hdcBmp);
return hr;

你可能感兴趣的:(DirectShow学习)