我正在创建一个跨 Web 平台应用程序,我希望能够访问平台相册并捕获图像。我已将 cordova.js 文件和 Javascript 放在我的标题中,但它似乎没有做任何事情
JavaScript
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for Cordova to connect with the device
document.addEventListener("deviceready",onDeviceReady,false);
// Cordova is ready to be used!
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Take picture using device camera and retrieve image as base64-encoded string
function capturePhoto() {
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, allowEdit : true,
destinationType: destinationType.DATA_URL });
}
// Called if something bad happens.
function onFail(message) {
alert('Failed because: ' + message);
}
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Unhide image elements
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
largeImage.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;
}
</script>
我的视图页面中的 Html 代码
<div data-role="content" class="jqm-content">
<div data-role="content">
<button data-theme="d" onclick="capturePhoto();">Capture Photo</button>
<button data-theme="d" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Browse Photos</button>
<img style="display:none;width:100%;" id="largeImage" src="" /> <br>
</div>
</div>