将C++图像发送到Python

2024-09-30 18:19:53 发布

您现在位置:Python中文网/ 问答频道 /正文

欢迎光临。我正在尝试做一些类似视频流客户端服务器应用程序的事情。在

C++中有一个客户端,Python中有一个服务器。我需要向python服务器发送一个C++映像。在

我试过了,但没有取得任何成就。在

C++代码:

//主

int main()
{
    boost::asio::io_service io_service;
    DataManager* m = new DataManager(io_service);
    DesktopReader* r = new DesktopReader(GetDesktopWindow());

    m->ConnectToSocket("127.0.0.1", 9191);

    Mat s;

    while (1) {

        r->GetDesktopMat(s);


        int imgSize = s.total() * s.elemSize();
        uchar *iptr = s.data;

        cout << imgSize << endl;

        m->SendData(s.data, imgSize);
    }

    return 0;
}

//数据管理器:

^{pr2}$

//桌面阅读器:

void DesktopReader::GetDesktopMat(Mat &src)
{
    HDC hwindowDC, hwindowCompatibleDC;

    int height, width, srcheight, srcwidth;
    HBITMAP hbwindow;
    BITMAPINFOHEADER  bi;

    hwindowDC = GetDC(hwnd);
    hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
    SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

    RECT windowsize;    // get the height and width of the screen
    GetClientRect(hwnd, &windowsize);

    srcheight = windowsize.bottom;
    srcwidth = windowsize.right;
    height = windowsize.bottom / 1;
    width = windowsize.right / 1;

    src.create(height, width, CV_8UC4);

    hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;
    bi.biHeight = -height; 
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    SelectObject(hwindowCompatibleDC, hbwindow);
    StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); 
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 
    DeleteObject(hbwindow);
    DeleteDC(hwindowCompatibleDC);
    ReleaseDC(hwnd, hwindowDC);
}

Python:

while True:
    try:
        data = client.recv(8294400) # 8294400 - image size. From: int imgSize = s.total() * s.elemSize();
        if data:
            img = cv2.imdecode(np.fromstring(data, np.uint8), 1)
            print(img) # There i got None
        else:
            raise print('Client disconnected')
    except:
        client.close()
        return False

密码有什么问题吗? 如何在python中获得C++映像?在


Tags: iosrcdataservicewidthintheightbi