Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Image zoom screenshot
Forum Updated to NodeBB v4.3 + New Features

QML Image zoom screenshot

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qt 5.7imagescreenshot
2 Posts 1 Posters 2.3k 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.
  • E Offline
    E Offline
    eiriham
    wrote on 30 Jun 2016, 07:20 last edited by eiriham
    #1

    Hey!

    I have an application where I take a picture. Afterwards I can view it, zoom in on it, view different parts while zoomed in etc.
    The next thing I want to do is that I want to be able to take a screenshot of the zoomed in image. I cant just take a screenshot of the screen because there are other elements on top of the image. So what I need is to have a snippet of my original image.

    This is my QML code:
    @
    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    import QtMultimedia 5.5
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Dialogs 1.2
    import "../components"

    //Snapshot View
    Item {
    id: snapshotViewItem
    visible: true
    width: mainWindow.width
    height: mainWindow.height

    //Flickable item to zoom and view different parts of the image
    Flickable{
        id: flick
    
        anchors.fill: parent
        contentWidth: parent.width
        contentHeight: parent.height
        boundsBehavior: Flickable.StopAtBounds
    
        rebound: Transition {
            NumberAnimation {
                properties: "x,y"
                duration: 0
            }
        }
    
    
    
        //Zoom on pinch
        PinchArea{
            id: pinchArea
            width: Math.max(flick.contentWidth, flick.width)
            height: Math.max(flick.contentHeight, flick.height)
            pinch.minimumScale: 1
            pinch.maximumScale: 10
            pinch.dragAxis: Pinch.XAndYAxis
    
            property real initialWidth
            property real initialHeight
    
            onPinchStarted: {
                initialWidth = initialWidth < flick.width ? flick.width : flick.contentWidth
                initialHeight = initialHeight < flick.height ? flick.height : flick.contentHeight
            }
    
            onPinchUpdated: {
    
                // resize content
                if(flick.contentWidth >= flick.width && flick.contentHeight >= flick.height){
                    flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center)
                }
            }
    
            onPinchFinished: {
                // Move its content within bounds.
                flick.returnToBounds()
    
                //Make sure we cant make the contentwidth and contentheight smaller than the actual window
                if(initialWidth < flick.width || initialHeight < flick.height){
                    initialWidth = flick.width
                    initialHeight = flick.height
                }
                if(flick.contentWidth < flick.width){
                    flick.contentWidth = flick.width
                }
                if(flick.contentHeight < flick.height){
                    flick.contentHeight = flick.height
                }
    
            }
    
            //The snapshot
            Image{
                id: snapshotImage
                anchors.fill: parent
                source: snapController.url
    
                //When press and hold on the image, it zooms out to normal size
                MouseArea {
                    anchors.fill: parent
                    scrollGestureEnabled: false
                    onPressAndHold: {
                        flick.contentWidth = flick.width
                        flick.contentHeight = flick.height
                    }
    
    
                }
    
            }
        }
    }
    }
    
    //Row layout for the buttons
    RowLayout{
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
    
    
        //New picture button
        RectangularButton{
            id: takePictureButton
            text: "Take a new picture"
            anchors.bottom: parent.bottom
            anchors.right: screenshotButton.left
            anchors.margins: 10
            onClicked: {
                flick.contentHeight = flick.height
                flick.contentWidth = flick.width
            }
    
        }
    
        //Screenshot button
        RectangularButton{
            id: screenshotButton
            text: "Screenshot"
            anchors.bottom: parent.bottom
            anchors.right: savePictureButton.left
            anchors.margins: 10
            onClicked: {
                
                console.log("Take screenshot")
            }
        }
    
    
    
        //Save picture button
        RectangularButton{
            id: savePictureButton
            text: "Save picture"
            anchors.bottom: parent.bottom
            anchors.right: exitButton.left
            anchors.margins: 10
            onClicked: {
    
            }
    
        }
    
        //Button to exit
        RectangularButton{
            id: exitButton
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            text: "Exit to mission view"
            anchors.margins: 10
    
        }
    }
    

    }

    @

    So my question is; How can I make that happen? Do I have to do it through c++, and how? Or is it possible to do directly through QML?

    Please help

    1 Reply Last reply
    0
    • E Offline
      E Offline
      eiriham
      wrote on 30 Jun 2016, 07:37 last edited by
      #2

      Got it to work.

      Only had to do this:
      @
      //Screenshot button
      RectangularButton{
      id: screenshotButton
      text: "Screenshot"
      anchors.bottom: parent.bottom
      anchors.right: savePictureButton.left
      anchors.margins: 10
      onClicked: {

                  //Take screenshot of the image
                  flick.grabToImage(function(result) {
                      snapController.url = result.url
                  }, Qt.size(snapshotViewItem.width,snapshotViewItem.height));
      
                  //Update zoom and update image to save
                  flick.contentHeight = flick.height
                  flick.contentWidth = flick.width
              }
          }
      

      @

      1 Reply Last reply
      0

      2/2

      30 Jun 2016, 07:37

      • Login

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