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?

user2696674
  • 159
  • 1
  • 2
  • 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. – Josh Dec 30 '13 at 16:49
  • @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 alternatives – eyllanesc Jun 09 '21 at 22:00
  • @user2696674 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? – Shtol Krakov Jun 10 '21 at 06:04
  • 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. – Paul Jay Jun 10 '21 at 15:19
  • @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 `@username` – eyllanesc Jun 10 '21 at 22:52
  • @PaulJay An alternative and simpler solution is to use a QVideoWidget and place the QImages using the associated QAbstractVideoSurface. – eyllanesc Jun 10 '21 at 22:56
  • @eyllanesc 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? – Paul Jay Jun 11 '21 at 08:34
  • @eyllanesc A small example with a single image would be enough. – Paul Jay Jun 11 '21 at 08:44

1 Answers1

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();
Mostafa Mahmoud
  • 182
  • 1
  • 10