“
|
camera对象提供对设备默认摄像头应用程序的访问。
|
方法:
参数:
-
camerasuccess
-
cameraerror
-
cameraoptions
选择使用摄像头拍照,或从设备相册中获取一张照片。图片以base64编码的字符串或图片uri形式返回。
简单的范例:
-
navigator.camera.getpicture( camerasuccess, cameraerror, [ cameraoptions ] );
复制代码
说明:
camera.getpicture函数打开设备的默认摄像头应用程序,使用户可以拍照(如果
camera.sourcetype 设置为 camera.picturesourcetype.camera,这也是默认值)。一旦拍照结束,摄像头应用程序会关闭并恢复用户应用程序。
如果camera.sourcetype = camera.picturesourcetype.photolibrary或camera.picturesourcetype.savedphotoalbum,系统弹出照片选择对话框,用户可以从相集中选择照片。
返回值会按照用户通过cameraoptions参数所设定的下列格式之一发送给camerasuccess回调函数:
-
一个字符串,包含base64编码的照片图像(默认情况)。
-
一个字符串,表示在本地存储的图像文件位置。
你可以对编码的图片或uri做任何处理,例如:
-
通过标签渲染图片(参看后续范例)
-
存储为本地数据(localstorage,lawnchair*等)
-
将数据发送到远程服务器
备注:较新的设备上
此文来自: 马开东博客 转载请注明出处 网址: http://www.makaidong.com
使用摄像头拍摄的照片的质量是相当不错的,使用base64对这些照片进行编码已导致其中的一些设备出现内存问题 (如iphone4、blackberry torch 9800)。因此,强烈建议将“camera.destinationtype”设为file_uri。
支持的平台:
-
android
-
blackberry webworks (os 5.0或更高版本)
-
ios
简单的范例:
拍照并获取base64编码的图像:
-
navigator.camera.getpicture(onsuccess, onfail, { quality: 50 });
-
-
function onsuccess(imagedata) {
-
var image = document.getelementbyid('myimage');
-
image.src = "data:image/jpeg;base64," + imagedata;
-
}
-
-
function onfail(message) {
-
alert('failed because: ' + message);
-
}
复制代码
拍照并获取图像文件路径:
-
navigator.camera.getpicture(onsuccess, onfail, { quality: 50,
-
destinationtype: camera.destinationtype.file_uri });
-
-
function onsuccess(imageuri) {
-
var image = document.getelementbyid('myimage');
-
image.src = imageuri;
-
}
-
-
function onfail(message) {
-
alert('failed because: ' + message);
-
}
复制代码
完整的范例:
-
<!doctype html>
-
<html>
-
<head>
-
<title>capture photo</title>
-
-
<script type="text/javascript" charset="utf-8" src="phonegap.js "></script>
-
<script type="text/javascript" charset="utf-8">
-
-
var picturesource; //图片来源
-
var destinationtype; //设置返回值的格式
-
-
// 等待phonegap连接设备
-
document.addeventlistener("deviceready",ondeviceready,false);
-
-
// phonegap准备就绪,可以使用!
-
function ondeviceready() {
-
picturesource=navigator.camera.picturesourcetype;
-
destinationtype=navigator.camera.destinationtype;
-
}
-
-
// 当成功获得一张照片的base64编码数据后被调用
-
function onphotodatasuccess(imagedata) {
-
-
// 取消注释以查看base64编码的图像数据
-
// console.log(imagedata);
-
// 获取图像句柄
-
var smallimage = document.getelementbyid('smallimage');
-
-
// 取消隐藏的图像元素
-
smallimage.style.display = 'block';
-
-
// 显示拍摄的照片
-
// 使用内嵌css规则来缩放图片
-
smallimage.src = "data:image/jpeg;base64," + imagedata;
-
}
-
-
// 当成功得到一张照片的uri后被调用
-
function onphotourisuccess(imageuri) {
-
-
// 取消注释以查看图片文件的uri
-
// console.log(imageuri);
-
// 获取图片句柄
-
var largeimage = document.getelementbyid('largeimage');
-
-
// 取消隐藏的图像元素
-
largeimage.style.display = 'block';
-
-
// 显示拍摄的照片
-
// 使用内嵌css规则来缩放图片
-
largeimage.src = imageuri;
-
}
-
-
// “capture photo”按钮点击事件触发函数
-
function capturephoto() {
-
-
// 使用设备上的摄像头拍照,并获得base64编码字符串格式的图像
-
navigator.camera.getpicture(onphotodatasuccess, onfail, { quality: 50 });
-
}
-
-
// “capture editable photo”按钮点击事件触发函数
-
function capturephotoedit() {
-
-
// 使用设备上的摄像头拍照,并获得base64编码字符串格式的可编辑图像
-
navigator.camera.getpicture(onphotodatasuccess, onfail, { quality: 20, allowedit: true });
-
}
-
-
//“from photo library”/“from photo album”按钮点击事件触发函数
-
function getphoto(source) {
-
-
// 从设定的来源处获取图像文件uri
-
navigator.camera.getpicture(onphotourisuccess, onfail, { quality: 50,
-
destinationtype: destinationtype.file_uri,sourcetype: source });
-
}
-
-
// 当有错误发生时触发此函数
-
function onfail(mesage) {
-
alert('failed because: ' + message);
-
}
-
-
</script>
-
-
</head>
-
<body>
-
<button onclick="capturephoto();">capture photo</button> <br>
-
<button onclick="capturephotoedit();">capture editable photo</button> <br>
-
<button onclick="getphoto(picturesource.photolibrary);">from photo library</button><br>
-
<button onclick="getphoto(picturesource.savedphotoalbum);">from photo album</button><br>
-
<img style="display:none;width:60px;height:60px;" id="smallimage" src="" />
-
<img style="display:none;" id="largeimage" src="" />
-
</body>
-
</html>
复制代码
提供图像数据的onsuccess回调函数。
-
function(imagedata) {
-
// 对图像进行处理
-
}
复制代码
参数:
-
imagedata:根据cameraoptions的设定值,为base64编码的图像数据或图像文件的uri。(字符串类型)
范例:
-
// 显示图片
-
function cameracallback(imagedata) {
-
var image = document.getelementbyid('myimage');
-
image.src = "data:image/jpeg;base64," + imagedata;
-
}
复制代码
提供错误信息的onerror回调函数。
-
function(message) {
-
// 显示有用信息
-
}
复制代码
参数:
定制摄像头设置的可选参数。
-
{ quality : 75,
-
destinationtype : camera.destinationtype.data_url,
-
sourcetype : camera.picturesourcetype.camera,
-
allowedit : true,
-
encodingtype : camera.encodingtype.jpeg,
-
targetwidth : 100,
-
targetheight : 100};
复制代码
选项:
-
quality:存储图像的质量,范围是[0,100]。(数字类型)
-
destinationtype:选择返回数据的格式。通过navigator.camera.destinationtype进行定义。(数字类型)
-
camera.destinationtype = {
-
data_url : 0, //返回base64编码字符串的图像数据
-
file_uri : 1 //返回图像文件的uri
-
}
复制代码
-
sourcetype:设定图片来源。通过nagivator.camera.picturesourcetype进行定义。(数字类型)
-
camera.picturesourcetype = {
-
photolibrary : 0,
-
camera : 1,
-
savedphotoalbum : 2
-
}
复制代码
-
allowedit:在选择图片进行操作之前允许对其进行简单编辑。(布尔类型)
-
encodingtype:选择返回图像文件的编码方式,通过navigator.camera.encodingtype进行定义。(数字类型)
-
camera.encodingtype = {
-
jpeg : 0, // 返回jpeg格式图片
-
png : 1 // 返回png格式图片
-
};
复制代码
-
targetwidth:以像素为单位的图像缩放宽度,必须和targetheight同时使用。相应的宽高比保持不变。(数字类型)
-
targetheight:以像素为单位的图像缩放高度,必须和targetwidth同时使用。相应的宽高比保持不变。(数字类型)
android的特异情况:
-
忽略allowedit参数。
-
camera.picturesourcetype.photolibrary 或 camera.picturesourcetype.savedphotoalbum 都会显示同一个相集。
-
camera.encodingtype不被支持。
blackberry的特异情况:
-
忽略quality参数。
-
忽略sourcetype参数。
-
忽略allowedit参数。
-
当拍照结束后,应用程序必须有按键注入权限才能关闭本地camera应用程序。
-
使用大图像尺寸,可能会导致新近带有高分辨率摄像头的型号设备无法对图像进行编码(如:torch 9800)。
palm的特异情况:
-
忽略quality参数。
-
忽略sourcetype参数。
-
忽略allowedit参数。
iphone的特异情况:
-
为了避免部分设备上出现内存错误,quality的设定值要低于50。
-
当使用destinationtype.file_uri时,使用摄像头拍摄的和编辑过的照片会存储到应用程序的documents/tmp目录。
-
<span javascript"="" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; background: transparent; max-width: 650px; height: auto;">var url = window.location.href;document.write("此文链接:"+url+"
");document.write("转载请注明出处:"+document.title+"");
|