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

原创 2015年07月31日 15:54:58

在前面的文章“如何在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 elements.
*/

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

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "videoapp.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("videoapp")

        Camera {
            id: camera

            imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash

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

            flash.mode: Camera.FlashRedEyeReduction

            videoRecorder {
                onRecorderStateChanged: {
                    console.log("onRecorderStateChanged: " + videoRecorder.recorderState);
                    if (videoRecorder.recorderState === CameraRecorder.StoppedState) {
                        console.log("actualLocation: " + videoRecorder.actualLocation);
                        myvideo.source =  videoRecorder.actualLocation;
                    }
                }
            }

            videoRecorder.audioEncodingMode: videoRecorder.ConstantBitrateEncoding;
            videoRecorder.audioBitRate: 128000
            videoRecorder.mediaContainer: "mp4"
            videoRecorder.outputLocation: getPriateDirectory()

            captureMode: Camera.CaptureVideo

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

        Row {
            id: container

            Item {
                width: page.width
                height: page.height

                VideoOutput {
                    id: video
                    anchors.fill: parent
                    source: camera
                    focus : visible // to receive focus and capture key events when visible
                    orientation: -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

                Video {
                    id: myvideo
                    anchors.fill: parent
                    autoPlay: true
                    focus: true

                    Component.onCompleted: {
                        myvideo.play();
                    }
                }

                Text {
                    text: myvideo.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: "playvideo"
                PropertyChanges {
                    target: container
                    x:-page.width
                }
                PropertyChanges {
                    target: capture
                    opacity:0
                }
                PropertyChanges {
                    target: stop
                    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}
                NumberAnimation { target: stop; property: "opacity"; duration: 200}
            }
        ]

        Row {
            anchors.bottom: parent.bottom
            anchors.bottomMargin: units.gu(1)
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: units.gu(1)

            Button {
                id: capture
                text: "Record"

                onClicked: {
                    console.log("capture path: " + getPriateDirectory());
                    camera.videoRecorder.record();
                }
            }

            Button {
                id: stop
                text: "Stop"

                onClicked: {
                    console.log("stop is clicked!");
                    camera.videoRecorder.stop();
                    page.state = "playvideo"
                }
            }

            Button {
                id: play
                text: "Play video"

                onClicked: {
                    console.log("filepath: " + myvideo.source);
                    console.log( "actual: " +  camera.videoRecorder.actualLocation);
                    myvideo.play();
                }
            }
        }

    }
}


在这里,QML Camera是被如下的方式使用的:

        Camera {
            id: camera

            imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash

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

            flash.mode: Camera.FlashRedEyeReduction

            videoRecorder {
                onRecorderStateChanged: {
                    console.log("onRecorderStateChanged: " + videoRecorder.recorderState);
                    if (videoRecorder.recorderState === CameraRecorder.StoppedState) {
                        console.log("actualLocation: " + videoRecorder.actualLocation);
                        myvideo.source =  videoRecorder.actualLocation;
                    }
                }
            }

            videoRecorder.audioEncodingMode: videoRecorder.ConstantBitrateEncoding;
            videoRecorder.audioBitRate: 128000
            videoRecorder.mediaContainer: "mp4"
            videoRecorder.outputLocation: getPriateDirectory()

            captureMode: Camera.CaptureVideo

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

我们定义了capureMode为Camera.CaptureVideo。同时我们也指定了录像文件的路径:

videoRecorder.outputLocation: getPriateDirectory()

当我们使用CameraRecorder来进行如下的方式录像时:

 camera.videoRecorder.record();

我们可以通过:

            videoRecorder {
                onRecorderStateChanged: {
                    console.log("onRecorderStateChanged: " + videoRecorder.recorderState);
                    if (videoRecorder.recorderState === CameraRecorder.StoppedState) {
                        console.log("actualLocation: " + videoRecorder.actualLocation);
                        myvideo.source =  videoRecorder.actualLocation;
                    }
                }
            }

来获取所录像的文件的实际路径。

我们可以通过:

               Video {
                    id: myvideo
                    anchors.fill: parent
                    autoPlay: true
                    focus: true

                    Component.onCompleted: {
                        myvideo.play();
                    }
                }

来播放已经录下的文件。

运行为我们的应用:

 

整个项目的源码在:https://github.com/liu-xiao-guo/videoapp



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

ubuntu 运行 usb camera

用软件Cheese 1) 插上 usb camera 2)  软件中心 找 cheese 运行 ref: http://www.ehow.com/how_5610857_mount-usb-camer...
  • span76
  • span76
  • 2015年04月27日 15:24
  • 2722

Ubuntu14.04下安装Gstreamer以测试UVC H264 Camera

版权声明:本文为博主原创文章,未经博主允许不得转载 Ubuntu14.04下安装Gstreamer以测试UVC H264 Camera 最近调试 UVC H264 Camera,需要在Ubu...
  • nan_nan_nan_nan_nan
  • nan_nan_nan_nan_nan
  • 2017年09月02日 17:16
  • 941

ubuntu--camera

usb_camerasudo apt-get install cheeseros: sudo apt-get install ros-indigo-usb-camroslaunch usb_cam-...
  • heroacool
  • heroacool
  • 2016年07月13日 11:12
  • 236

ubuntu下安装摄像头应用程序xawtv

准备工作:安装xawtv应用程序 方法一、 源码xawtv-3.95.tar.gz: http://www.kraxel.org/releases/xawtv/     下载后上传到Ubuntu上解...
  • qq_21792169
  • qq_21792169
  • 2016年03月08日 18:00
  • 4739

Ubuntu下用opencv调用摄像头过程分享

环境:CB A10  +  lubuntu1.05 + opencv2.6.4.1 准备:2G以上空闲空间. 直接在CB上编译,交叉编译可参见:移植Qt和OpenCV做图像处理方面的开发中mat...
  • ganlumm
  • ganlumm
  • 2014年10月05日 20:03
  • 3656

ubuntu下使用UVC摄像头

转载于:http://blog.csdn.net/xingyu19871124/article/details/5764954 导师让学习opencv编程,想用Qt但是发现windo...
  • w_ww_w
  • w_ww_w
  • 2014年01月02日 17:05
  • 2787

USB Camera V4L2 视频录制——ubuntu(1)

  • 2016年08月04日 16:52
  • 1.61MB
  • 下载

Ubuntu下视频的处理

一 视频下载 很多实验数据会从YouTube上下载,我用的工具是youtube-dl 1 安装: sudo apt-get install youtube-dl 即可 2 配置使用:如果你已经翻墙,直...
  • lanbing510
  • lanbing510
  • 2014年12月28日 17:51
  • 2676

VMware10+ubuntu14.04打开webcam摄像头失败问题

win7+VMware workstation10+ubuntu14.04 cheese无法打开webcam摄像头
  • haoruojing
  • haoruojing
  • 2017年03月09日 19:13
  • 2013

TensorFlow安装教程与Android Camera Demo示例(Ubuntu Trusty 14.04 LTS)

第一部分Tensorflow重要学习资料 英文官网 中文社区 极客学院 Tensorflow Github0 资料介绍 Tensorflow Mobile 官网:https://www.te...
  • qingqingdeaini
  • qingqingdeaini
  • 2016年07月24日 03:16
  • 3276
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在Ubuntu手机平台中使用Camera API来录像
举报原因:
原因补充:

(最多只允许输入30个字)