如何在QML中使用camera API来拍照

在先前的例程中“如何使用Ubuntu手机平台中的照相机API来存储照片”,我们已经展示了如何使用Item的属性来存储我们的照片。在这篇文章中,我们将使用Camera
API
来完成同样的功能。

我们来直接贴自己的代码:

main.qml

import QtQuick 2.0
import Ubuntu.Components 1.1
import QtMultimedia 5.0

/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "cameraapp.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(60)
    height: units.gu(85)

    property var resolution

    // This function is used to get the writable private directory of this app
    function getPriateDirectory() {
        var sharepath = "/home/phablet/.local/share/";
        var path = sharepath + applicationName;
        console.log("path: " + path);
        return path;
    }

    Page {
        id: page
        title: i18n.tr("cameraapp")

        Camera {
            id: camera

            imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash

            exposure {
                exposureCompensation: -1.0
                exposureMode: Camera.ExposurePortrait
            }

            flash.mode: Camera.FlashRedEyeReduction

            imageCapture {
                onImageCaptured: {
                    console.log("image captured! reqId: " + requestId)
                    image.source = preview  // Show the preview in an Image
                }

                onImageSaved: {
                    console.log("image has been saved: " + requestId);
                    console.log("saved path: " + path);
                    image.source = path;
                }
            }

            Component.onCompleted: {
                resolution = camera.viewfinder.resolution;
                console.log("resolution: " + resolution.width + " " + resolution.height);
            }
        }

        Row {
            id: container
            VideoOutput {
                id: video
                source: camera
                width: page.width
                height: page.height
                focus : visible // to receive focus and capture key events when visible
                orientation: -90

                //                Image {
                //                    id: photoPreview
                //                    anchors.fill: parent
                //                    rotation: -90
                //                }

                SwipeArea {
                    anchors.fill: parent
                    onSwipe: {
                        console.log("swipe happened!: " + direction)
                        switch (direction) {
                        case "left":
                            page.state = "image";
                            break
                        }
                    }
                }
            }

            Item {
                id: view
                width: page.width
                height: page.height

                Image {
//                    anchors.fill: parent
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    width: parent.height
                    height: parent.width
                    id: image
                    rotation: 90
                    fillMode: Image.PreserveAspectFit
                }

                Text {
                    text: image.source
                    color:"red"
                    font.pixelSize: units.gu(2.5)
                    width: page.width
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                }

                SwipeArea {
                    anchors.fill: parent
                    onSwipe: {
                        console.log("swipe happened!: " + direction)
                        switch (direction) {
                        case "right":
                            page.state = "";
                            break
                        }
                    }
                }
            }
        }

        states: [
            State {
                name: "image"
                PropertyChanges {
                    target: container
                    x:-page.width
                }
                PropertyChanges {
                    target: capture
                    opacity:0
                }
            }
        ]

        transitions: [
            Transition {
                NumberAnimation { target: container; property: "x"; duration: 500
                    easing.type:Easing.OutSine}
                //                NumberAnimation { target: inputcontainer; property: "opacity"; duration: 200}
                NumberAnimation { target: capture; property: "opacity"; duration: 200}
            }
        ]

        Button {
            id: capture
            anchors.bottom: parent.bottom
            anchors.bottomMargin: units.gu(1)
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Capture"

            onClicked: {
                console.log("capture path: " + getPriateDirectory());
                camera.imageCapture.captureToLocation(getPriateDirectory());
                page.state = "image"
            }
        }
    }
}

在这里我们必须指出的是我们可以使用:

camera.imageCapture.captureToLocation(getPriateDirectory());

来把照片存储到我们指定的位置。这个位置可以是手机应用自己的私有的目录或它下面的子目录。

我们可以在如下的代码中得到照相机存储的文件信息:

           imageCapture {
                onImageCaptured: {
                    console.log("image captured! reqId: " + requestId)
                    image.source = preview  // Show the preview in an Image
                }

                onImageSaved: {
                    console.log("image has been saved: " + requestId);
                    console.log("saved path: " + path);
                    image.source = path;
                }
            }

通过得到的path信息,我们可以得到照片的位置,并显示它。

在我们的设计中,我们可以使用swipe向左或向右来切换viewFinder及拍照的相片。

运行我们的应用,我们可以看到如下的图片:

  

整个项目的源码在:git clone https://gitcafe.com/ubuntu/cameraapp.git

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-30 06:57:55

如何在QML中使用camera API来拍照的相关文章

在Ubuntu手机平台中使用Camera API来录像

