一、使用现有的图片文件显示在 界面 picture 控件
- 在MFC picture 加载bitmap 图片方法
- 图片可以是从资源文件夹来的,也可以是文件路径等
- CBitMap 载入图像接口 有两种方式
BOOL LoadBitmap(LPCTSTR lpszResourceName); // 资源文件名
BOOL LoadBitmap(UINT nIDResource); // 资源ID
CreateBitmap; // 创建一个,指定参数的bitmap图片
void MyVideo::OnBnClickedBtnAppyset()
{// TODO: 在此添加控件通知处理程序代码CBitmap img; // 这里不能使用 指针if (!img.LoadBitmap(IDB_BITMAP2)) {std::cout << "asdasd" << std::endl;}CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATICPICTURE);pStatic->ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);pStatic->SetBitmap((HBITMAP)img);
}
二、 显示从USB 或者其他网络获取的图像数据流
- 图像视频刷新定时器时间
void MyVideo::slotRefVideoTimer()
{m_videoQueueMutex->lock();if (m_videoQueue.size()){GUI_PW("%s", "show image");unsigned char * data = m_videoQueue.front();// 图像数据流unsigned char * rgb = (unsigned char *)malloc(256 * 192 * 3); yuv422torgb(rgb, data, GUIDE_RES_WIDETH, GUIDE_RES_HEIGHT);if (m_startSaveRgb){char saveFileName[256] = { 0 };snprintf(saveFileName, sizeof(saveFileName), "RGB/%d.raw", time(0));FILE* fp = NULL;fopen_s(&fp ,saveFileName, "ab+");if (NULL != fp){CORE_PW("%s fopen success !", saveFileName);fwrite(rgb, 1, 256 * 192 * 3, fp);fclose(fp);}else{CORE_PW("%s fopen fail !", saveFileName);}m_startSaveRgb = false;}// 显示图片到 IDC_STATICPICTURE 界面控件ShowRGBToWnd(IDC_STATICPICTURE, rgb, GUIDE_RES_WIDETH, GUIDE_RES_HEIGHT);free(data);free(rgb);m_videoQueue.pop_front();}m_videoQueueMutex->unlock();
}
- 将 USB 摄像头 数据流构造成 bitmap 画到界面
void MyVideo::ShowRGBToWnd(UINT hWnd, unsigned char *rgbImageData, int width, int height)
{m_framIndex++;cout << "frame index =" << m_framIndex << endl;// 制作bitmapinfo(数据头)BITMAPINFO bitInfo;bitInfo.bmiHeader.biBitCount = 8 * 3; // RGB 888 3个字节bitInfo.bmiHeader.biWidth = width;bitInfo.bmiHeader.biHeight = -height; //注意"-"号(正数时倒着绘制)bitInfo.bmiHeader.biPlanes = 1;bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bitInfo.bmiHeader.biCompression = BI_RGB;bitInfo.bmiHeader.biClrImportant = 0;bitInfo.bmiHeader.biClrUsed = 0;bitInfo.bmiHeader.biSizeImage = 0;bitInfo.bmiHeader.biXPelsPerMeter = 0;bitInfo.bmiHeader.biYPelsPerMeter = 0;CDC *pDC = GetDlgItem(hWnd)->GetDC(); //获取图片控件DC//绘图::StretchDIBits(pDC->GetSafeHdc(),0, 0, 256* m_scaleValue, 192* m_scaleValue, // 按照用户选择放大0, 0, 256, 192,rgbImageData,&bitInfo,DIB_RGB_COLORS,SRCCOPY);cout << "current scale StretchDIBits = " << m_scaleValue;
}