×
Namespaces

Variants
Actions
(Difference between revisions)

Secondary camera in QML

From Nokia Developer Wiki
Jump to: navigation, search
xmlich02 (Talk | contribs)
(Xmlich02 -)
 
hamishwillee (Talk | contribs)
m (Text replace - "Category:Multimedia" to "")
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Draft]]
+
[[Category:Qt]][[Category:Qt Mobility]][[Category:MeeGo Harmattan]][[Category:Code Examples]][[Category:Camera]][[Category:Imaging]]
{{Abstract|This article explains how to use the secondary camera in QML }}
+
{{Abstract|This article explains how to use the secondary (front-facing) camera in QML }}
''Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use''
+
{{Note|This is an entry in the [[PureView Imaging Competition 2012Q2]]}}
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [[Media:SecondaryCameraInQml.tar.gz]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
+
|devices= N9
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|platform= Qt 4.6 and later
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
+
|devicecompatability= must have secondary camera
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
+
|dependencies= qt mobility
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|keywords= Camera, QML
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|creationdate= <!-- Format YYYYMMDD -->
+
|creationdate= 20120607
|author= <!-- Display as link [[User:username]] -->
+
|author= [[User:xmlich02]]
 
}}
 
}}
  
 
== Introduction ==
 
== Introduction ==
  
This article describes how to use the secondary camera in QML. The article [http://harmattan-dev.nokia.com/docs/library/html/guide/html/Developer_Library_Developing_for_Harmattan_Using_the_device_camera.html] describes how to use the primary camera in various ways. However, access to the secondary camera is not available in QML.
+
The reference library topic [http://harmattan-dev.nokia.com/docs/library/html/guide/html/Developer_Library_Developing_for_Harmattan_Using_the_device_camera.html 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.
  
== Current solution ==
+
This article describes some options for using the secondary camera in QML.
  
The secondary camera (i.e. frontal camera) is available though the C++ API. The registration of the component in QML is possible. However, this component does not provide the methods which are in the QML Camera component [http://doc.qt.nokia.com/qtmobility-1.2/qml-camera.html].
+
== Basic solution ==
  
<code lang=cpp>
+
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.
 +
<code>
 +
CONFIG += mobility
 +
MOBILITY += multimedia
 +
</code>
 +
 
 +
The C++ part of the FrontCameraApp component is shown below:
 +
 
 +
<code cpp-qt>
 
#include <QGraphicsVideoItem>
 
#include <QGraphicsVideoItem>
  
Line 47: Line 57:
 
</code>
 
</code>
  
The multimedia part from qt mobility has to be included in project.
+
Lastly the FrontCameraApp object has to be registered for QML in the main.cpp file.
  
<code>
+
<code cpp-qt>
CONFIG += mobility
+
    qmlRegisterType<FrontCameraApp>("cz.vutbr.fit.pcmlich", 1, 0, "CameraFront");
MOBILITY += multimedia
+
 
</code>
 
</code>
  
Also, the FrontCameraApp object has to be registered for QML.
 
 
<code cpp>
 
    qmlRegisterType<FrontCameraApp>("cz.vutbr.fit.pcmlich", 1, 0, "CameraFront");
 
</code>
 
  
The use in QML is as follows:
+
Then use the new {{Icode|CameraFront}} element in QML is as follows:
  
 
<code css>
 
<code css>
 
import com.nokia.meego 1.0
 
import com.nokia.meego 1.0
 
import cz.vutbr.fit.pcmlich 1.0
 
import cz.vutbr.fit.pcmlich 1.0
// import QtMultimediaKit 1.1
 
 
Page {
 
Page {
 
   CameraFront {  
 
   CameraFront {  
 
     anchors.fill: parent;
 
     anchors.fill: parent;
 
   }
 
   }
/*
 
// or primary camera
 
  Camera {
 
    anchors.fill: parent;
 
  }
 
*/
 
 
}
 
}
 
</code>
 
</code>
  
== Additional features ==
+
== Advanced solution/workaround ==
 +
The basic solution above does not provide the methods which are in the [http://doc.qt.nokia.com/qtmobility-1.2/qml-camera.html 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.
  
Since Qt mobility is open source, you can download the source codes from [http://qt.gitorious.org/qt-mobility]. The implementation of the Camera element is in /plugins/declarative/multimedia/qdeclarativecamera.cpp. In order to use of this class in your own project it is necessary to modify its code to work with secondary camera as follows:  
+
First download the source code from [http://qt.gitorious.org/qt-mobility gitorious]. The implementation of the Camera element is in /plugins/declarative/multimedia/qdeclarativecamera.cpp. Change the code as follows:
 
+
<code cpp-qt>
<code cpp>
+
 
QDeclarativeCamera::QDeclarativeCamera(QDeclarativeItem *parent) :
 
QDeclarativeCamera::QDeclarativeCamera(QDeclarativeItem *parent) :
 
     QDeclarativeItem(parent),
 
     QDeclarativeItem(parent),
Line 100: Line 97:
  
 
Next, it is necessary to add included private header files to your project.
 
Next, it is necessary to add included private header files to your project.
<code cpp>
+
<code cpp-qt>
 
#include "qdeclarativecamera_p.h"
 
#include "qdeclarativecamera_p.h"
 
#include "qdeclarativecamerapreviewprovider_p.h"
 
#include "qdeclarativecamerapreviewprovider_p.h"
 
</code>
 
</code>
  
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.
+
There could be a conflict in the class names {{Icode|QDeclarativeCamera}} and other problems, so it is recommended to perform refactoring of {{Icode|QDeclarativeCamera}} class. Finally, the modified class is registered into QML, as is described in the previous section.
  
== Problems ==
+
== Flip the image==
  
 
The image from the secondary camera could be confusing to the user. Therefore it is appropriate to flip it.  
 
The image from the secondary camera could be confusing to the user. Therefore it is appropriate to flip it.  
Line 114: Line 111:
 
         transform: Rotation { origin.x: frontcam.width/2; origin.y: frontcam.height/2; axis { x: 0; y: 1; z: 0 } angle: 180  }
 
         transform: Rotation { origin.x: frontcam.width/2; origin.y: frontcam.height/2; axis { x: 0; y: 1; z: 0 } angle: 180  }
 
</code>
 
</code>
 +
 +
== Limitations ==
  
 
The primary and secondary cameras cannot be opened at same time.
 
The primary and secondary cameras cannot be opened at same time.
Line 119: Line 118:
 
== Summary ==
 
== 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 [FIXME]
+
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]]

Latest revision as of 08:00, 14 June 2013

This article explains how to use the secondary (front-facing) camera in QML

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

Article Metadata
Code ExampleTested with
Devices(s): N9
Compatibility
Platform(s): Qt 4.6 and later
Device(s): must have secondary camera
Dependencies: qt mobility
Article
Keywords: Camera, QML
Created: xmlich02 (07 Jun 2012)
Last edited: hamishwillee (14 Jun 2013)

Contents

[edit] 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.

[edit] 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;
}
}

[edit] 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.

[edit] 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  }

[edit] Limitations

The primary and secondary cameras cannot be opened at same time.

[edit] 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

This page was last modified on 14 June 2013, at 08:00.
93 page views in the last 30 days.
×