gpt4 book ai didi

c++ - QAbstractVideoSurface 示例

转载 作者:行者123 更新时间:2023-12-01 13:45:27 49 4
gpt4 key购买 nike

我正在尝试使自己成为具有更多功能的 QML 相机项目,并提供 VideoOutput 的来源元素。比如这样:

VideoOutput{
source:mycamera
}
MyCustomCamera{
id:mycamera
}

in the document it says

If you are extending your own C++ classes to interoperate with VideoOutput, you can either provide a QObject based class with a mediaObject property that exposes a QMediaObject derived class that has a QVideoRendererControl available, or you can provide a QObject based class with a writable videoSurface property that can accept a QAbstractVideoSurface based class and can follow the correct protocol to deliver QVideoFrames to it.



我试过给我的对象一个私有(private)属性 mediaObject ,它是 QCamera 类型,但看起来 QCamera 没有 QVideoRenderControl(或者我的错不知道如何正确执行它)。

我需要达到我一开始展示的效果,无论如何欢迎。

或者,谁能给我一个简短的例子,说明“接受 blablabla 并遵循正确协议(protocol)的可写 videoSurace 属性”是什么意思?

最佳答案

我无法帮助您解决您的主要问题,但我可以为您提供 videoSurface 的示例用法.您可以像这样使用“可写videoSurface”:

我的示例包括三个主要步骤:

  • 您编写了一个具有 QAbstactVideoSurface 属性的类。这个类将是你的视频提供者,它可以通过调用它的 present() 函数在 VideoOutput 上显示帧。

  • 视频适配器.h
    #ifndef VIDEOADAPTER_H
    #define VIDEOADAPTER_H
    #include <QObject>
    #include <QAbstractVideoSurface>
    #include <QVideoSurfaceFormat>
    #include <QTimer>

    class VideoAdapter : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QAbstractVideoSurface* videoSurface READ videoSurface WRITE setVideoSurface NOTIFY signalVideoSurfaceChanged)
    public:
    explicit VideoAdapter(QObject *parent = nullptr);

    QAbstractVideoSurface *videoSurface() const;
    void setVideoSurface(QAbstractVideoSurface *videoSurface);

    signals:
    void signalVideoSurfaceChanged();

    private slots:
    void slotTick();

    private:
    void startSurface();

    private:
    QAbstractVideoSurface *mVideoSurface;
    QVideoSurfaceFormat *mSurfaceFormat;
    QImage *mImage;
    QTimer mTimer;
    };

    #endif // VIDEOADAPTER_H

    视频适配器.cpp
    #include "videoadapter.h"
    #include <QDebug>

    VideoAdapter::VideoAdapter(QObject *parent)
    : QObject(parent), mVideoSurface(nullptr), mSurfaceFormat(nullptr)
    {
    mTimer.setInterval(1000);
    connect(&mTimer, &QTimer::timeout, this, &VideoAdapter::slotTick);
    }

    QAbstractVideoSurface *VideoAdapter::videoSurface() const
    {
    return mVideoSurface;
    }

    void VideoAdapter::setVideoSurface(QAbstractVideoSurface *videoSurface)
    {
    if(videoSurface != mVideoSurface)
    {
    mVideoSurface = videoSurface;
    emit signalVideoSurfaceChanged();
    startSurface();

    // This is the test timer that will tick for us to present the image
    // on the video surface
    mTimer.start();
    }
    }

    void VideoAdapter::slotTick()
    {
    QVideoFrame frame(*mImage);
    mVideoSurface->present(frame);
    }

    void VideoAdapter::startSurface()
    {
    mImage = new QImage("../resources/images/test.jpg");
    auto pixelFormat = QVideoFrame::pixelFormatFromImageFormat(mImage->format());
    mSurfaceFormat = new QVideoSurfaceFormat(mImage->size(), pixelFormat);
    if(!mVideoSurface->start(*mSurfaceFormat))
    {
    qDebug() << "Surface couldn't be started!";
    }
    }

    此类仅加载图像文件并使用计时器显示它,但在您的情况下,您将拥有一个帧源,因此您可以更改它以满足您的需要。如果您可以将框架转换为 QImageQVideoFrame你可以像这样显示它。
  • 您必须使此类在 QML 中可用。在我的情况下,我创建了一个对象并通过将其设置为属性使其对 QML 可见。
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    QQmlDebuggingEnabler enabler;

    VideoAdapter adapter;
    // When you do this this object is made visible to QML context with the
    // given name
    engine.rootContext()->setContextProperty("videoAdapter", &adapter);

    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();
    }
  • 您将此对象作为 QML 中的源提供给 VideoOutput。
    Window {
    visible: true
    width: 640
    height: 480
    color: "black"
    title: qsTr("Video Player")

    VideoOutput {
    id: videoPlayer
    anchors.fill: parent
    source: videoAdapter
    }
    }

  • 正如我所说的这个例子是一个简单的例子,它只加载一个图像并且只定期显示一个图像。

    这个问题是一个老问题,你可能会继续前进,但希望这至少可以帮助其他人。

    关于c++ - QAbstractVideoSurface 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25418763/

    49 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com