Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved QCameraViewfinder Widget Size/Aspect Ratio

    General and Desktop
    qcamera
    2
    5
    3350
    Loading More Posts
    • 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.
    • Andy314
      Andy314 last edited by Andy314

      Hello,
      I must take a photo on a windows tablet.

      The way is to put a QWidget on the form and promote it to a QCameraViewFinder.
      I test it with a Logitech webcam, but have some aspect-ration problems with this approach. The in the QTCreator included Example has the same problem.
      When the program starts it seem that the ViewFinder has the size-ratio of the picture (I dont kown why - coincidentally for my camera ? found no code for it! ).
      When I resize the window f.e. to smaller size horizontal, the width of the ViewFinder shrinks correct to the size of its parent QStackedPanel. The height of the ViewFinder (or only its picture) shrinks too, but not in the correct ratio. It is slower so that I get after some shrinking a vertical stretched picture.
      How can I get a picture with a correct aspect ratio?
      What has the ViewFinder for a size policy or internal drawing function.

      Andy314 1 Reply Last reply Reply Quote 0
      • Andy314
        Andy314 @Andy314 last edited by Andy314

        The QCamaraViewFinder seem total buggy. I found additional effects.
        When its width is smaller than the video width it shows an stretched active video at bottom (updated by camera) with the correct aspect ratio but cutted at top
        and above it a stable area which shows the picture at the moment of resizing (not updated).

        If the vertical size is smaller all works correct as expected.
        Here is the docu for ui->ViewFinder->setAspectRatioMode(Qt::KeepAspectRatio);
        Qt::KeepAspectRatio 1 The size is scaled to a rectangle as large as possible inside a given rectangle, **preserving ** the aspect ratio.

        QML demo works in all cases correct so that I think this is definitely a bug in the QCameraViewFinder - espacialy because the vertical size scales. The intention was to scale it but the formula is wrong.

        What can I do ?
        An other problem is to rotate the image when I rotate the tablet.

        Charby 1 Reply Last reply Reply Quote 0
        • Charby
          Charby @Andy314 last edited by

          @Andy314 May be using QML Camera is the way to go. That's what I did for a previous project running on iOS and Android for taking picture and reading QrCode and it was pretty OK.
          I remember though having had to customize things to have the correct orientation...this what I did for the viewfinder :

          VideoOutput {
                  id: viewfinder
                  ..
                  source: camera
                  autoOrientation: Qt.platform.os == 'ios' ? false : true
                  orientation: camera.orientation
          }
          

          This should well behave with the rotation of the tablet (at least it is the behavior I wanted)
          If you need better control, you need to use sensors, here is a code snippet for rotating the picture taken by the camera, it may be not satisfactory but could help you to give you new ideas :

          
           import QtSensors 5.3
          
          OrientationSensor{
                  id:orient
                  active:true
                  property int rotAngle :90
                  property bool isPortrait : true
                  onReadingChanged: {
                      if (reading.orientation === OrientationReading.LeftUp)
                      {
                          rotAngle = -90;
                          isPortrait = false;
                      }
                      else if (reading.orientation === OrientationReading.RightUp)
                      {
                          rotAngle = 90;
                          isPortrait = false;
                      }
                      else if (reading.orientation === OrientationReading.TopDown)
                      {
                          isPortrait = true;
                          rotAngle = 180;
                      }
                      else {
                          isPortrait = true;
                          rotAngle = 0;
                      }
          
                  }
              }
          
          Image{
                      property alias isPortrait : orient.isPortrait
                      property int rotAngle :orient.rotAngle
                      id:previewImage
                      width:parent.width;
                      height:parent.height;
                      autoTransform:true
                      rotation : rotAngle//Qt.platform.os == 'ios' ? 90 : 0
                      transformOrigin: Item.Center
                      fillMode : Image.PreserveAspectFit
                      scale : isPortrait ? 1.0 :  height/width
                      /*onRotationChanged: {
                          //width = parent.width;
                          //height = parent.height;
                          console.log("Image portrait" + isPortrait)
                          console.log("current orientations is portrait" + orient.isPortrait)
                          if (isPortrait)
                          {
          
                              if (orient.isPortrait)
                              {
                                  scale=1;
                              }
                              else{
                                  scale=1;
          
                              }
                          }
                          else
                          {
                              if (orient.isPortrait)
                              {
                                  scale=1;
                              }
                              else{
                                  scale=height/width;
          
                              }
                          }
                      }*/
          
                  }
          
          Andy314 1 Reply Last reply Reply Quote 1
          • Andy314
            Andy314 @Charby last edited by

            Hello @Charby,
            the QML-Camera indeed works better.
            The autoOrientation property is described in Qt 5.5 documentation but it will be marked as a unknown property in the QML-editor. Nevertheless I get no error when I call the file.
            So I think that is an error in the intellisense,
            but it works not. The picture keeps its orientation.

            Charby 1 Reply Last reply Reply Quote 0
            • Charby
              Charby @Andy314 last edited by

              @Andy314 If using autoOrientation is not satisfactory I would suggest to change the orientation manually.
              You can see an example of how to achieve this in our last meetup materials (https://forum.qt.io/topic/63838/qt-qml-workshop-materials/4), the demo project can also be seen on github.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post