×
Namespaces

Variants
Actions
(Difference between revisions)

QML horizon component for camera apps

From Nokia Developer Wiki
Jump to: navigation, search
lildeimos (Talk | contribs)
(Lildeimos -)
lildeimos (Talk | contribs)
(Lildeimos - - Readings management)
Line 243: Line 243:
 
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
 
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
 
</code>
 
</code>
the slot is connected to {{Icode|onReadingChanged()}}. This function is connected only for the time needed to fill the list. Then the connection is 'redirected' to {{Icode|onReadingChanged2()}} where new readings will be stored sequentially (circulary?) in the list, averages are calculated and the {{Icode|paint()}} function called:
+
the slot is connected to {{Icode|onReadingChanged()}}. This function is connected only for the time needed to fill the list. Then the connection is 'redirected' to {{Icode|onReadingChanged2()}} where new readings will be stored sequentially (circulary?) in the list, averages are calculated and the reimplemented [http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#paint QGraphicsItem::paint()] function called:
 
<code>
 
<code>
 
// Slot called when the circular list isn't filled yet. When it is filled, change the connected slot to onReadingChanged2()
 
// Slot called when the circular list isn't filled yet. When it is filled, change the connected slot to onReadingChanged2()

Revision as of 00:36, 8 May 2012

This article explains how to use QML horizon line component

Article Metadata
Tested with
SDK: Nokia Qt SDK 1.2.0
Devices(s): Nokia C7-00, Nokia N950
Compatibility
Platform(s): Symbian^3 and later, Harmattan
Symbian
Device(s): All* (must have internal Accelerometer senso)
Article
Keywords: Horizon, QDeclarativeItem, QAccelerometer
Created: lildeimos (08 May 2012)
Last edited: lildeimos (08 May 2012)

Note.pngNote: This is an entry in the PureView Imaging Competition 2012Q2

Contents

Introduction

This QML component draws a overlay horizon line on top of the parent Item.

(sorry for bad quality... the screen shoot I used doesn't work with camera :) )

Overview

While developing this component, I chosen to use QAccelerometer sensor instead QRotationSensor. The reson is that some devices doesn't support Z-axis and for example my Nokia C7 has a snap of 15° deg of values. Should not be a big deal to replace the current accelerometer with the rotation sensor. Another problem I have encountered, is that the sensors are not calibrated all the same. To resolve this problem, I added 6 calibration properties (min and max values for each axis). The calibration properties in the example, are taken asking to the user to put the device in 3 different orientations. The drawback is that values are lost the next time the application run. A solution could be store those with QSettings as done in the OMCcam project.

Usage

Start copying horizon.cpp and horizon.h into your project source directory. In the main.cpp file include the .h:

#include "horizon.h"

then register the component:

qmlRegisterType<Horizon>("Horizon", 1, 0, "Horizon");

In you qml file you can now declare Horizon component:

Rectangle {
id: mainRect
Horizon {
id: horizon
anchors.fill: parent
clip: true
active: true
penWidth: 3
color: "black"
} // Horizon
}

active property will activate the accelerometer sensor to display a line on top of mainRect with a black color and a penWidth of 3 pixels. Horizon component has also {{Icode|calibrate[Min|Max][X|Y|Z]} (six properties) that are needed to calculate the position of the line. By default, min values are set to zero, max values are set to 9.8. Every devices need a sensor calibration, to do this, the user help is needed. The following code ask to the user to put the device in vertical portrait mode, then in landscape and finally horizontal with display facing upward:

Dialog {
id: calibratePortrait
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
text: "Put your phone in portrait and press Ok"
}
}
 
buttons: ButtonRow {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 30
Button {
width: 100
text: "OK"
onClicked: {
horizonMenu.open=false;
horizon.calibrateMinX=horizon.averageRotX;
horizon.calibrateMaxY=horizon.averageRotY;
horizon.calibrateMinZ=horizon.averageRotZ;
calibratePortrait.accept();
calibrateLandscape.open();
}
}
Button {width: 100; text: "Cancel"; onClicked: calibratePortrait.reject() }
}
} // calibratePortrait
 
 
Dialog {
id: calibrateLandscape
visualParent: mainPage
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
text: "Put your phone in landscape and press Ok"
}
}
 
buttons: ButtonRow {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 30
Button {
width: 100
text: "OK"
onClicked: {
horizon.calibrateMaxX=horizon.averageRotX;
horizon.calibrateMinY=horizon.averageRotY;
calibrateLandscape.accept();
calibratePlane.open();
}
}
Button {width: 100; text: "Cancel"; onClicked: calibrateLandscape.reject()}
}
} // calibrateLandscape
 
Dialog {
id: calibratePlane
visualParent: mainPage
title: Text {
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: 28
color: "white"
text: "Rotation sensor calibration"
}
 
content: Item {
height: 50
width: parent.width
Text {
font.pixelSize: 22
anchors.centerIn: parent
color: "white"
wrapMode: Text.WrapAnywhere
text: "Put your phone on a horizontal plane (face up) and press Ok"
}
}
 
buttons: ButtonRow {
spacing: 30
anchors.horizontalCenter: parent.horizontalCenter
Button {
width: 100
text: "OK"
onClicked: {
horizon.calibrateMaxZ=horizon.averageRotZ;
calibratePlane.accept();
}
}
Button {width: 100;text: "Cancel"; onClicked: calibratePlane.reject()}
}
} // calibrateLandscape

