We can see the use of the camera in the Ubuntu QML API documentation, but there is no way to write any of the front camera calls. For some applications, the use of the front-facing camera is important. We must use QT C + + code to implement this function. In this article, we'll show you how to use a front-facing camera in an Ubuntu phone.
1) Create one of the most basic QML applications
This allows us to generate a project that contains plugin.
2) Add Cameraselector to the project to select the camera
To be able to invoke some of the camera's APIs, we add the following Cameraselector class to the plugin:
#ifndef camera_selector_h#define camera_selector_h#include <QObject> #include <QCamera> #include < Qvideodeviceselectorcontrol>class cameraselector:public qobject{ q_object q_property (QObject* Cameraobject read Cameraobject WRITE setcameraobject) q_property (int selectedcameradevice READ Selectedcameradevice WRITE setselectedcameradevice) public: qobject* cameraobject () const; void Setcameraobject (Qobject *cam); int selectedcameradevice () const; void Setselectedcameradevice (const int cameraid);p rivate: qcamera *m_camera; Qvideodeviceselectorcontrol *m_deviceselector;}; #endif//Camera_selector_h
#include "cameraselector.h" #include <qmediaservice>void cameraselector::setcameraobject (qobject *cam) { / /Get the Qcamera from the declarative camera's Mediaobject property. M_camera = qvariant_cast<qcamera*> (Cam->property ("Mediaobject")); Get the video Device selector control qmediaservice *service = M_camera->service (); M_deviceselector = qobject_cast<qvideodeviceselectorcontrol*> (Service->requestcontrol ( QVIDEODEVICESELECTORCONTROL_IID));} Qobject *cameraselector::cameraobject () const{ return m_camera;} int Cameraselector::selectedcameradevice () const{ return 0;} void Cameraselector::setselectedcameradevice (const int cameraid) { //A camera might already be started, make sure it ' s Unloaded m_camera->unload (); M_deviceselector->setselecteddevice (Cameraid);}
We add the "multimedia" library to the CMakeLists.txt in "backend" to invoke Qcamera.
Include_directories ( ${cmake_current_source_dir}) Set ( Frontcamerabackend_srcs modules/frontcamera/ Backend.cpp modules/frontcamera/mytype.cpp modules/frontcamera/cameraselector.cpp) add_library ( Frontcamerabackend MODULE ${frontcamerabackend_srcs}) set_target_properties (Frontcamerabackend properties library_output_directory Frontcamera) qt5_use_modules (frontcamerabackend Gui Qml Quick Multimedia) # Copy Qmldir File to build dir-running in Qtcreatoradd_custom_target (Frontcamerabackend-qmldir all COMMAND CP ${cmake_current _source_dir}/modules/frontcamera/qmldir ${cmake_current_binary_dir}/frontcamera DEPENDS ${QMLFILES}) # Install Plugin Fileinstall (TARGETS frontcamerabackend DESTINATION ${qt_imports_dir}/frontcamera/) Install (FILES modules /frontcamera/qmldir DESTINATION ${qt_imports_dir}/frontcamera/)
Also add the following sentences to the Backend.cpp:
Qmlregistertype<cameraselector> (URI, 1, 0, "Cameraselector");
In order to use camera, we make the following changes to our FRONTCAMERA.QML:
Import QtQuick 2.0import ubuntu.components 1.1import frontcamera 1.0import qtmultimedia 5.0/*! \brief MainView with Tabs element. First Tab have a single Label and second Tab have a single Toolbaraction.*/mainview {//ObjectName for Functio NAL testing purposes (AUTOPILOT-QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Frontcamera.liu-xiao-guo"/* The enables the application to change orientation when the device is rotated. The default is False. *///automaticorientation:true//Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false Width:units.gu (Height:units.gu) Page {title:i18n.tr ("App with" End ") button {Id:activaterearcamera text:" Rear Camera "onclicked: { Selector. Selectedcameradevice = 0; CamerA.start (); }} button {Id:activatefrontcamera text: "Front camera" Anchors.left:ac Tivaterearcamera.right Anchors.leftMargin:units.gu (2) onclicked: {selector. selecte Dcameradevice = 1; Camera.start (); }} Camera {Id:camera ImageProcessing.whiteBalanceMode:CameraImageProcessing.WhiteB Alanceflash Exposure {exposurecompensation: -1.0 exposureMode:Camera.ExposurePo Rtrait} flash.mode:Camera.FlashRedEyeReduction imagecapture {Onimagecap tured: {photopreview.source = preview//Show the preview in an Image}} } cameraselector {Id:selector Cameraobject:camera} videooutput { Source:camera anchors.fill:parent Focus:visible//To receive focus and capture key events when visible} Image {Id:photoprev Iew}}}
To run our application:
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/frontcamera.git
How to use a front-facing camera in an Ubuntu phone