In a previous article, "How to use a front camera in an Ubuntu phone", we can use the corresponding C + + code to control the camera at the front and back, but how can we store the captured photos in the corresponding file? We can use the QT 5.4 version of item to provide a new feature "Grabtoimage". This way we can easily save the photos we get to our respective directories.
In the Ubuntu mobile platform, for security reasons, our app can only access the directory that we have access to. In this routine, we used some environment variables to get the location of the directory we needed to store:
Our entire code is as follows:
Import QtQuick 2.0import ubuntu.components 1.1import qtmultimedia 5.0import readenv 1.0/*! \brief MainView with a Label and Button Elements.*/mainview {id:root//ObjectName for functional testing purposes (AUTOPILOT-QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Camerashot.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) Page {id:mainpage title:i1 8n.tr ("Camerashot") Property string path: "" "Readenv {id:env} Item { Id:page1 anchors.fill:parent Camera {id:camera imageprocessing.whi TebalancEMode:CameraImageProcessing.WhiteBalanceFlash Exposure {exposurecompensation:-1.0 ExposureMode:Camera.ExposurePortrait} flash.mode:Camera.FlashRedEyeReduct Ion Imagecapture {onimagecaptured: {Photopreview.source = Previ EW/Show The preview in an Image}} videooutput { Id:video Source:camera anchors.fill:parent focus:visible//To Rece Ive focus and capture key events when visible orientation: -90 Mousearea { Anchors.fill:parent onclicked: {var app_id = env.getenv ("app_id"); var app_pkgname = App_id.split ('_') [0] Mainpage.path = env.getenv ("xdg_data_home") + "/" + App_pkgname + "/pic.png"; Console.log ("Share path:" + Mainpage.path); var a = Video.grabtoimage (function (Result) {result.savetofile (Mainpage.path); }, Qt.size (Photopreview.width, photopreview.height)); Console.log ("return Result:" + a); }} (Image {Id:photopreview}} but ton {Id:button Anchors.bottom:parent.bottom anchors.right:parent.right Text: "Show the Taken Picture" onclicked: {pic.visible = true; Page1.visible = false; Console.log ("Image path:" + Mainpage.path); Image.source = ""; Image.source = "file:///" + mainpage.path; }}} ITEM {id:pic anchors.fill:parent visible:false Image {id:i Mage Cache:false Anchors.fill:parent} Label {text: Mainpage.path} Image {Anchors.bottom:parent.bottom Anchors.left:pa Rent.left Anchors.leftMargin:units.gu (1) Anchors.bottomMargin:units.gu (1) W Idth:units.gu (3) Height:units.gu (3) Source: "Images/icon-left-arrow.png" Mo Usearea {anchors.fill:parent onclicked: {pic.visible = FAL Se page1.visible = true; } } } } }}
The whole design is very simple. In the main interface we can take pictures of us by touching the screen anywhere in the camera's image. We can use the show the Taken Picture button to go to the next page to display the stored pictures. For ease of design, we chose a fixed file name. Of course, developers can choose their own desired file name.
The source code for the entire project is: Git clone https://gitcafe.com/ubuntu/camerashot.git
How to use the camera API in the Ubuntu mobile platform to store photos