You can bind the open event of the first dialog ( calibratePortrait ) to a button, when accepted the second dialog will rise ans so for the third.

As you can see, here are appeared another 3 properties: averageRotX averageRotY averageRotZ. To avoid flickering of the horizon line, the component makes an average calculation of a defined number of samples of the accelerometer sensor. In these properties is stored the actual average of the last n sensor readings.

Implementation

I will explain here how I thought to implement Horizon. I remind you to the download section for the source code.

Readings management

In the header file is defined

#define ROT_SAMPLES 5

this defines the number of rotation readings to be averaged. As mentioned before, this is useful to avoid shaking of the horizon line since the readings values are very wobble.

I opted to use a circular list to store the last ROT_SAMPLES. This list is composed by this struct and declarations:

typedef struct listRot_ {
qreal value;
struct listRot_ *curr,*prev,*next;
} listRot;
 
listRot *rotX_samples[ROT_SAMPLES];
listRot *rotY_samples[ROT_SAMPLES];
listRot *rotZ_samples[ROT_SAMPLES];
listRot *currRotX;
listRot *currRotY;
listRot *currRotZ;

These 3 arrays are initialized in constructor:

m_rotation = new QAccelerometer();
 
for (int i = 0; i < ROT_SAMPLES; i++) {
rotX_samples[i] = new listRot;
rotY_samples[i] = new listRot;
rotZ_samples[i] = new listRot;
}
for (int i = 0; i < ROT_SAMPLES; i++) {
rotX_samples[i]->prev = (i>0 ? rotX_samples[i-1] : rotX_samples[ROT_SAMPLES]);
rotX_samples[i]->next = (i<ROT_SAMPLES-1 ? rotX_samples[i+1] : rotX_samples[0]);
rotX_samples[i]->value = i;
 
rotY_samples[i]->prev = (i>0 ? rotY_samples[i-1] : rotY_samples[ROT_SAMPLES]);
rotY_samples[i]->next = (i<ROT_SAMPLES-1 ? rotY_samples[i+1] : rotY_samples[0]);
rotY_samples[i]->value = i;
 
rotZ_samples[i]->prev = (i>0 ? rotZ_samples[i-1] : rotZ_samples[ROT_SAMPLES]);
rotZ_samples[i]->next = (i<ROT_SAMPLES-1 ? rotZ_samples[i+1] : rotZ_samples[0]);
rotZ_samples[i]->value = i;
}
currRotX = rotX_samples[0];
currRotY = rotY_samples[0];
currRotZ = rotZ_samples[0];
 
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));

the slot is connected to onReadingChanged(). This function is connected only for the time needed to fill the list. Then the connection is 'redirected' to onReadingChanged2() where new readings will be stored sequentially (circulary?) in the list, averages are calculated and the reimplemented QGraphicsItem::paint() function called:

// Slot called when the circular list isn't filled yet. When it is filled, change the connected slot to onReadingChanged2()
void Horizon::onReadingChanged()
{
// fill the circular list of samples
if (!m_reading) return;
if (!m_rotation->isActive() || m_rotation->isBusy()) return;
static int count = 0;
 
rotX_samples[count]->value = m_reading->x();
rotY_samples[count]->value = m_reading->y();
rotZ_samples[count]->value = m_reading->z();
 
// first ROT_SAMPLES samples are achieved, so onReadingChanged2() will be used next time
if (count>ROT_SAMPLES) {
disconnect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged()));
connect(m_rotation, SIGNAL(readingChanged()), this, SLOT(onReadingChanged2()));
}
 
count++;
}
 
// Slot called when the circular list is filled
void Horizon::onReadingChanged2()
{
// calculate an average of the last ROT_SAMPLES samples. This prevent horizon line to shake
if (!m_reading) return;
if (!m_rotation->isActive() || m_rotation->isBusy()) return;
 
currRotX->value = m_reading->x();
currRotY->value = m_reading->y();
currRotZ->value = m_reading->z();
 
for (int i=0; i<ROT_SAMPLES; i++, currRotX=currRotX->next, currRotY=currRotY->next, currRotZ=currRotZ->next) {
m_average_rotX += currRotX->value;
m_average_rotY += currRotY->value;
m_average_rotZ += currRotZ->value;
}
 
m_average_rotX /= ROT_SAMPLES;
m_average_rotY /= ROT_SAMPLES;
m_average_rotZ /= ROT_SAMPLES;
 
currRotX=currRotX->next->next;
currRotY=currRotY->next->next;
currRotZ=currRotZ->next->next;
 
update();
 
emit averageRotXChanged();
emit averageRotYChanged();
emit averageRotZChanged();
}

Horizon drawing

143 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×