HBITMAP上的DrawText没有"可见" DC?

时间:2017-05-26 14:14:22

标签: c++ image-processing gdi

我需要在网络摄像头框架上打印一个时间戳并尝试下一步:

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 );
}

我在保存测试图像文件(字符串CameraFrame)时只得到一个没有文字的框架......

相机在后台拍摄,屏幕上没有显示任何内容,因此我不确定HDC是否会选择。

一般来说,我有一个RGB缓冲图像,其中必须放置具有透明度的文本。如何实现?

1 个答案:

答案 0 :(得分:1)

看起来您正在压缩(并保存)原始相机帧缓冲区buf,而不是位图缓冲区。 CreateDIBSection的使用也是错误的,因为param#4是一个out参数,它应该接收指向DIB位值位置的指针,并且你试图将指针传递给那里的现有图像数据。 / p>