In the previous article, "How to take photos using the camera API in QML," we described how to use the camera API to take pictures. Today we'll show you how to use the camera API for video recording in this article.
First of all, as before, I directly put my own code to paste it out:
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 Manifest ApplicationName: "Videoapp.liu-xiao-guo"/* The 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 (HEIGHT:UNITS.GU) 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:CameraI Mageprocessing.whitebalanceflash Exposure {exposurecompensation: -1.0 Exposuremo De:Camera.ExposurePortrait} flash.mode:Camera.FlashRedEyeReduction Videorecorder { Onrecorderstatechanged: {console.log ("onrecorderstatechanged:" + Videorecorder.recorderst ATE); 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:vi Deo anchors.fill:parent Source:camera focus:visible//To Rec Eive Focus and capture key events when visible orientation: -90} swipear EA {anchors.fill:parent onswipe: {console.log ("swipe Happe ned!: "+ direction) switch (direction) {case" left ": Page.state = "image"; Break}}}} Item {Id:vie W width:page.width height:page.height Video {id:myvide o 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.WrapAtWordBo Undaryoranywhere} swipearea {anchors.fill:parent on Swipe: {console.log ("Swipe happened!:" + direction) switch (direction) { Case "Right": Page.state = ""; Break}}}} states: [Sta TE {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:in Putcontainer; 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:p Arent.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: "P Lay Video "onclicked: {console.log (" filepath: "+ myvideo.source); Console.log ("actual:" + camera.videoRecorder.actualLocation); Myvideo.play (); } } } }}
Here, QML camera is used in the following ways:
Camera {Id:camera ImageProcessing.whiteBalanceMode:CameraImageProcessing.WhiteBalanceFlas H Exposure {exposurecompensation: -1.0 exposureMode:Camera.ExposurePortrait } flash.mode:Camera.FlashRedEyeReduction Videorecorder {Onrecorderstatechan Ged: {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.outputloca Tion:getpriatedirectory () CaptureMode:Camera.CaptureVideo component.oncompleted: {resolution = Camera.viewfinder. Resolution Console.log ("Resolution:" + Resolution.width + "+ resolution.height); Console.log ("deviceId:" + Camera.deviceid)}
We define the Capuremode as Camera.capturevideo. We also specify the path of the video file:
VideoRecorder.outputLocation:getPriateDirectory ()
When we use Camerarecorder to do the following video recording:
Camera.videoRecorder.record ();
We can do this by:
Videorecorder { onrecorderstatechanged: { console.log ("onrecorderstatechanged:" + Videorecorder.recorderstate); if (videorecorder.recorderstate = = = camerarecorder.stoppedstate) { console.log ("actuallocation:" + videorecorder.actuallocation); Myvideo.source = videorecorder.actuallocation;}} }
To get the actual path of the file being recorded.
We can do this by:
Video { id:myvideo anchors.fill:parent autoplay:true focus:true component.oncompleted: { Myvideo.play (); } }
To play the files that have been recorded.
Run for our application:
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/videoapp.git
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use the camera API to record in Ubuntu mobile platforms