gpt4 book ai didi

javascript - 在 cordova 应用程序中使用相机拍摄照片

转载 作者:行者123 更新时间:2023-12-04 00:09:08 25 4
gpt4 key购买 nike

我继续接受有关 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>

索引.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(); 

再次感谢您的帮助

最佳答案

在调用 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);
}

关于javascript - 在 cordova 应用程序中使用相机拍摄照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31071503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com