Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-109039

Can't save QVideoFrame as an image from QML App

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.4.0, 6.5.0 Beta1, 6.7.3
    • Multimedia
    • None
    • armeabi-v7a
    • Android

    Description

      Hi
      I want to save QVideoFrame of QML MediaPlayer as an image.

      Since `videoFrame()` function of QVideoSink no accessible from QML, I have to use a C++ helper to access QVideoSink functionalities.

      C++
      this class use use Q_OBJECT and QML_ELEMENT

       

      void Helper::saveCurrentFrame(QObject *videoOutput)
      {
      if (m_sink == nullptr){
          m_sink = qvariant_cast<QVideoSink*>(videoOutput->property("videoSink"));
       }
         m_sink->videoFrame().toImage().save("frame.png");
      }
      

      QML

      Helper {
          id: helper
       }
      
      MediaPlayer {
          id: player
          videoOutput: videoOutput
      }
      
      VideoOutput {
          id: videoOutput
          anchors.fill: parent
      }
      Button {
          text: "save frame"
          onClicked: {
              helper.saveCurrentFrame(videoOutput)
       }
      }
      

       

      But this raise Cannot make QOpenGLContext current in a different thread
      and causes the App crash in both Windows and Android.

      I was able to hack this error with help of converting QVideoFrame to QImage manually :

       

      QVideoFrame currentFrame = m_sink->videoFrame();
      currentFrame.map(QVideoFrame::ReadOnly); 
      QImage::Format imageFormat = QVideoFrameFormat::imageFormatFromPixelFormat(currentFrame.pixelFormat());
      
      QImage image(currentFrame.bits(0),
                       currentFrame.width(),
                       currentFrame.height(),imageFormat);
      
      QImage delegateImage = image.scaled( 400, 200, Qt::KeepAspectRatio);
      delegateImage.save("frame.png")

       

      but this trick just works in Windows and not Android.
      in Android I'm still getting Cannot make QOpenGLContext current in a different thread.

      If you need full sample code just comment please, thank you Qt team.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              padubsky Pavel Dubsky
              spfl97 Dawn Walker
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes