我第一次尝试使用 Cordova 原生插件。我从相机和文档中提供的示例代码开始。但是,这失败了,navigator.camera
并且未定义。
我已经包含了下面的代码。
<div data-role="page" id="CameraPage">
<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) {
alert("Photo Data Success");
// 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;
}
// A button will call this function
//
function capturePhoto() {
alert("function is called");
if(_.isUndefined(navigator.camera)){
alert("Camera is not defined");
}else{
alert("WTF?!");
}
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
我根据 CLI 说明安装了相机插件
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
我还添加了 cordova.js 文件。
<script type="text/javascript" charset="utf-8" src="js/cordova-js/lib/cordova.js"></script>