标签:
在这篇文章中,我们将详细介绍如何使用Cordova Camera HTML5 应用。更多关于Cordova的开发指南,开发者可以参考文章“the Cordova Guide”。通过这个例程,我们可以学习在Ubuntu平台上如何利用Cordova API来完成一个我们所需要的照相机功能。关于如何创建一个Cordova架构的简单的应用,开发者可以参阅文章“如何在Ubuntu手机平台中开发Cordova HTML5应用”。在那篇文章中,它介绍了如何设置自己的环境。建议开发者先阅读该文章。
在我们做练习之前,我们可以在我们想要创建应用目录的下面,打入如下的命令:
$ bzr branch lp:ubuntu-sdk-tutorials
$ cordova create cordovacam cordovacam.mycompany "CordovaCam" $ cd cordovacam
$ cp ../ubuntu-sdk-tutorials/html5/html5-tutorial-cordova-camera/www/icon.png ./www/img/logo.png
<icon src="www/img/logo.png" />
<?xml version='1.0' encoding='utf-8'?> <widget id="cordovacam.mycompany" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name>CordovaCam</name> <description> A sample Apache Cordova application that responds to the deviceready event. </description> <author email="myname@mycompany.com" href="http://cordova.io"> Apache Cordova Team </author> <content src="index.html" /> <plugin name="cordova-plugin-whitelist" version="1" /> <access origin="*" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <platform name="android"> <allow-intent href="market:*" /> </platform> <platform name="ios"> <allow-intent href="itms:*" /> <allow-intent href="itms-apps:*" /> </platform> <icon src="www/img/logo.png" /> </widget>
$ cordova platform add ubuntu
{ "policy_groups": ["networking", "camera”, "audio"], "policy_version":1 }
$ cordova plugin add org.apache.cordova.camera
$ cordova run --device --debug
$ cordova build --device -- --framework ubuntu-sdk-15.04 --verbose
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>An Ubuntu HTML5 application</title> <meta name="description" content="An Ubuntu HTML5 application"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> <!-- Ubuntu UI Style imports - Ambiance theme --> <link href="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/css/appTemplate.css" rel="stylesheet" type="text/css" /> <!-- Ubuntu UI javascript imports - Ambiance theme --> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/fast-buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/core.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/dialogs.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/page.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/pagestacks.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/tabs.js"></script> <!-- Cordova platform API access - Uncomment this to have access to the Javascript APIs --> <script src="cordova.js"></script> <!-- Application script and css --> <script src="js/app.js"></script> <link href="css/app.css" rel="stylesheet" type="text/css" /> </head> <body> <div data-role="mainview"> <header data-role="header"> <ul data-role="tabs"> <li data-role="tabitem" data-page="camera">Camera</li> </ul> </header> <div data-role="content"> <div data-role="tab" id="camera"> <div id="loading"> <header>Loading...</header> <progress class="bigger">Loading...</progress> </div> <div id="loaded"> <button data-role="button" class="ubuntu" id="click">Take Picture</button> <img id="image" src="" /> </div> </div> <!-- tab: camera --> </div> <!-- content --> </div> <!-- mainview --> </body> </html>
<!-- Cordova platform API access - Uncomment this to have access to the Javascript APIs --> <script src="cordova.js"></script> <!-- Application script and css --> <script src="js/app.js"></script> <link href="css/app.css" rel="stylesheet" type="text/css" />
#loading { position: absolute; left:45%; } #loaded { display: none; }
/** * Wait before the DOM has been loaded before initializing the Ubuntu UI layer */ var UI = new UbuntuUI(); window.onload = function () { UI.init(); document.addEventListener("deviceready", function() { if (console && console.log) console.log('Platform layer API ready'); //hide the loading div and display the loaded div document.getElementById("loading").style.display = "none"; document.getElementById("loaded").style.display = "block"; // event listener to take picture UI.button("click").click( function() { navigator.camera.getPicture(onSuccess, onFail, { quality: 100, targetWidth: 400, targetHeight: 400, destinationType: Camera.DestinationType.DATA_URL, correctOrientation: true }); console.log("Take Picture button clicked"); }); // "click" button event handler }, false); // deviceready event handler }; // window.onload event handler // show new picture in html and log function onSuccess(imageData) { var image = document.getElementById('image'); image.src = "data:image/jpeg;base64," + imageData; image.style.margin = "10px";<img src="http://img.blog.csdn.net/20150727112433223?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="300" alt="" /> image.style.display = "block"; } // log failure message function onFail(message) { console.log("Picture failure: " + message); }
<body> <div data-role="mainview"> <header data-role="header"> <ul data-role="tabs"> <li data-role="tabitem" data-page="camera">Camera</li> </ul> </header> <div data-role="content"> <div data-role="tab" id="camera"> <div id="loading"> <header>Loading...</header> <progress class="bigger">Loading...</progress> </div> <div id="loaded"> <button data-role="button" class="ubuntu" id="click">Take Picture</button> <img id="image" src="" /> </div> </div> <!-- tab: camera --> </div> <!-- content --> </div> <!-- mainview --> </body>
<script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/fast-buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/core.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/buttons.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/dialogs.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/page.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/pagestacks.js"></script> <script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/tabs.js"></script>
$ ubuntu-html5-theme install 14.10
$ ubuntu-html5-theme convert
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>An Ubuntu HTML5 application</title> <meta name="description" content="An Ubuntu HTML5 application"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> <!-- Ubuntu UI Style imports - Ambiance theme --> <link href="ambiance/css/appTemplate.css" rel="stylesheet" type="text/css" /> <!-- Ubuntu UI javascript imports - Ambiance theme --> <script src="ambiance/js/fast-buttons.js"></script> <script src="ambiance/js/core.js"></script> <script src="ambiance/js/buttons.js"></script> <script src="ambiance/js/dialogs.js"></script> <script src="ambiance/js/page.js"></script> <script src="ambiance/js/pagestacks.js"></script> <script src="ambiance/js/tabs.js"></script> <!-- Cordova platform API access - Uncomment this to have access to the Javascript APIs --> <script src="cordova.js"></script> <!-- Application script and css --> <script src="js/app.js"></script> <link href="app.css" rel="stylesheet" type="text/css" /> </head> <body> <div data-role="mainview"> <header data-role="header"> <ul data-role="tabs"> <li data-role="tabitem" data-page="camera">Camera</li> </ul> </header> <div data-role="content"> <div data-role="tab" id="camera"> <div id="loading"> <header>Loading...</header> <progress class="bigger">Loading...</progress> </div> <div id="loaded"> <button data-role="button" class="ubuntu" id="click">Take Picture</button> <img id="image" src="" /> </div> </div> <!-- tab: camera --> </div> <!-- content --> </div> <!-- mainview --> </body> </html>
<!-- Ubuntu UI javascript imports - Ambiance theme --> <script src="ambiance/js/fast-buttons.js"></script> <script src="ambiance/js/core.js"></script> <script src="ambiance/js/buttons.js"></script> <script src="ambiance/js/dialogs.js"></script> <script src="ambiance/js/page.js"></script> <script src="ambiance/js/pagestacks.js"></script> <script src="ambiance/js/tabs.js"></script>
$ cordova run --device --debug
版权声明:本文为博主原创文章,未经博主允许不得转载。
在Ubuntu平台上创建Cordova Camera HTML5应用
标签:
原文地址:http://blog.csdn.net/ubuntutouch/article/details/47080949