Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. VideoOutput + QQuickView::SizeRootObjectToView + Qt::PortraitOrientation

VideoOutput + QQuickView::SizeRootObjectToView + Qt::PortraitOrientation

Scheduled Pinned Locked Moved Solved Mobile and Embedded
videooutputqmlqcamerac++portrait
9 Posts 4 Posters 6.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dhirvonen
    wrote on 5 Jan 2016, 14:21 last edited by dhirvonen 1 May 2016, 15:26
    #1

    I'm trying to preview the front facing camera at maximum resolution in an upright orientation locked to the device's screen. This is using QT 5.5 in a QML application adapted from the qmlvideofilter sample.

    I'm using the following C++:

        view.setResizeMode( QQuickView::SizeRootObjectToView );
        view.reportContentOrientationChange(Qt::PortraitOrientation);
    

    And the VideoOutput + Camera QML shown below (see comments):

      Camera {
        id: camera
        position: Camera.FrontFace
        objectName: "CameraObject"
      }
    
      QTRenderGL {
        id: qtrendergl
      }
    
      VideoOutput {
        id: output    
        source: camera
        objectName: "VideoOutput"    
        focus : visible
        filters: [ infofilter, videofilter ]
        anchors.fill: parent
        anchors.centerIn: parent
        anchors.margins: 0
        fillMode: VideoOutput.PreserveAspectFit
    
        // Using this C++ for fixed upright orientation:
        //
        // QObject* qmlCamera = root->findChild<QObject*>("CameraObject");
        // QCamera* camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
        // QCameraInfo cameraInfo(*camera);
        // qmlVideoOutput->setProperty("rotation", cameraInfo.orientation());
        // rotation: 0
    
        // Enabling autoOrientation in combination with
        //
        //     fillMode: VideoOutput.PreserveAspectFit
        //
        // provides the stretch to fit behavior I am looking for, except I don't want the
        // varying device orientation landscale/portrait switching.  When I specify an
        // upright rotation manually (in C++ or QML) then the VideoOutput does not stretch
        // to fit the screen (i.e., no VideoOutput.PreserveAspectFit) properly.
        //
        //     autoOrientation: true
      }
    

    For configuration A (autoOrientation: true + fillMode.VideoOutput.PreserveAspectFit) I get the behavior I'm looking for when the phone is upright, but the screen switches to landscape orientation when I rotate the phone, which I don't want.

    When I use configuration B, basically fillMode.VideoOutput.PreserveAspectFit with a manually specified upright camera rotation, such as QML rotation: 90 or C++ qmlVideoOutput->setProperty("rotation", cameraInfo.orientation()) and view.reportContentOrientationChange(Qt::PortraitOrientation) then I get the upright camera output and locked screen orientation I want, but I don't see the aspect ratio preserving stretch.

    Here is an example of the output for configuration A.

    Here is an example of the output for configuration B (note the wide border).

    I feel I probably need to either: 1) find a QT way to override VideoOutput in QML/C++ (inheritance, signal/slot); or 2) leave the QML Camera + VideOutput (with filter) configured as is, since it is mostly working, but disable the final rendering and draw the frames directly in OpenGL using my own class via abeforeRendering() signal. The later option would have some advantages, since it would allow me to compensate for arbitrary geometric changes to the output QVideoFrame texture in my custom OpenGL VideoFilterRunnable without breaking the stretching behavior . (Any approach that gives me the desired output would be fine.) It seems the direct drawing could be accomplished using another class registered in QML and configured for the beforeRendering() signal like this:

    connect(window(), SIGNAL(beforeRendering()), m_renderer, SLOT(paint()), Qt::DirectConnection);
    

    if I can get my output QVideoFrame to the new rendering class. I grabbed some code as a place holder for this from the QT squircle sample in my project here. That still needs the QTVideoFrame input and a way to disable the defaultVideoOutputrendering. Any hints on this approach, or a fix for the defaultVideoOutput rendering would be greatly appreciated. The full sample (adapted from the qmlvideofilter) is available on github here.

    1 Reply Last reply
    1
    • C Offline
      C Offline
      Charby
      wrote on 6 Jan 2016, 08:17 last edited by
      #2

      Did you try scaling the video output when defining the rotation ?

      VideoOutput {
              id: viewfinder
              anchors.fill : parent
              source: camera
              rotation : 90
              transformOrigin: Item.Center
              fillMode : VideoOutput.PreserveAspectCrop
              scale : height/width
              
          }
      

      One could also use the "orientation" property of the VideoOutput.

      D 1 Reply Last reply 6 Jan 2016, 15:35
      2
      • C Charby
        6 Jan 2016, 08:17

        Did you try scaling the video output when defining the rotation ?

        VideoOutput {
                id: viewfinder
                anchors.fill : parent
                source: camera
                rotation : 90
                transformOrigin: Item.Center
                fillMode : VideoOutput.PreserveAspectCrop
                scale : height/width
                
            }
        

        One could also use the "orientation" property of the VideoOutput.

        D Offline
        D Offline
        dhirvonen
        wrote on 6 Jan 2016, 15:35 last edited by
        #3

        @Charby said:

        scale : height/width

        Excellent. scale : height/width is exactly what I needed. Thanks. Somehow I missed that (probably basic QML) property in my search. I'll need to set this adaptively in order to deal with different phones. The following C++ variation gives me exactly the fixed orientation and scale I wanted (I'm guessing this could also be done in QML) :

            QQuickItem* root = view.rootObject();
            
            QObject* qmlCamera = root->findChild<QObject*>("CameraObject");
            assert(qmlCamera != nullptr);
            
            QCamera* camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
            assert(camera != nullptr);
            
            QObject * qmlVideoOutput = root->findChild<QObject*>("VideoOutput");
            assert(qmlVideoOutput);
        
            const QCameraInfo cameraInfo(*camera);
            const bool hasTranspose = (cameraInfo.orientation() / 90) % 2;
            const auto &resolution = camera->viewfinderSettings().resolution();
            double scale = hasTranspose ? double(resolution.width())/resolution.height() : 1.0;
            qmlVideoOutput->setProperty("scale", scale);
            qmlVideoOutput->setProperty("rotation", cameraInfo.orientation());
        
        R 1 Reply Last reply 10 Jan 2016, 16:07
        0
        • C Offline
          C Offline
          Charby
          wrote on 6 Jan 2016, 16:43 last edited by
          #4

          Glad this helped !
          You are right, this would be achieved in QML (which would keep the C++ backend agnostic of the UI which is more in line with QtQuick I think...)

          I think something similar would go in QML :

              Camera {
                  id: camera
              }
              VideoOutput {
                  property bool hasTranspose : (camera.orientation / 90) % 2
                  anchors.fill : parent
                  source: camera
                  rotation : camera.orientation
                  transformOrigin: Item.Center
                  fillMode : VideoOutput.PreserveAspectCrop
                  scale : hasTranspose ? camera.viewfinder.resolution.height / camera.viewfinder.resolution.width : 1.0
                  
                  
              }
          

          Maybe the following could also be satisfactory :

          Camera {
                  id: camera
              }
              VideoOutput {
                  anchors.fill : parent
                  source: camera
                  orientation: camera.orientation
                  
              }
          
          D 1 Reply Last reply 7 Jan 2016, 06:29
          2
          • C Charby
            6 Jan 2016, 16:43

            Glad this helped !
            You are right, this would be achieved in QML (which would keep the C++ backend agnostic of the UI which is more in line with QtQuick I think...)

            I think something similar would go in QML :

                Camera {
                    id: camera
                }
                VideoOutput {
                    property bool hasTranspose : (camera.orientation / 90) % 2
                    anchors.fill : parent
                    source: camera
                    rotation : camera.orientation
                    transformOrigin: Item.Center
                    fillMode : VideoOutput.PreserveAspectCrop
                    scale : hasTranspose ? camera.viewfinder.resolution.height / camera.viewfinder.resolution.width : 1.0
                    
                    
                }
            

            Maybe the following could also be satisfactory :

            Camera {
                    id: camera
                }
                VideoOutput {
                    anchors.fill : parent
                    source: camera
                    orientation: camera.orientation
                    
                }
            
            D Offline
            D Offline
            dhirvonen
            wrote on 7 Jan 2016, 06:29 last edited by
            #5

            @Charby

            Even better (and much cleaner). After inverting the -camera.orientation it works beautifully. Thanks again.

              VideoOutput {
                id: output    
                source: camera
                objectName: "VideoOutput"    
                filters: [ infofilter, videofilter ]
                anchors.fill: parent
                orientation: -camera.orientation
              }
            
            DQUY05D 1 Reply Last reply 3 May 2021, 03:55
            0
            • D dhirvonen
              6 Jan 2016, 15:35

              @Charby said:

              scale : height/width

              Excellent. scale : height/width is exactly what I needed. Thanks. Somehow I missed that (probably basic QML) property in my search. I'll need to set this adaptively in order to deal with different phones. The following C++ variation gives me exactly the fixed orientation and scale I wanted (I'm guessing this could also be done in QML) :

                  QQuickItem* root = view.rootObject();
                  
                  QObject* qmlCamera = root->findChild<QObject*>("CameraObject");
                  assert(qmlCamera != nullptr);
                  
                  QCamera* camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
                  assert(camera != nullptr);
                  
                  QObject * qmlVideoOutput = root->findChild<QObject*>("VideoOutput");
                  assert(qmlVideoOutput);
              
                  const QCameraInfo cameraInfo(*camera);
                  const bool hasTranspose = (cameraInfo.orientation() / 90) % 2;
                  const auto &resolution = camera->viewfinderSettings().resolution();
                  double scale = hasTranspose ? double(resolution.width())/resolution.height() : 1.0;
                  qmlVideoOutput->setProperty("scale", scale);
                  qmlVideoOutput->setProperty("rotation", cameraInfo.orientation());
              
              R Offline
              R Offline
              Ruslan Baratov
              wrote on 10 Jan 2016, 16:07 last edited by
              #6

              @dhirvonen said:

              @Charby said:

              scale : height/width

              Excellent. scale : height/width is exactly what I needed. Thanks. Somehow I missed that (probably basic QML) property in my search.

              Actually I can't find this part in documentation too. There is no scale property in VideoOutput: http://doc.qt.io/qt-5/qml-qtmultimedia-videooutput.html In fact I know that VideoOutput inherits from QQuickItem: https://github.com/hunter-packages/Qt/blob/84a5846a20da62e2e2c73c1218927f8746c42de6/qtmultimedia/src/multimedia/qtmultimediaquicktools_headers/qdeclarativevideooutput_p.h#L54 QQuickItem has scale property: http://doc.qt.io/qt-5/qquickitem.html#scale-prop but I don't see height/width.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Charby
                wrote on 10 Jan 2016, 23:55 last edited by Charby 1 Oct 2016, 23:59
                #7

                scale, height and width are Item (qquickitem) inhereted properties.
                Just to avoid any misunderstanding : "height/width" is not a property name but just height divided by width.

                R 1 Reply Last reply 11 Jan 2016, 16:12
                1
                • C Charby
                  10 Jan 2016, 23:55

                  scale, height and width are Item (qquickitem) inhereted properties.
                  Just to avoid any misunderstanding : "height/width" is not a property name but just height divided by width.

                  R Offline
                  R Offline
                  Ruslan Baratov
                  wrote on 11 Jan 2016, 16:12 last edited by
                  #8

                  @Charby said:

                  "height/width" is not a property name but just height divided by width.

                  Okay, I see. Thanks for clarification. So the only one issue I see left here is that there is no such inherited members shown in documentation:

                  • VideoOutput (no scale member): http://doc.qt.io/qt-5/qml-qtmultimedia-videooutput-members.html
                  • Video (scale member shown): http://doc.qt.io/qt-5/qml-qtmultimedia-video-members.html
                  1 Reply Last reply
                  0
                  • D dhirvonen
                    7 Jan 2016, 06:29

                    @Charby

                    Even better (and much cleaner). After inverting the -camera.orientation it works beautifully. Thanks again.

                      VideoOutput {
                        id: output    
                        source: camera
                        objectName: "VideoOutput"    
                        filters: [ infofilter, videofilter ]
                        anchors.fill: parent
                        orientation: -camera.orientation
                      }
                    
                    DQUY05D Offline
                    DQUY05D Offline
                    DQUY05
                    wrote on 3 May 2021, 03:55 last edited by
                    #9

                    @dhirvonen said in VideoOutput + QQuickView::SizeRootObjectToView + Qt::PortraitOrientation:

                    @Charby

                    Even better (and much cleaner). After inverting the -camera.orientation it works beautifully. Thanks again.

                      VideoOutput {
                        id: output    
                        source: camera
                        objectName: "VideoOutput"    
                        filters: [ infofilter, videofilter ]
                        anchors.fill: parent
                        orientation: -camera.orientation
                      }
                    

                    Hi Mr,
                    I am having a similar project of yours, but do not understand the objectName property my VideoOutput does, is it because I use Qt5.15?

                    objectName: "VideoOutput" 
                    

                    Thanks!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved