将图像原始数据缓冲区设置为QMediaPlayer

时间:2013-08-19 14:00:47

标签: qt qt4 qt5

我希望获得窗口小部件应用程序的屏幕截图,然后使用setMedia()将其原始数据缓冲区设置为QMeidaPlayer。到目前为止我所做的是接收图像,保存图像,然后从中读取。但是,我想问你如何直接读取原始数据而不将其保存到媒体播放器中:

QPixmap originalPixmap = QPixmap::grabWidget(this);

QImage *image = new QImage(originalPixmap.toImage());

QByteArray ba;

QBuffer buffer(&ba);

buffer.setBuffer(&ba);

buffer.open(QIODevice::WriteOnly);

image->save(&buffer); // writes image into ba in PNG format

image->save(image Path);

mediaPlayer.setMedia(QUrl::fromLocalFile(image path)); //I want this to read from raw data

mediaPlayer.play();

我希望这会占用最少的CPU。但是,保存和从文件读取会消耗大量的CPU,占47%。

干杯,

更新:我也使用此代码片段测试了该程序。但它不会在视频小部件上绘制缓冲区内容。

QBuffer *buffer = new QBuffer(image_ba);
QMediaContent media;
mediaPlayer.setMedia(media, buffer);
mediaPlayer.play();

如何解决此问题以将图像原始数据输入到视频小部件?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

QPixmap originalPixmap = QPixmap::grabWidget(this);
QBuffer *buffer = new QBuffer(); // make sure that your buffer has the same life span as your media player, or otherwise the media player would try to read data from non-existing buffer. (the setMedia() and play() methods are non-blocking)
buffer.open(QIODevice::ReadWrite);
originalPixmap.save(buffer, "PNG"); // can be any other format, not just PNG
    
mediaPlayer.setMedia(QUrl(), buffer);
mediaPlayer.play();