如果VideoCapture实例化,则无法在QML中播放视频/音频

时间:2019-10-04 23:25:56

标签: c++ qt opencv

我正在使用Qt 5.13.1开发ubuntu 16.04,并尝试使用Qt多媒体模块在应用程序中播放视频或音频,还尝试使用OpenCV从网络摄像机捕获图片。

如果我实例化新的cv :: VideoCapture对象,则没有问题,QML UI中没有视频,没有音频不会运行而没有错误

  

cv :: VideoCapture * cap =新的cv :: VideoCapture();

任何帮助将不胜感激,谢谢。

project.pro:

QT += quick
CONFIG += c++11
QT+= multimedia

DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += \
        main.cpp\
        CamBase.cpp


HEADERS += \
    CamBase.h

RESOURCES += qml.qrc

LIBS += -L/usr/lib -L/usr/local/lib

LIBS += -lopencv_core -lopencv_imgproc -lopencv_objdetect -lopencv_imgcodecs -lopencv_highgui -lopencv_videoio
INCLUDEPATH += /usr/local/include/opencv

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "CamBase.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    CamBase *mCamBase = new CamBase();

    mCamBase->runTest();

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

CamBase.h:

#ifndef CAMBASE_H
#define CAMBASE_H

#include <QObject>
#include "opencv/highgui.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
#include "opencv/cv.h"

class CamBase : public QObject
{
    Q_OBJECT
public:
    explicit CamBase(QObject *parent = nullptr);

    void runTest();
    bool saveFrameCapture(cv::Mat frame);
signals:

public slots:
};

#endif // CAMBASE_H

CamBase.cpp:

#include "CamBase.h"
#include <QDir>
#include <QDebug>

const int maxCount = 10;

CamBase::CamBase(QObject *parent) : QObject(parent)
{
}

void CamBase::runTest()
{
    cv::Mat frameOriginal;
    cv::VideoCapture *cap = new cv::VideoCapture();
    cap->open(0);

    for(int cnt = 0; cnt < maxCount; cnt++)
    {
        (*cap) >> frameOriginal;
    }
    saveFrameCapture(frameOriginal);
    cap->release();

}

bool CamBase::saveFrameCapture(cv::Mat frame)
{
    QString pathFile;
    pathFile = "./test.bmp";

    if(!imwrite(pathFile.toUtf8().constData(),frame))
    {
        return false;
    }
    return true;
}

main.qml:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtMultimedia 5.7

Window {
    visible: true
    width: Screen.width/2
    height: Screen.height/2
    title: qsTr("Hello World")
    Video {
        id: video
        width: Screen.width/2
        height: Screen.height/2
        source: "file:///home/user/Documents/video.mp4"
        autoPlay: true
        loops: Animation.Infinite
        onStopped: play()
    }
}

0 个答案:

没有答案