Pong
September 1st 2014, Qt Swipe Camera
← August 31th 2014 Qt Android Activity Pausing | ● | September 1st 2014 Qt GPS Camera →
What I always wanted for my smartphone camera was swipe gesture support. My preference is the following behavior:
- click: search and focus
- double click: capture
- swipe left: exposure compensation −1 Ev
- swipe right: exposure compensation +1 Ev
- swipe up: digital zoom +50%
- swipe down: reset of digital zoom and exposure compensation
To implement the above swipe behavior we first add a new SwipeArea QML module, which behaves much like a MouseArea, but adds swipe signals on top:
// original from https://gist.github.com/kovrov
// adapted and improved by Stefan Roettger
import QtQuick 2.0
MouseArea {
property int clicks: 0
property point origin
property double originTime
signal clicked()
signal doubleClicked()
signal move(int x, int y)
signal swipe(string direction, double speed)
onPressed: {
drag.axis = Drag.XAndYAxis
origin = Qt.point(mouse.x, mouse.y)
originTime = new Date().valueOf()
}
onPositionChanged: {
switch (drag.axis) {
case Drag.XAndYAxis:
if (Math.abs(mouse.x - origin.x) > 16) {
drag.axis = Drag.XAxis
}
else if (Math.abs(mouse.y - origin.y) > 16) {
drag.axis = Drag.YAxis
}
break
case Drag.XAxis:
move(mouse.x - origin.x, 0)
break
case Drag.YAxis:
move(0, mouse.y - origin.y)
break
}
}
onReleased: {
var delta = new Date().valueOf() - originTime
switch (drag.axis) {
case Drag.XAndYAxis:
if (++clicks > 1) {
clicks = 0
clickTimer.running = false
doubleClicked()
}
else {
clickTimer.running = true
}
break
case Drag.XAxis:
swipe(mouse.x - origin.x < 0 ? "left" : "right",
Math.abs(mouse.x - origin.x) / delta)
break
case Drag.YAxis:
swipe(mouse.y - origin.y < 0 ? "up" : "down",
Math.abs(mouse.y - origin.y) / delta)
break
}
}
Timer {
id: clickTimer
interval: 350; running: false; repeat: false
onTriggered: {
clicks = 0
clicked()
}
}
}
// adapted and improved by Stefan Roettger
import QtQuick 2.0
MouseArea {
property int clicks: 0
property point origin
property double originTime
signal clicked()
signal doubleClicked()
signal move(int x, int y)
signal swipe(string direction, double speed)
onPressed: {
drag.axis = Drag.XAndYAxis
origin = Qt.point(mouse.x, mouse.y)
originTime = new Date().valueOf()
}
onPositionChanged: {
switch (drag.axis) {
case Drag.XAndYAxis:
if (Math.abs(mouse.x - origin.x) > 16) {
drag.axis = Drag.XAxis
}
else if (Math.abs(mouse.y - origin.y) > 16) {
drag.axis = Drag.YAxis
}
break
case Drag.XAxis:
move(mouse.x - origin.x, 0)
break
case Drag.YAxis:
move(0, mouse.y - origin.y)
break
}
}
onReleased: {
var delta = new Date().valueOf() - originTime
switch (drag.axis) {
case Drag.XAndYAxis:
if (++clicks > 1) {
clicks = 0
clickTimer.running = false
doubleClicked()
}
else {
clickTimer.running = true
}
break
case Drag.XAxis:
swipe(mouse.x - origin.x < 0 ? "left" : "right",
Math.abs(mouse.x - origin.x) / delta)
break
case Drag.YAxis:
swipe(mouse.y - origin.y < 0 ? "up" : "down",
Math.abs(mouse.y - origin.y) / delta)
break
}
}
Timer {
id: clickTimer
interval: 350; running: false; repeat: false
onTriggered: {
clicks = 0
clicked()
}
}
}
With the above QML module we can implement a camera that sets the exposure compensation via left and right swipes:
Camera {
id: camera
videoRecorder {
resolution: "640x480"
frameRate: 30
}
}
VideoOutput {
id: viewfinder
anchors.fill: parent;
source: camera
autoOrientation: true
SwipeArea {
anchors.fill: parent;
onClicked: {
camera.searchAndLock()
}
onDoubleClicked: {
camera.imageCapture.capture()
}
onSwipe: {
if (direction == "left") {
camera.exposure.exposureCompensation -= 1.0
}
else if (direction == "right") {
camera.exposure.exposureCompensation += 1.0
}
else if (direction == "down") {
camera.exposure.exposureCompensation = 0.0
}
}
}
}
id: camera
videoRecorder {
resolution: "640x480"
frameRate: 30
}
}
VideoOutput {
id: viewfinder
anchors.fill: parent;
source: camera
autoOrientation: true
SwipeArea {
anchors.fill: parent;
onClicked: {
camera.searchAndLock()
}
onDoubleClicked: {
camera.imageCapture.capture()
}
onSwipe: {
if (direction == "left") {
camera.exposure.exposureCompensation -= 1.0
}
else if (direction == "right") {
camera.exposure.exposureCompensation += 1.0
}
else if (direction == "down") {
camera.exposure.exposureCompensation = 0.0
}
}
}
}
Full source code here:
← August 31th 2014 Qt Android Activity Pausing | ● | September 1st 2014 Qt GPS Camera →