2017-05-26 46 views
0

我需要打印的攝像頭車架上的時間戳,並試圖下一步:HBITMAP上沒有「可見」DC的DrawText?

CameraFrameBufferSize = WebCam->GetFrameSize(); 
CameraFrameBuffer = (unsigned char *)realloc(CameraFrameBuffer, CameraFrameBufferSize); 
unsigned char * buf = WebCam->CaptureFrame(); // returns pointer to RGB buffer of frame 

HDC hDC = CreateCompatibleDC(NULL); 
HFONT font = CreateFont(20, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, RUSSIAN_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, "times"); 
RECT rect; 
rect.left = 0; 
rect.right = WebCam->GetFrameWidth(); 
rect.top = 10; 
rect.bottom = 50; 

HBITMAP hBitmap = CreateHBITMAPfromByteArray(hDC, WebCam->GetFrameWidth(), WebCam->GetFrameHeight(), 3, buf); 

SelectObject(hDC, hBitmap); 
SelectObject(hDC, font); 
SetBkMode(hDC, TRANSPARENT); 
SetTextColor(hDC, RGB(255,255,255)); 
string Text = GetTime("%Y.%m.%d %H:%M:%S"); 
DrawTextA(hDC, Text.c_str(), Text.size(), &rect, DT_CENTER | DT_WORDBREAK); 

jpge::compress_image_to_jpeg_file_in_memory(CameraFrameBuffer, CameraFrameBufferSize, WebCam->GetFrameWidth() ,WebCam->GetFrameHeight(), 3, buf, CameraCompressor); 

CameraFrame = string(reinterpret_cast<char*>(CameraFrameBuffer), CameraFrameBufferSize); 
ReleaseDC(NULL, hDC); 
DeleteObject(hBitmap); 
DeleteObject(font); 

CreateHBITMAPfromByteArray是:

HBITMAP CreateHBITMAPfromByteArray(HDC hdc, int Width, int Height, int Colors, unsigned char* pImageData){ 
    LPBITMAPINFO lpbi = new BITMAPINFO; 
    lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    lpbi->bmiHeader.biWidth = Width; 
    lpbi->bmiHeader.biHeight = -Height; 
    lpbi->bmiHeader.biPlanes = 1; 
    lpbi->bmiHeader.biBitCount = Colors*8; 
    lpbi->bmiHeader.biCompression = BI_RGB; 
    lpbi->bmiHeader.biSizeImage = 0; 
    lpbi->bmiHeader.biXPelsPerMeter = 0; 
    lpbi->bmiHeader.biYPelsPerMeter = 0; 
    lpbi->bmiHeader.biClrUsed = 0; 
    lpbi->bmiHeader.biClrImportant = 0; 

    return CreateDIBSection(hdc, lpbi, DIB_RGB_COLORS,(void **)&pImageData,NULL,0); 
} 

並保存測試圖像文件時,我想起來了沒有文字框(string CameraFrame)...

相機在後臺拍攝並且屏幕上沒有顯示任何內容,所以我不確定我正在選擇HDC。

一般來說,我有一個圖像的RGB緩衝區,其中必須放置具有透明度的文本。如何實現?

回答

1

它看起來像壓縮(和保存)原始相機幀緩衝區buf,而不是位圖緩衝區。 CreateDIBSection的使用也是錯誤的,因爲參數#4是一個應該接收指向DIB位值位置的指針的外部參數,並且您試圖將指針傳遞給現有的圖像數據。

+0

是的,我用錯了'CreateDIBSection'。謝謝。 – Iceman

相關問題