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

在前面的文章“如何在QML中使用camera API来拍照”中,我们介绍了如何使用Camera API来进行拍照。今天我们在这篇文章中来介绍如何使用Camera API来进行录像。

首先,还是和以前一样,我直接把自己的代码贴出来:

main.qml

import QtQuick 2.0import Ubuntu.Components 1.1import 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 manifestapplicationName: "videoapp.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(60)height: units.gu(85)property var resolution// This function is used to get the writable private directory of this appfunction getPriateDirectory() {var sharepath = "/home/phablet/.local/share/";var path = sharepath + applicationName;console.log("path: " + path);return path;}Page {id: pagetitle: i18n.tr("videoapp")Camera {id: cameraimageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlashexposure {exposureCompensation: -1.0exposureMode: Camera.ExposurePortrait}flash.mode: Camera.FlashRedEyeReductionvideoRecorder {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: 128000videoRecorder.mediaContainer: "mp4"videoRecorder.outputLocation: getPriateDirectory()captureMode: Camera.CaptureVideoComponent.onCompleted: {resolution = camera.viewfinder.resolution;console.log("resolution: " + resolution.width + " " + resolution.height);console.log("deviceId: " + camera.deviceId)}}Row {id: containerItem {width: page.widthheight: page.heightVideoOutput {id: videoanchors.fill: parentsource: camerafocus : visible // to receive focus and capture key events when visibleorientation: -90}SwipeArea {anchors.fill: parentonSwipe: {console.log("swipe happened!: " + direction)switch (direction) {case "left":page.state = "image";break}}}}Item {id: viewwidth: page.widthheight: page.heightVideo {id: myvideoanchors.fill: parentautoPlay: truefocus: trueComponent.onCompleted: {myvideo.play();}}Text {text: myvideo.sourcecolor:"red"font.pixelSize: units.gu(2.5)width: page.widthwrapMode: Text.WrapAtWordBoundaryOrAnywhere}SwipeArea {anchors.fill: parentonSwipe: {console.log("swipe happened!: " + direction)switch (direction) {case "right":page.state = "";break}}}}}states: [State {name: "playvideo"PropertyChanges {target: containerx:-page.width}PropertyChanges {target: captureopacity:0}PropertyChanges {target: stopopacity:0}}]transitions: [Transition {NumberAnimation { target: container; property: "x"; duration: 500easing.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.bottomanchors.bottomMargin: units.gu(1)anchors.horizontalCenter: parent.horizontalCenterspacing: units.gu(1)Button {id: capturetext: "Record"onClicked: {console.log("capture path: " + getPriateDirectory());camera.videoRecorder.record();}}Button {id: stoptext: "Stop"onClicked: {console.log("stop is clicked!");camera.videoRecorder.stop();page.state = "playvideo"}}Button {id: playtext: "Play video"onClicked: {console.log("filepath: " + myvideo.source);console.log( "actual: " + camera.videoRecorder.actualLocation);myvideo.play();}}}}}

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

Camera {id: cameraimageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlashexposure {exposureCompensation: -1.0exposureMode: Camera.ExposurePortrait}flash.mode: Camera.FlashRedEyeReductionvideoRecorder {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: 128000videoRecorder.mediaContainer: "mp4"videoRecorder.outputLocation: getPriateDirectory()captureMode: Camera.CaptureVideoComponent.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();我们可以通过:

让我们从自身的禁锢中放心地飞出去,重新审视自己,

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

相关文章:

你感兴趣的文章:

标签云: