在这篇文章中,我们将具体介绍怎样使用Cordova Camera HTML5 应用。很多其它关于Cordova的开发指南,开发人员能够參考文章“the Cordova Guide”。通过这个例程。我们能够学习在Ubuntu平台上怎样利用Cordova API来完毕一个我们所须要的照相机功能。关于怎样创建一个Cordova架构的简单的应用,开发人员能够參阅文章“怎样在Ubuntu手机平台中开发Cordova
HTML5应用”。在那篇文章中,它介绍了怎样设置自己的环境。建议开发人员先阅读该文章。
在我们做练习之前,我们能够在我们想要创建应用文件夹的以下,打入例如以下的命令:
$ bzr branch lp:ubuntu-sdk-tutorials
在上面的代码中。有一个已经完毕好的Camera代码。
1)创建一个最主要的Cordova框架应用
我们能够參阅文章“怎样在Ubuntu手机平台中开发Cordova HTML5应用”来创建一个叫做Camera的简单应用:
$ cordova create cordovacam cordovacam.mycompany "CordovaCam"
$ cd cordovacam
2)定义应用自己的图标
$ cp ../ubuntu-sdk-tutorials/html5/html5-tutorial-cordova-camera/www/icon.png ./www/img/logo.png
然后,我们须要改动我们的config.xml文件。加入例如以下的项到它里面去:
<icon src="www/img/logo.png" />
当然。我们也有必要改动作者自己的邮件地址。改动完后,config.xml的显演示样例如以下:
<? 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>
注意: 这一步不可缺少。否则我们的应用通过不了package的验证。
3)加入Ubuntu平台支持代码到项目中
$ cordova platform add ubuntu
- platforms/ubuntu/
{
"policy_groups": ["networking", "camera”, "audio"],
"policy_version":1
}
4)加入Camera API支持
$ cordova plugin add org.apache.cordova.camera
来加入Cordova runtime到你的项目中去。
5)执行我们的应用
$ cordova run --device --debug
就像在我们之前文章“怎样在Ubuntu手机平台中开发Cordova HTML5应用”中介绍的那样。这个命令是在利用默认的平台进行执行的(眼下是14.10)。
在其他的平台上,你须要在它的后面加上平台的參数:
$ cordova run --device -- --framework ubuntu-sdk-15.04
假设没有不论什么问题的话,你将看到例如以下的画面:
6)定义HTML 5用户界面
<!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" />
这里有一个app.css文件,一个app.js文件。我们须要把原来的index.css及index.js文件换成它们。
app.css
#loading {
position: absolute;
left:45%;
}
#loaded {
display: none;
}
app.js
/**
* Wait before the DOM has been loaded before initializing the Ubuntu UI layer
*/ window.onload = function () {
var UI = new UbuntuUI(); // This must be called after window is loaded
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";
image.style.display = "block";
} // log failure message
function onFail(message) {
console.log("Picture failure: " + message);
}
var UI = new UbuntuUI(); // This must be called after window is loaded
必须是在window.onload里做。不能够放在它的前面去做。
否则我们的UI就会有问题。
执行的显演示样例如以下:
<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>
这里採用了Ubuntu平台上的HTML5 UI Toolkit。而在我们的index.html head部分:
<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>
它依赖于手机系统提供的ambiance。
这有可能造成我们的UI和眼下的最新的ambiance不可以全然匹配。为了解决问题,我们參考连接https://code.launchpad.net/~dbarth/ubuntu-html5-theme/cmdline-tool/+merge/253498
$ ubuntu-html5-theme install 14.10
然后
$ ubuntu-html5-theme convert
执行完后,我们再又一次看一下我们的index.html文件:
<!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>
而且在当前的cordovacam/www文件夹下多了一个叫做“ambiance”的文件夹。这样应用再也不依赖于系统所提供的ambiance了。
$ cordova run --device --debug