CameraPage.qml 8.23 KB
Newer Older
1

Jonah Brüchert's avatar
Jonah Brüchert committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/****************************************************************************
**
** Copyright (C) 2018 Jonah Brüchert
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
Jonah Brüchert's avatar
Jonah Brüchert committed
40 41 42 43
import QtQuick 2.7
import QtMultimedia 5.8
import org.kde.kirigami 2.0 as Kirigami
import QtQuick.Controls 2.0 as Controls
44
import QtQuick.Controls.Material 2.0
Jonah Brüchert's avatar
Jonah Brüchert committed
45
import QtQuick.Layouts 1.2
Jonah Brüchert's avatar
Jonah Brüchert committed
46
import QtGraphicalEffects 1.0
Jonah Brüchert's avatar
Jonah Brüchert committed
47 48 49

Kirigami.Page {
    id: cameraPage
Jonah Brüchert's avatar
Jonah Brüchert committed
50

Jonah Brüchert's avatar
Jonah Brüchert committed
51 52 53 54 55 56 57
    leftPadding: 0
    rightPadding: 0
    bottomPadding: 0
    topPadding: 0

    mainAction: Kirigami.Action {
        id: captureAction
Jonah Brüchert's avatar
Jonah Brüchert committed
58 59
        text: {
            if (camera.captureMode == Camera.CaptureStillImage)
Jonah Brüchert's avatar
Jonah Brüchert committed
60
                return qsTr("Capture photo")
Jonah Brüchert's avatar
Jonah Brüchert committed
61
            else if (camera.videoRecorder.recorderStatus == CameraRecorder.RecordingStatus)
Jonah Brüchert's avatar
Jonah Brüchert committed
62
                return qsTr("Stop recording video")
Jonah Brüchert's avatar
Jonah Brüchert committed
63
            else if (camera.captureMode == Camera.CaptureVideo)
Jonah Brüchert's avatar
Jonah Brüchert committed
64
                return qsTr("Start recording video")
Jonah Brüchert's avatar
Jonah Brüchert committed
65
        }
Jonah Brüchert's avatar
Jonah Brüchert committed
66 67
        iconName: {
            if (camera.captureMode == Camera.CaptureStillImage)
Jonah Brüchert's avatar
Jonah Brüchert committed
68
                return qsTr("camera-photo")
Jonah Brüchert's avatar
Jonah Brüchert committed
69
            else if (camera.videoRecorder.recorderStatus == CameraRecorder.RecordingStatus)
Jonah Brüchert's avatar
Jonah Brüchert committed
70
                return qsTr("window-close")
Jonah Brüchert's avatar
Jonah Brüchert committed
71
            else if (camera.captureMode == Camera.CaptureVideo)
Jonah Brüchert's avatar
Jonah Brüchert committed
72
                return qsTr("video-mp4")
Jonah Brüchert's avatar
Jonah Brüchert committed
73
        }
Jonah Brüchert's avatar
Jonah Brüchert committed
74 75 76
        onTriggered: {
            if (camera.captureMode == Camera.CaptureStillImage) {
                camera.imageCapture.capture()
Jonah Brüchert's avatar
Jonah Brüchert committed
77
                showPassiveNotification(qsTr("Took a photo"))
78
            } else if (camera.videoRecorder.recorderStatus == CameraRecorder.RecordingStatus) {
Jonah Brüchert's avatar
Jonah Brüchert committed
79 80
                camera.videoRecorder.stop()
                recordingFeedback.visible = false
Jonah Brüchert's avatar
Jonah Brüchert committed
81
                showPassiveNotification(qsTr("Stopped recording"))
82
            } else if (camera.captureMode == Camera.CaptureVideo) {
Jonah Brüchert's avatar
Jonah Brüchert committed
83 84
                camera.videoRecorder.record()
                recordingFeedback.visible = true
Jonah Brüchert's avatar
Jonah Brüchert committed
85
                showPassiveNotification(qsTr("Started recording"))
Jonah Brüchert's avatar
Jonah Brüchert committed
86 87
            }
        }
Jonah Brüchert's avatar
Jonah Brüchert committed
88
    }
89

Jonah Brüchert's avatar
Jonah Brüchert committed
90 91
    leftAction: Kirigami.Action {
        id: switchAction
Jonah Brüchert's avatar
Jonah Brüchert committed
92
        text: qsTr("Switch mode")
Jonah Brüchert's avatar
Jonah Brüchert committed
93 94 95 96 97 98
        iconName: "document-swap"
        onTriggered: {
            if (camera.captureMode == Camera.CaptureStillImage)
                camera.captureMode = Camera.CaptureVideo
            else
                camera.captureMode = Camera.CaptureStillImage
99

Jonah Brüchert's avatar
Jonah Brüchert committed
100 101 102
            console.log("Capture Mode switched")
        }
    }
Jonah Brüchert's avatar
Jonah Brüchert committed
103

Jonah Brüchert's avatar
Jonah Brüchert committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    Rectangle {
        id: cameraUI
        state: "PhotoCapture"

        anchors {
            fill: parent
            centerIn: parent
        }

        color: "darkgrey"

        states: [
            State {
                name: "PhotoCapture"
                StateChangeScript {
                    script: {
                        camera.captureMode = Camera.CaptureStillImage
                        camera.start()
                    }
                }
            },
            State {
                name: "VideoCapture"
                StateChangeScript {
                    script: {
                        camera.captureMode = Camera.CaptureVideo
                        camera.start()
                    }
                }
            }
        ]

        Camera {
            id: camera
            captureMode: Camera.CaptureStillImage
Jonah Brüchert's avatar
Jonah Brüchert committed
139
            imageProcessing.whiteBalanceMode: settings.whiteBalanceMode
Jonah Brüchert's avatar
Jonah Brüchert committed
140 141 142

            imageCapture {
                id: imageCapture
Jonah Brüchert's avatar
Jonah Brüchert committed
143
                resolution: settings.photoResolution
Jonah Brüchert's avatar
Jonah Brüchert committed
144 145 146 147 148
            }

            videoRecorder {
                id: videoRecorder
                resolution: settings.videoResolution
Jonah Brüchert's avatar
Jonah Brüchert committed
149
                frameRate: 30
Jonah Brüchert's avatar
Jonah Brüchert committed
150 151 152 153 154
            }
        }

        VideoOutput {
            id: viewfinder
155 156 157
            visible: cameraUI.state == "PhotoCapture"
                     || cameraUI.state == "VideoCapture"

Jonah Brüchert's avatar
Jonah Brüchert committed
158 159 160 161 162 163 164 165 166 167 168 169
            // Workaround
            orientation: Kirigami.Settings.isMobile ? -90 : 0

            anchors.fill: parent

            source: camera
        }

        PinchArea {
            anchors.fill: parent
            property real initialZoom
            onPinchStarted: {
170
                initialZoom = camera.digitalZoom
Jonah Brüchert's avatar
Jonah Brüchert committed
171 172
            }
            onPinchUpdated: {
173 174 175 176
                var scale = camera.maximumDigitalZoom / 8 * pinch.scale
                        - camera.maximumDigitalZoom / 8
                camera.setDigitalZoom(Math.min(camera.maximumDigitalZoom,
                                               camera.digitalZoom + scale))
Jonah Brüchert's avatar
Jonah Brüchert committed
177 178
            }
        }
Jonah Brüchert's avatar
Jonah Brüchert committed
179 180 181 182 183 184

        MouseArea {
            anchors.fill: parent

            onClicked: {
                if (camera.lockStatus == Camera.Unlocked) {
185
                    camera.searchAndLock()
Jonah Brüchert's avatar
Jonah Brüchert committed
186
                    console.log("searching focus...")
187 188
                } else {
                    camera.unlock()
Jonah Brüchert's avatar
Jonah Brüchert committed
189 190 191 192
                    console.log("unlocking focus...")
                }
            }
        }
Jonah Brüchert's avatar
Jonah Brüchert committed
193 194 195 196
    }

    Controls.Slider {
        orientation: Qt.Vertical
Jonah Brüchert's avatar
Jonah Brüchert committed
197

Jonah Brüchert's avatar
Jonah Brüchert committed
198 199 200 201
        from: 1
        value: camera.digitalZoom
        to: Math.min(4.0, camera.maximumDigitalZoom)

Jonah Brüchert's avatar
Jonah Brüchert committed
202
        height: Kirigami.Units.gridUnit * 20
Jonah Brüchert's avatar
Jonah Brüchert committed
203 204 205 206 207 208 209 210

        anchors {
            right: parent.right
            verticalCenter: parent.verticalCenter
        }

        onValueChanged: camera.setDigitalZoom(value)
    }
Jonah Brüchert's avatar
Jonah Brüchert committed
211 212 213 214 215 216 217 218 219

    Rectangle {
        id: recordingFeedback
        visible: false
        color: "red"
        radius: Kirigami.Units.gridUnit
        height: Kirigami.Units.gridUnit * 2
        width: height

Jonah Brüchert's avatar
Jonah Brüchert committed
220 221 222 223 224 225 226
        layer.enabled: recordingFeedback.enabled
        layer.effect: DropShadow {
            color: Material.dropShadowColor
            samples: 30
            spread: 0.5
        }

Jonah Brüchert's avatar
Jonah Brüchert committed
227 228 229 230 231 232
        anchors {
            left: parent.left
            top: parent.top
            margins: Kirigami.Units.gridUnit * 2
        }
    }
Jonah Brüchert's avatar
Jonah Brüchert committed
233 234 235 236 237 238 239 240 241

    Rectangle {
        id: preview
        visible: imageCapture.capturedImagePath
        width: Kirigami.Units.gridUnit * 6
        height: width
        layer.enabled: preview.enabled
        layer.effect: DropShadow {
            color: Material.dropShadowColor
242 243
            samples: 30
            spread: 0.5
Jonah Brüchert's avatar
Jonah Brüchert committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257
        }

        MouseArea {
            anchors.fill: parent
            onClicked: Qt.openUrlExternally(imageCapture.capturedImagePath)
        }

        anchors {
            right: parent.right
            bottom: parent.bottom
            margins: Kirigami.Units.gridUnit
        }

        Image {
Jonah Brüchert's avatar
Jonah Brüchert committed
258
            fillMode: Image.PreserveAspectCrop
Jonah Brüchert's avatar
Jonah Brüchert committed
259 260 261 262
            anchors.fill: parent
            source: imageCapture.capturedImagePath
        }
    }
Jonah Brüchert's avatar
Jonah Brüchert committed
263
}