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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章