当我遇到类似的问题时,我偶然发现了你的问题。过了一会儿,我找到了一个对我有用的解决方案。即使您的问题是一个较旧的问题并且您可能会继续前进,我也想分享我的答案以可能对其他人有所帮助。
我在中找到了答案QT文档在“使用低级视频帧”部分中。那里发布的代码片段作为起点非常有帮助,但我必须修改它,以便它正常工作。一个最小的工作示例如下所示:
FrameProvider.h
#include <QObject>
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
class FameProvider : public QObject
{
Q_OBJECT
Q_PROPERTY(QAbstractVideoSurface *videoSurface READ videoSurface WRITE setVideoSurface)
public:
QAbstractVideoSurface* videoSurface() const { return m_surface; }
private:
QAbstractVideoSurface *m_surface = NULL;
QVideoSurfaceFormat m_format;
public:
void setVideoSurface(QAbstractVideoSurface *surface)
{
if (m_surface && m_surface != surface && m_surface->isActive()) {
m_surface->stop();
}
m_surface = surface;
if (m_surface && m_format.isValid())
{
m_format = m_surface->nearestFormat(m_format);
m_surface->start(m_format);
}
}
void setFormat(int width, int heigth, int format)
{
QSize size(width, heigth);
QVideoSurfaceFormat format(size, format);
m_format = format;
if (m_surface)
{
if (m_surface->isActive())
{
m_surface->stop();
}
m_format = m_surface->nearestFormat(m_format);
m_surface->start(m_format);
}
}
public slots:
void onNewVideoContentReceived(const QVideoFrame &frame)
{
if (m_surface)
m_surface->present(frame);
}
};
main.qml
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import QtMultimedia 5.4
import com.yourcompany.FrameProvider 1.0
ApplicationWindow {
objectName: "mainWindow"
visible: true
width: 640
height: 480
FrameProvider{
objectName: "provider"
id: provider
}
VideoOutput {
id: display
objectName: "display"
anchors.top: parent.top
anchors.bottom: parent.bottom
width: parent.width
source: provider
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
// initialize the qml application engine
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//register the custom control to the qml application engine
qmlRegisterType<FameProvider>("com.yourcompany.FrameProvider", 1, 0, "FrameProvider");
// start the view
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
{
return -1;
}
// find your custom control
QObject *rootObject = engine.rootObjects().first();
Qobject *display = rootObject->findChild<QObject *>("display");
auto provider = qvariant_cast<FameProvider *>(display->property("source"));
// Create your custom frame source class, which inherits from QObject. This source is expected to have the following public fields and signals:
// - int width
// - int height
// - int format (following QVideoFrame::PixelFormat)
// - signals: void newFrameAvailable(const QVideoFrame &frame);
CustomFramesource source;
// Set the correct format for the video surface (Make sure your selected format is supported by the surface)
provider->setFormat(source.width,source.height, source.format);
// Connect your frame source with the provider
QObject::connect(&source, SIGNAL(newFrameAvailable(const QVideoFrame &)), provider, SLOT(onNewVideoContentReceived(const QVideoFrame &)));
// run the app
int retVal = app.exec();
return 0;
}
MWE 是从我的实际代码中浓缩而来的,因此未经测试。我希望它无论如何都能运行并显示所有需要的步骤。