如何在Ubuntu手機中使用前置照相機,ubuntu照相機

來源:互聯網
上載者:User

如何在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.