在前面的文章"如何在QML中使用camera API来拍照"中,我们介绍了如何使用Camera API来进行拍照.今天我们在这篇文章中来介绍如何使用Camera API来进行录像. 首先,还是和以前一样,我直接把自己的代码贴出来: main.qml import QtQuick 2.0 import Ubuntu.Components 1.1 import QtMultimedia 5.0 /*! \brief MainView with a Label and Button elem

如何在QML中使用multitouch

在Qt QML中,它可以利用multitouch来做一些我们想做的事情.在今天的文章中,我们将介绍如何使用multitouch来做一些我们想做的事. 其实,在QML中利用多点触控是非常容易的一件事.我们下面直接来展示我们的例程来给大家讲解一下: import QtQuick 2.0 import Ubuntu.Components 1.1 /*! \brief MainView with a Label and Button elements. */ MainView { // objectNa

如何在QML中利用Sprite来做我们需要的动画

在游戏中动画的设计非常中要.在QML中,它提供了丰富的animation,但是有时我们需要对图像进行变化,就像放电影一样.在今天的这篇文章中,我们将设计一个可以变化图像的动画.我们可以通过Qt所提供的Sprite功能来实现. 为了设计的方便,我们先设计一个我们自己的bear动画,这个动画的图像大小为: 2048x256.它刚好是8副图256x256 在我们的Sprite设计中,我们想按照上述图像显示的顺序依次显示每个图像,这样就可以形成一个可以连续变化的动画效果. 直接把我们的动画设计文件Bea

Android 如何在Eclipse中查看Android API源码 及 support包源码

当我们阅读android API开发文档时候,上面的每个类,以及类的各个方法都是已经写好的方法和控件,可是我们只是在搬来使用,不知道它的原理,它是如何被实现的.android系统是开源的,所以谷歌官方在每发布一个版本的时候都会放出它对应的API源码的,让我们可以深入了解android的API实现过程,这就是开源的魅力.如果我们从API源码的角度去了解了开发过程,那样对于作为开发人员的我们,便会对他有更深入的体会,有助于日后的软件开发. 比如查看Activity的源码,如图 也可以查看系统方法怎么

【转】Android 如何在Eclipse中查看Android API源码 及 support包源码

原文网址:http://blog.csdn.net/vipzjyno1/article/details/22954775 当我们阅读android API开发文档时候,上面的每个类,以及类的各个方法都是已经写好的方法和控件,可是我们只是在搬来使用,不知道它的原理,它是如何被实现的.android系统是开源的,所以谷歌官方在每发布一个版本的时候都会放出它对应的API源码的,让我们可以深入了解android的API实现过程,这就是开源的魅力.如果我们从API源码的角度去了解了开发过程,那样对于作为开

如何在QML中使用ListView并导航到其它页面中

我们知道ListView在QML应用中扮演非常重要的角色.看看我们的很多的应用都是在使用ListView.那么当我们点击ListView中的item并导航到另外一个页面呢?其实这样的方法有很多.在这篇文章中,我们来介绍其中的几种.开发者可以参照其中的设计,或自己想出更好的设计. 1)使用PageStack来完成 在我们的RssReader中的例子中,我们使用了PageStack来完成我们的导航.我们可以把我们的每个页面都做成我们的Page.当我们的页面被点击后,我们把新的Page压入栈中.在返回

如何在React中处理REST API请求

REST API通常用于Web开发中.它们是Web应用程序用来彼此"交谈"的编程接口.它们用于访问功能部件和数据." REST"(代表性状态转移)是定义API属性的概念.本文将重点介绍如何使用基于Web的API从数据库中检索数据. Axios是一个npm软件包,允许应用程序将HTTP请求发送到Web API.要在您的React应用程序中使用Axios,请使用以下命令: npm install axios 要么 yarn add axios 在创建react组件之前,

演示如何在 WebForm 中提供 web api 服务

Global.asax.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using System.Web.Http; namespace WebApiWebFormHost { public class

如何在QML中设计一个expandable ListView

在前面的文章"如何在QML中使用ListView并导航到其它页面中"中,我们已经介绍了各种在ListView中导航到其它页面的方法.在这篇文章中,我来介绍如何建立一个expandable的ListView.通过这样的方法,ListView可以不用导航到其它的页面中,但是它可以通过状态的控制占据整个页面,而得到显示. 首先我们可以使用Ubuntu SDK来创建一个最简单的"QML App with Simple UI (qmlproject)"项目.我们的Main.q