phonegap的照相机API

时间:2024-01-18 09:21:02
1. Camera Api简单介绍
2. 拍照
3. 预览照片 一、 Camera Api简单介绍 Camera选择使用摄像头拍照,或从设备相册中获取一张照片。图片以base64编码的
字符串或图片URI形式返回。
方法:
1. camera.getPicture 拍照获取相册图片
navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );
cameraSuccess:提供图像数据的onSuccess回调函数。
cameraError:提供错误信息的onError回调函数。
cameraOptions:定制摄像头设置的可选参数
2. camera.cleanup 清除拍照后设备的缓存图片
navigator.camera.cleanup( cameraSuccess, cameraError );
3.cameraOptions参数:
定制摄像头设置的可选参数。
quality : 存储图像的质量,范围是[0,100]。
destinationType :选择返回数据的格式。
sourceType :设定图片来源。data:image/jpeg;base64,
allowEdit :在选择图片进行操作之前允许对其进行简单编辑。(好像只有ios支持)
encodingType :选择返回图像文件的编码方encodingType: Camera.EncodingType.JPEG
targetWidth :以像素为单位的图像缩放宽度指定图片展示的时候的宽度
targetHeight :以像素为单位的图像缩放高度指定图片展示的时候的高度
saveToPhotoAlbum:拍完照片后是否将图像保存在设备上的相册
mediaType 设置选择媒体的类型
cameraDirection 选择前置摄像头还是后置摄像头
注意:在Android中。
忽略allowEdit参数。 Camera.PictureSourceType.PHOTOLIBRARY或
Camera.PictureSourceType.SAVEDPHOTOALBUM都会显示同一个相集。 . quality: Quality of the saved image, expressed as a range of 0-100, where 100
is typically full resolution with no loss from file compression. (Number) (Note that
information about the camera's resolution is unavailable.)
. destinationType: Choose the format of the return value. Defined
in navigator.camera.DestinationType (Number) Camera.DestinationType={
DATA_URL :0, // Return image as base64-encoded string
FILE_URI :1, // Return image file URI
NATIVE_URI :2 // Return image native URI (e.g. assets-library://
on iOS or content:// on Android)
}; . sourceType: Set the source of the picture. Defined
in navigator.camera.PictureSourceType (Number) Camera.PictureSourceType={
PHOTOLIBRARY :0,//图库中获取
CAMERA :1,//设备照相机中获取
SAVEDPHOTOALBUM :2//从相册中获取
}; . allowEdit: Allow simple editing of image before selection. (Boolean)
. encodingType: Choose the returned image file's encoding. Defined
in navigator.camera.EncodingType (Number) Camera.EncodingType={
JPEG :0, // Return JPEG encoded image
PNG :1 // Return PNG encoded image
}; . targetWidth: Width in pixels to scale image. Must be used with targetHeight. Aspect ratio remains constant. (Number)
. targetHeight: Height in pixels to scale image. Must be used with targetWidth.
Aspect ratio remains constant. (Number)
. mediaType: Set the type of media to select from. Only works
when PictureSourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM. Defined
in nagivator.camera.MediaType (Number) Camera.MediaType={
PICTURE:0, // allow selection of still pictures only. DEFAULT. Will
return format specified via DestinationType
VIDEO:1, // allow selection of video only, WILL ALWAYS RETURN
FILE_URI
ALLMEDIA :2 // allow selection from all media types
}; . correctOrientation: Rotate the image to correct for the orientation of the device
during capture. (Boolean)
. saveToPhotoAlbum: Save the image to the photo album on the device after
capture. (Boolean)
. popoverOptions: iOS-only options that specify popover location in iPad.
Defined in CameraPopoverOptions.
. cameraDirection: Choose the camera to use (front- or back-facing). Defined
in navigator.camera.Direction (Number) Camera.Direction={
BACK :0, // Use the back-facing camera
FRONT :1 // Use the front-facing camera
}; Android Quirks . Ignores the allowEdit parameter.
. Camera.PictureSourceType.PHOTOLIBRARY and Camera.PictureSourceType.SAVEDPHOTOALBUM both display the same photo album. 简单的列子:
1.拍照并获取Base64编码的图像: navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
} 2.拍照并获取图像文件路径: 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);
} 3.一个完整的例子: <!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">
varpictureSource; //图片来源
vardestinationType; //设置返回值的格式
// 等待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);
// 获取图像句柄
varsmallImage = document.getElementById('smallImage');
// 取消隐藏的图像元素
smallImage.style.display = 'block';
// 显示拍摄的照片
// 使用内嵌CSS规则来缩放图片
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// 当成功得到一张照片的URI后被调用
function onPhotoURISuccess(imageURI) {
// 取消注释以查看图片文件的URI
// console.log(imageURI);
// 获取图片句柄
varlargeImage = 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"capturePhoto();">Capture Photo</button><br>
<button"capturePhotoEdit();">Capture Editable Photo</button><br>
<button"getPhoto(pictureSource.PHOTOLIBRARY);">From Photo
Library</button><br>
<button"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> 二、 拍照 function getPictureFromCamera()
{ navigator.camera.getPicture(onSuccess, onFail, { quality: 50,destinationType:
Camera.DestinationType.DATA_URL,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY });
} 三、 预览照片 function getPictureFromePhotoLibrary()
{
navigator.camera.getPicture(onSuccessFromLib, onFail, { allowEdit:true,quality:
90,destinationType:Camera.DestinationType.FILE_URI ,sourceType:navigator.camera.PictureSourceType.PHOTOLIBRARY,targetHeight:288,targetWidth:288 });
function onSuccessFromLib(imageURI)
{
alert("imageURI"+imageURI);
var image = document.getElementById('myImage');
image.src = imageURI;
}
}