如何在Ubuntu手機中使用前置照相機,ubuntu照相機
我們可以在Ubuntu QML的API文檔中看到Camera的用法,但是裡面沒有寫到任何的前置Camera的調用方法。對於一些應用來說,前置Camera的使用是重要的。我們必須使用Qt C++代碼來實現這個功能。在這篇文章中,我們來介紹如何使用Ubuntu手機中的前置照相機。
1)建立一個最基本的QML應用
這樣我們就產生了一個含有plugin的項目。
2)在項目中加入CameraSelector來選擇照相機
為了能夠調用Camera的一些API,我們在plugin中加入如下的CameraSelector類:
#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);private: 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);}
我們在“backend”中的CMakeLists.txt中加入“MultiMedia"庫以調用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 for 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/)
同時在backend.cpp中加入如下的句子:
qmlRegisterType<CameraSelector>(uri, 1, 0, "CameraSelector");
為了使用Camera,我們對我們的FrontCamera.qml做如下的修改:
import QtQuick 2.0import Ubuntu.Components 1.1import FrontCamera 1.0import QtMultimedia 5.0/*! \brief MainView with Tabs element. First Tab has a single Label and second Tab has a single ToolbarAction.*/MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "frontcamera.liu-xiao-guo" /* This property 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(100) height: units.gu(76) Page { title: i18n.tr("App with backend") Button { id: activateRearCamera text: "Rear Camera" onClicked: { selector. selectedCameraDevice = 0; camera.start(); } } Button { id: activateFrontCamera text: "Front camera" anchors.left : activateRearCamera.right anchors.leftMargin: units.gu(2) onClicked: { selector. selectedCameraDevice = 1; camera.start(); } } Camera { id: camera imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash exposure { exposureCompensation: -1.0 exposureMode: Camera.ExposurePortrait } flash.mode: Camera.FlashRedEyeReduction imageCapture { onImageCaptured: { 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: photoPreview } }}
運行我們的應用:
整個項目的源碼在:git clone https://gitcafe.com/ubuntu/frontcamera.git