如何在Ubuntu手机中使用前置照相机

我们可以在Ubuntu QML的中看到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_OBJECTQ_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 controlQMediaService *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 unloadedm_camera->unload();m_deviceSelector->setSelectedDevice(cameraId);}

我们在“backend”中的CMakeLists.txt中加入“MultiMedia"库以调用QCamera。

include_directories(${CMAKE_CURRENT_SOURCE_DIR})set(FrontCamerabackend_SRCSmodules/FrontCamera/backend.cppmodules/FrontCamera/mytype.cppmodules/FrontCamera/cameraselector.cpp)add_library(FrontCamerabackend MODULE${FrontCamerabackend_SRCS})set_target_properties(FrontCamerabackend PROPERTIESLIBRARY_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 ALLCOMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/modules/FrontCamera/qmldir ${CMAKE_CURRENT_BINARY_DIR}/FrontCameraDEPENDS ${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 andsecond 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 manifestapplicationName: "frontcamera.liu-xiao-guo"/*This property enables the application to change orientationwhen the device is rotated. The default is false.*///automaticOrientation: true// Removes the old toolbar and enables new features of the new header.useDeprecatedToolbar: falsewidth: units.gu(100)height: units.gu(76)Page {title: i18n.tr("App with backend")Button {id: activateRearCameratext: "Rear Camera"onClicked: {selector. selectedCameraDevice = 0;camera.start();}}Button {id: activateFrontCameratext: "Front camera"anchors.left : activateRearCamera.rightanchors.leftMargin: units.gu(2)onClicked: {selector. selectedCameraDevice = 1;camera.start();}}Camera {id: cameraimageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlashexposure {exposureCompensation: -1.0exposureMode: Camera.ExposurePortrait}flash.mode: Camera.FlashRedEyeReductionimageCapture {onImageCaptured: {photoPreview.source = preview // Show the preview in an Image}}}CameraSelector {id: selectorcameraObject: camera}VideoOutput {source: cameraanchors.fill: parentfocus : visible // to receive focus and capture key events when visible}Image {id: photoPreview}}}

运行我们的应用:

明天是世上增值最快的一块土地,因它充满了希望

如何在Ubuntu手机中使用前置照相机

相关文章:

你感兴趣的文章:

标签云: