出于测试目的,我复制了在phonegap camera API上找到的完整示例,并在onPhotoDataSuccess上放置了一个警报,以测试函数何时被触发。在拍摄第一张照片时,将不会显示警报。但是,在第一次尝试之后,将在保存照片后显示警报。
有什么建议吗?如果有什么不清楚的地方,我很乐意说得更具体一些。
我在我的Android Galaxy S3上测试了下面的代码
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for device API libraries to load
//
document.addEventListener("deviceready",onDeviceReady,false);
// device APIs are available
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64-encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
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>-更新1
我已经在另一段代码上进行了测试:
(function () {
$scroller = $('.scroller'),
// Take a picture using the camera or select one from the library
takePicture = function (e) {
var options = {
quality: 45,
targetWidth: 1000,
targetHeight: 1000,
destinationType: Camera.DestinationType.FILE_URI,
encodingType: Camera.EncodingType.JPEG,
sourceType: Camera.PictureSourceType.CAMERA
};
navigator.camera.getPicture(
function (imageURI) {
console.log(imageURI);
alert('test');
$scroller.append('<img src="' + imageURI + '"/>');
},
function (message) {
// We typically get here because the use canceled the photo operation. Fail silently.
}, options);
return false;
};
$('.camera-btn').on('click', takePicture);
}());这也有同样的效果。它在第一次捕捉期间不做任何事情,但在第二次捕捉之后显示图片。我还发现第二张照片后面的照片是我拍的第一张照片。getPicture中的第一个参数似乎不会在第一个快照上触发。这很令人沮丧,因为logcat并没有向我展示任何可以使用的东西。
-更新2
我刚刚在Phonegap Build上试过,它工作正常。所以这一定和插件有关...
发布于 2013-10-25 00:36:39
从3.0.0更新到3.1.0后,我也遇到了同样的问题。相机延迟,没有地理位置等。
查看文件platforms\android\cordova\version是否声明了旧版本。然后你需要更新你的平台。这就是我所做的。
删除所有插件:删除平台:cordova plugin rm org.apache.cordova.camera
cordova platform remove android (将删除您对*.java文件所做的更改)
cordova plugin add org.apache.cordova.camera
这基本上就像创建一个新项目。
https://stackoverflow.com/questions/18650715
复制相似问题