-1

Trying to get some data from screen, using code from link below Keep gettings linking errors. Honestly, was trying to get any help in google, two hours gave me nothing. Please advise. Using QT/OpenCV/Windows 10

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <Windows.h>
#include <iostream>

using namespace std;
using namespace cv;

class hwnd2Mat
{
public:
    hwnd2Mat(HWND hwindow, float scale = 1);
    virtual ~hwnd2Mat();
    virtual void read();
    Mat image;

private:
    HWND hwnd;
    HDC hwindowDC, hwindowCompatibleDC;
    int height, width, srcheight, srcwidth;
    HBITMAP hbwindow;
    BITMAPINFOHEADER  bi;
};

hwnd2Mat::hwnd2Mat(HWND hwindow, float scale)
{
    hwnd = hwindow;
    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 = (int)(windowsize.bottom * scale);
    width = (int)(windowsize.right * scale);

    image.create(height, width, CV_8UC4);

    // create a bitmap
    hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);    //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
    bi.biWidth = width;
    bi.biHeight = -height;  //this is the line that makes it draw upside down or not
    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;

    // use the previously created device context with the bitmap
    SelectObject(hwindowCompatibleDC, hbwindow);
    read();
};

void hwnd2Mat::read()
{
    // copy from the window device context to the bitmap device context
    StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, image.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow
};

hwnd2Mat::~hwnd2Mat()
{
    // avoid memory leak
    DeleteObject(hbwindow);
    DeleteDC(hwindowCompatibleDC);
    ReleaseDC(hwnd, hwindowDC);
};

int main()
{
    Mat ui(40, 400, CV_8UC3, Scalar(0, 130, 0));
    putText(ui, "Press Esc to stop capturing", Point(30,30), FONT_HERSHEY_COMPLEX, 0.7,
        Scalar(0, 0, 255), 1);
    Mat bgrImg;
    hwnd2Mat capDesktop(GetDesktopWindow());

    VideoWriter writer;
    int codec = VideoWriter::fourcc('X', 'V', 'I', 'D');
    double fps = 10.0;
    string filename = "./desktop_capture.avi";
    writer.open(filename, codec, fps, capDesktop.image.size(), true);
    // check if we succeeded
    if (!writer.isOpened()) {
        cerr << "Could not open the output video file for write\n";
        return -1;
    }

    while ( true )
    {
        capDesktop.read();
        cvtColor(capDesktop.image, bgrImg, COLOR_BGRA2BGR);
        writer << bgrImg;
        imshow("desktop capture", ui);
        int key = waitKey(5);

        if (key == 27)
            break;
    }
}

:-1: error: release/main.o:main.cpp:(.text+0x59): undefined reference to __imp_StretchBlt' :-1: error: release/main.o:main.cpp:(.text+0x91): undefined reference to __imp_GetDIBits' :-1: error: release/main.o:main.cpp:(.text+0xbb): undefined reference to __imp_DeleteObject' :-1: error: release/main.o:main.cpp:(.text+0xc5): undefined reference to __imp_DeleteDC' :-1: error: release/main.o:main.cpp:(.text+0x1bc): undefined reference to __imp_DeleteObject' :-1: error: release/main.o:main.cpp:(.text+0x1c7): undefined reference to __imp_DeleteDC' :-1: error: release/main.o:main.cpp:(.text+0x246): undefined reference to __imp_CreateCompatibleDC' :-1: error: release/main.o:main.cpp:(.text+0x259): undefined reference to __imp_SetStretchBltMode' :-1: error: release/main.o:main.cpp:(.text+0x2db): undefined reference to __imp_CreateCompatibleBitmap' :-1: error: release/main.o:main.cpp:(.text+0x34f): undefined reference to __imp_SelectObject'

jNc
  • 1
  • 1
  • You have to be way kore specific about how exactly you link these libraries with your code, otherwise there's not more than general advice here. – πάντα ῥεῖ May 25 '22 at 17:33
  • my current pro file settings under the link below https://pastebin.com/3GyStVVD – jNc May 25 '22 at 18:24
  • [Edit] your question to add additional information, and note that everything needs to be self contained in it. Links to Code, or images of code aren't acceptable. You'll have to provide a [mcve] as required here. – πάντα ῥεῖ May 25 '22 at 18:27
  • Edited! Is that posted now, so anyone can answer? – jNc May 26 '22 at 09:10

0 Answers0