6

我想获取小部件应用程序的屏幕截图,然后使用 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();

有什么想法可以解决这个问题以将图像原始数据输入到视频小部件?

4

1 回答 1

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();
于 2021-06-15T19:05:12.680 回答