set image raw data buffer to QMediaPlayer
Asked Answered
T

1

6

I want to get a screen shot of the widget application and then set its raw data buffer to QMeidaPlayer with setMedia(). What I have done so far is to receive the image, SAVE it, and then read from it. However, I would like to ask you how to read raw data directly without saving it into media player:

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();

I want this to take the minimum CPU usage. However, saving and reading from file consumes lots of CPU,47%.

Cheers,

Update: I tested the program with this code snippet as well. But it does not draw the buffer contents on video widget.

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

Any ideas how I can resolve this to input image raw data to video widget?

Thalweg answered 19/8, 2013 at 14:0 Comment(8)
This might be a solution to something on but in the meantime they've deprecated QPixmap::grabWidget() in favor of QWidget::grab() returning a QPixmap.Bourque
@pauljay What is the underlying objective? I ask because QMediaPlayer is made to read video or audio files, and not images, so a possible solution is to convert the .png to .mp4 (or similar) which can be complicated. If instead your objective in the background is to display QPixmap in a QVideoWidget then there are other alternativesVassaux
@Thalweg What's the point to do that? You can draw pixmap to QLabel for ex. It will cost less than throw picture to video widget. Or you trying to display realtime image?Briquette
I know there are other solutions to this problem. I want to know if its possible render an image using QBuffer and QMediaPlayer, and if so how to accomplish it, not with a Label or any other workaround. Simply because I want to know.Psychic
@PaulJay As I said, the solution is to convert the QPixmap (or QImage) to .mp4 or a format that is supported by the QtMultimedia backends, and that work can be unnecessarily complicated. Please use @usernameVassaux
@PaulJay An alternative and simpler solution is to use a QVideoWidget and place the QImages using the associated QAbstractVideoSurface.Vassaux
@Vassaux I would be happy with this answer. We would still be using the QVideoWidget and would be able to use the byte stream of an image. Could you provide a working example?Psychic
@Vassaux A small example with a single image would be enough.Psychic
B
0

here's how you can do it:

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();
Bucky answered 15/6, 2021 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.