Qt界面内调用本地视频和摄像头

最近在界面上调用了本地视频,给大家介绍一下调用的过程
首先需要在Ubuntu中配置好opencv,具体的配置过程不再赘述,可以参考这篇博文
链接

一.注意点

1.在Qt中,可以使用QTime类来代替waitKey()来实现延时,然后将其中的timeout()函数作为信号,信号输出视频截至。
2.Mat类要转换成Qt的QImage,QImage是qt中的图片及视频容器。
3.在这里我使用的资源路径为绝对路径,相对路径调不出来,建议使用绝对路径.

二.使用的控件

一个label控件用于存放视频,一个combox控件用于播放类型 ,两个pushButton控件用于开始和暂停,另外布局中需要两个label控件和一个groupbox控件

三.代码

1.picture.h
#ifndef PICTURE_H
#define PICTURE_H

#include <QWidget>
#include <QTimer>
#include<opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<QPaintEvent>

using namespace cv;


namespace Ui {
class picture;
}

class picture : public QWidget
{
    Q_OBJECT

public:
    explicit picture(QWidget *parent = nullptr);
    ~picture();

private:
    Ui::picture *ui;
    VideoCapture capture;
    QTimer *timer;
    Mat frame;
    bool isCamera = 0;
   // void paintEvent(QPaintEvent*event);

private slots:
    void importFrame();
    void on_displayButton_clicked();
    void on_stopButton_clicked();
    void on_comboBox_currentIndexChanged(int index);
};

#endif // PICTURE_H
2.picture.cpp
#include "picture.h"
#include "ui_picture.h"
#include <QTimer>
#include<QString>
#include <QDebug>
#include<opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<QPainter>
#include<QPixmap>

picture::picture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::picture)
{

    ui->setupUi(this);
    this->setWindowTitle("实时图像视图显示");
    this->resize(QSize(1000,800));
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(importFrame()));//import frame when timeout

}

void picture::importFrame()
{
    capture >> frame;
    Mat output;
    cvtColor(frame, output, CV_BGR2RGB);//only RGB of Qt
    QImage srcQImage = QImage((uchar*)(output.data), output.cols, output.rows, QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(srcQImage));
    ui->label->resize(srcQImage.size());
    ui->label->show();
}

void picture::on_displayButton_clicked()
{
    if (isCamera)
    {
        capture.open(0);
    }
    else
    {
        bool ok = capture.open("/home/wwf0528/Qt/untitled6/520.mp4");
        qDebug()<<ok;
    }
    timer->start(40);// Start timing, Signal out when timeout
}

void picture::on_stopButton_clicked()
{
    timer->stop();
    capture.release();
}


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

void picture::on_comboBox_currentIndexChanged(int index)
{
    if (index)//此处待确定
        isCamera = 1;
    else
        isCamera = 0;
}

3.ui设计界面

放置三个控件,并设置好布局.
在这里插入图片描述

四.最终效果

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值