经过 10 天的努力,我又回到了就我的任务提出基本问题的问题上。
我有一个三层结构。
T1。名为“MPC”的 ROS 项目连续发布主题“video_image”帧(例如每秒 30 帧视频图像)。
T2。作为 Qt5.5 项目开发的 C++ 程序——一种中间 worker 。
T3。 Qml UI 上的图像类型对象(Qt5.5 Quick)。
我想在 T3 UI 上显示来自 MPC(T1) 的帧的视频剪辑。 到目前为止,我肯定能做的就是从 T1 的帧中创建 QImages,我可以在 PWD 上创建一个临时图像文件(例如,currentFrame.jpg)。
我应该怎么做才能将帧流作为源提供给 UI 视频播放器(也就是说,到目前为止,我仍然认为图像类型是视频播放器 UI 的候选对象。)
我是否遵循了可能的解决方案?从这些图片帧流创建视频查看器提要的可行方法是什么?
最佳答案
这可以通过使用 QQuickPaintedItem 来完成。
在 C++ Qt 部分你需要有一个继承 QQuickPaintedItem 的类:
//imagewriter.h
class imagewriter : public QQuickPaintedItem
{
Q_OBJECT
public:
imagewriter(QQuickItem *parent = 0);
void paint(QPainter *painter);
//the paint method is already implemented in QQuickPaintedItem
//you just override it
QImage image; //your image
};
//imagewriter.cpp
imagewriter::imagewriter(QQuickItem *parent): QQuickPaintedItem(parent)
{
}
void imagewriter::paint(QPainter *painter)
{
painter->drawImage(QPoint(0,0),this->image);
//this will simply draw the image in your qml UI !
}
现在您只需注册此对象,以便能够在您的 qml 文件中使用它:
//在主 .cpp 中:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<imagewriter>("mr.jb",1,0,"my_video_image");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
//main.qml :
import mr.jb 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
my_video_image {
id : my_img_strm1
anchors.centerin : parent
}
}
my_video_image 对象应该能够显示您的 qimage,您需要做的就是将该图像不断更新为您从 ROS 流中获得的最后一张图像,并且每次更新时调用预定义的 update( ); 方法,以便自动重新触发负责在 qml 世界中绘制图像的 paint 方法。 我想您的 T1 部分没有问题。
可以通过多种方式(例如 QUdp)在 T2(C++ 类)中接收和构建图像。
最后一部分(T3)在上面的代码中进行了描述。
关于c++ - 从 C++ 的图像流中创建 Qml 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215578/