C++图片数据流显示在窗口上

#include "atlimage.h"#include "gdiplus.h"#pragma comment( lib, "gdiplus.lib" )typedef struct _Cam_Display{ WORD wWidth; WORD wHeight; WORD wX; WORD wY;...
摘要由CSDN通过智能技术生成

#include "atlimage.h"
#include "gdiplus.h"
#pragma comment( lib, "gdiplus.lib" )

typedef struct _Cam_Display
{
    WORD        wWidth;
    WORD        wHeight;
    WORD        wX;
    WORD        wY;
    DWORD       hWnd;
}CAMDISP,*LPCAMDISP;

BOOL    CVS2010Image::ShowPicAtWin(LPVOID lpDispaly, const char*  imageData,int imageSize)
{
    BOOL bRet = FALSE;

    LPCAMDISP lpDis = (LPCAMDISP)lpDispaly;
    if(lpDis->hWnd==NULL)return FALSE;
    HWND hDisWnd = (HWND)lpDis->hWnd;

    HGLOBAL  hMem = GlobalAlloc(GMEM_MOVEABLE, 0x00400000);
    BYTE*    pMem = (BYTE*)GlobalLock(hMem);
    memcpy(pMem, imageData, imageSize);
    GlobalUnlock(hMem);

    IStream *pIStm;
    CImage image;

    if (CreateStreamOnHGlobal(hMem, TRUE, &pIStm) == S_OK)
    {

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用第三方库来生成图片流,并使用操作系统提供的API来创建子窗口显示图片。 生成图片流可以使用OpenCV库中的QRCode生成功能。具体实现步骤如下: 1. 安装OpenCV库。 2. 使用以下代码生成QRCode图片流: ``` #include <opencv2/opencv.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> using namespace cv; int main(int argc, char** argv) { std::string text = "Hello, world!"; Mat qr = QRCodeDetector::decode(text); if (!qr.empty()) { imshow("QR Code", qr); waitKey(0); } return 0; } ``` 3. 将生成的图片流传递给子窗口显示。 可以使用Windows提供的API函数来创建子窗口显示图片。具体实现步骤如下: 1. 在窗口创建完成后,使用以下代码创建子窗口: ``` HWND hChildWnd = CreateWindowEx( 0, // Optional window styles. TEXT("STATIC"), // Window class NULL, // Window text WS_CHILD | WS_VISIBLE, // Window style 0, // Position x 0, // Position y width, // Width height, // Height hWndParent, // Parent window NULL, // Menu hInst, // Instance handle NULL // Additional application data ); ``` 其中,hWndParent为父窗口句柄,width和height为图片流的宽度和高度。 2. 将图片流传递给子窗口: ``` HBITMAP hBitmap = CreateBitmap(width, height, 1, 32, pData); SendMessage(hChildWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap); ``` 其中,pData为图片流数据。 3. 显示窗口: ``` ShowWindow(hChildWnd, SW_SHOW); ``` 完整代码示例: ``` #include <opencv2/opencv.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <Windows.h> using namespace cv; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; // Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "WindowClass"; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, "WindowClass", "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL); if (hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 3: Showing the Window ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 4: The Message Loop while (GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hChildWnd = NULL; switch (msg) { case WM_CREATE: { std::string text = "Hello, world!"; Mat qr = QRCodeDetector::decode(text); if (!qr.empty()) { int width = qr.cols; int height = qr.rows; hChildWnd = CreateWindowEx( 0, // Optional window styles. TEXT("STATIC"), // Window class NULL, // Window text WS_CHILD | WS_VISIBLE, // Window style 0, // Position x 0, // Position y width, // Width height, // Height hwnd, // Parent window NULL, // Menu (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), // Instance handle NULL // Additional application data ); if (hChildWnd != NULL) { int bpp = qr.elemSize() * 8; int stride = qr.step; uchar* pData = qr.data; HBITMAP hBitmap = CreateBitmap(width, height, 1, bpp, pData); SendMessage(hChildWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap); ShowWindow(hChildWnd, SW_SHOW); } } break; } case WM_CLOSE: { DestroyWindow(hwnd); break; } case WM_DESTROY: { PostQuitMessage(0); break; } default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值