我继续训练有关cordova和javascript(我需要它)。 我尝试使用相机创建一个非常小的应用程序。为此,我已采取以下提供的代码 [http://cordova.apache.org/docs/en/2.5.0/cordova_camera_camera.md.html#Camera] 并且我试图在netbeans的cordova项目中调整此代码。
我得到以下代码。我提供了index.html和index.js。 我在模拟器中测试时遇到问题。当我点击按钮时,什么都没发生,没有错误信息,没有什么,显然它不带相机的图片。似乎方法capturePhoto中的行存在问题,因为我在netbeans中有一个警告(未声明全局变量destinationType)。请问你能帮帮我吗 ?
的index.html
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<button id="myBtn">Capture Photo</button> <br>
<img style="display:none;width:80px;height:80px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
<script>
document.getElementById("myBtn").addEventListener("click", capturePhoto());
</script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
index.js
var app = { pictureSource: "", destinationType: "", // Application Constructor initialize: function() { this.bindEvents(); }, // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); }, // deviceready Event Handler // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function() { app.receivedEvent('deviceready'); app.pictureSource = navigator.camera.PictureSourceType; app.destinationType = navigator.camera.DestinationType; }, // Update DOM on a Received Event receivedEvent: function(id) { var parentElement = document.getElementById(id); var listeningElement = parentElement.querySelector('.listening'); var receivedElement = parentElement.querySelector('.received'); listeningElement.setAttribute('style', 'display:none;'); receivedElement.setAttribute('style', 'display:block;'); console.log('Received Event: ' + id); }, // Called when a photo is successfully retrieved onPhotoDataSuccess: function(imageData) { var smallImage = document.getElementById('smallImage'); smallImage.style.display = 'block'; smallImage.src = "data:image/jpeg;base64," + imageData; }, // A button will call this function capturePhoto: function() { navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 100, destinationType: destinationType.DATA_URL }); }, // Called if something bad happens. onFail: function(message) { alert('Failed because: ' + message); } }; app.initialize();
再次感谢你的帮助
答案 0 :(得分:0)
在致电navigator.camera.getPicture
时,您需要destinationType: Camera.DestinationType.DATA_URL
而不是destinationType: destinationType.DATA_URL
。您还应该指定sourceType和mediaType,因此您可能希望执行以下操作:
function capturePhoto() {
var options = {
quality: 100,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
mediaType: Camera.MediaType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
saveToPhotoAlbum: true
};
navigator.camera.getPicture(onPhotoDataSuccess, onFail, options);
}