OpenCv打開圖像窗口顯示在MFC控件上 和 Qt控件上

OpenCv將圖像窗口嵌入到MFC Picture Control控件上,resizeWindow調整窗口大小和控件大小一致,WINDOW_NORMAL設置圖像自適應窗口

int width, height;
CRect picRect;

GetDlgItem(IDC_DISPLAY_STATIC)->GetClientRect(&picRect);
width = picRect.right;
height = picRect.bottom;

namedWindow("MyPicture", WINDOW_NORMAL);
resizeWindow("MyPicture",width,height);
HWND hWnd = (HWND)cvGetWindowHandle("MyPicture");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_DISPLAY_STATIC)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);



char *fn="E:\\img\\18-11-52O.bmp";	
Mat image=imread(fn);
if(!image.data)
	MessageBox(_T("圖像加載失敗!"));
imshow("MyPicture",image);

 

 

顯示到Qt控件,(參考)



#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 打開攝像頭
    bool ok = mvCapture.open(0);

    namedWindow("view", WINDOW_AUTOSIZE);

    HWND Whnd;
    Whnd=(HWND)cvGetWindowHandle("view");
    if (Whnd)
    {
        HWND parentHwnd;
        parentHwnd = (HWND)GetWindowLong(Whnd,GWL_HWNDPARENT);
        if(parentHwnd)
        {
            ShowWindow(parentHwnd,SW_HIDE);//隱藏
        }

        SetWindowLong(parentHwnd,GWL_STYLE,/*WS_CLIPCHILDREN|*/WS_CLIPSIBLINGS|WS_CHILDWINDOW);//隱藏標題欄

        SetWindowLong(parentHwnd,GWL_EXSTYLE,WS_EX_TOPMOST);//在最前面

        if(parentHwnd)
        {
            ShowWindow(parentHwnd,SW_SHOW);//顯示
        }
    }

    QObject::connect(&dataTimer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    dataTimer.start(100);



    //QWidget* a = QWidget::find((WId)hWnd1);
    //ui->gridLayout->addWidget(a);
}

MainWindow::~MainWindow()
{
    delete ui;
}


// 顯示定時器
void MainWindow::OnTimer()
{
    Mat srcImage;
    mvCapture>>srcImage;
    imshow("view", srcImage);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章