Secondary camera in QML
This article explains how to use the secondary (front-facing) camera in QML
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Introduction
The reference library topic Using the device camera describes how to use the primary camera in various ways. However, access to the secondary camera is only available to C++ apps.
This article describes some options for using the secondary camera in QML.
Basic solution
The secondary camera (i.e. front-facing camera) is available though the C++ API. The basic solution is to create a declarative component and expose this to QML.
First include multimedia in the project file.
CONFIG += mobility
MOBILITY += multimedia
The C++ part of the FrontCameraApp component is shown below:
#include <QGraphicsVideoItem>
FrontCameraApp::FrontCameraApp(QDeclarativeItem *parent) : QDeclarativeItem(parent)
{
myViewfinder = new QGraphicsVideoItem(this);
QByteArray frontCamera = QCamera::availableDevices()[1];
myCamera = new QCamera(frontCamera);
myCamera->setViewfinder(myViewfinder);
myCamera->start();
}
Lastly the FrontCameraApp object has to be registered for QML in the main.cpp file.
qmlRegisterType<FrontCameraApp>("cz.vutbr.fit.pcmlich", 1, 0, "CameraFront");
Then use the new CameraFront element in QML is as follows:
import com.nokia.meego 1.0
import cz.vutbr.fit.pcmlich 1.0
Page {
CameraFront {
anchors.fill: parent;
}
}
Advanced solution/workaround
The basic solution above does not provide the methods which are in the QML Camera Element. If you need more advanced functionality through the front-facing camera one approach is to simply modify the existing QML Element for the main camera to work with the front-facing camera.
First download the source code from gitorious. The implementation of the Camera element is in /plugins/declarative/multimedia/qdeclarativecamera.cpp. Change the code as follows:
QDeclarativeCamera::QDeclarativeCamera(QDeclarativeItem *parent) :
QDeclarativeItem(parent),
m_camera(0),
m_viewfinderItem(0),
m_imageSettingsChanged(false),
m_pendingState(ActiveState),
m_isStateSet(false)
{
// m_camera = new QCamera(this); // remove this line
QByteArray frontCamera = QCamera::availableDevices()[1]; // add this line
m_Camera = new QCamera(frontCamera); // add this line
...
}
Next, it is necessary to add included private header files to your project.
#include "qdeclarativecamera_p.h"
#include "qdeclarativecamerapreviewprovider_p.h"
There could be a conflict in the class names QDeclarativeCamera and other problems, so it is recommended to perform refactoring of QDeclarativeCamera class. Finally, the modified class is registered into QML, as is described in the previous section.
Flip the image
The image from the secondary camera could be confusing to the user. Therefore it is appropriate to flip it.
transform: Rotation { origin.x: frontcam.width/2; origin.y: frontcam.height/2; axis { x: 0; y: 1; z: 0 } angle: 180 }
Limitations
The primary and secondary cameras cannot be opened at same time.
Summary
After these modifications, we have a QML component for the secondary camera which allows us modify white balance mode, exposure compensation, and other configuration of the secondary camera. It is also possible to capture images and monitor camera states. This solution is only a workaround, since the Qt Mobility is not providing a suitable interface for the secondary camera. All mentioned components are available in the attached file: Media:SecondaryCameraInQml.tar.gz


Contents
Hamishwillee - Also applicable to Symbian?
Hi
Interesting article. I like the fact that you've shown a couple of options. I've given this a basic subedit - please confirm you're still happy with it.
Would this work on Symbian too?
regards
Hamishhamishwillee 06:57, 18 June 2012 (EEST)
Xmlich02 -
All your changes seems to be okay.
The sample code should be working with symbian, however currently i have no device to test it.xmlich02 22:06, 18 June 2012 (EEST)
Kwhitefoot - Very good and very helpful, thanks.
I have a question but not directly about the code.
What license is this code released under? In fact what is the situation regarding code re-use for any of the code found on this site?
I see that the packaging files contain a GPL 2 license file but it is incomplete, authors names and copyright dates are missing.
I don't want to make a great fuss about this sort of thing, but just in case I make use of it and make a lot of money I'd like to be sure I won't be causing any trouble or annoyance.
Actually the chance of me making any money is small (most likely zero) but I don't want to tread on anyone's toes in the process even so.
Now to a real question about the code. The compiler complains:
They must only use lower-case letters, numbers, '-', '+' and '.'. We will try to work around that, but you may experience problems.
It installed fine so it is not even an inconvenience just now but how do I fix the problem?kwhitefoot 21:36, 25 October 2012 (EEST)
Xmlich02 - licence and problems with project name
(1) Licence - Feel free to use parts of code used in tutorial. They are provided as an text licensed under an public domain licence.The sample application attached to tutorial could contain some third party code obtained from some other project. In case you want to be 100% sure, that you are doing it right, please use GPL licence.
(2) The problems with unsupported characters could be solved by refactoring of the code. It could be done for this sample code easily, but I see a very few sense in fixing this warning. (I have better things to do in next 5 minutes)xmlich02 22:16, 25 October 2012 (EEST)
Kwhitefoot - Unsupported characters fixed
I created a new project called frontcam and copied most of the files into it, that seemed to fix the problem. GPL is what I will use if I ever publish anything.
Thanks again for a very clear article and also for providing a front camera class that correctly flips the picture regardless of the orientation of the device; there are several apps published in the Ovi store and elsewhere that turn the picture upside down if you rotate the phone!kwhitefoot 23:52, 25 October 2012 (EEST)
Hamishwillee - Licensing
Anything uploaded to the site is done so under the conditions here: http://www.developer.nokia.com/Terms_and_conditions/ , unless it explicitly states otherwise.
Regards
Hamishhamishwillee 08:28, 26 October 2012 (EEST)