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

标签 javascript cordova netbeans camera

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

相关文章:

javascript - Jquery Datepicker,束手无策

javascript - 滚动的音频

ios - 使用绝对设备URI时Phonegap 2.2音频错误

android - 如何从 Windows 将应用程序提交到 Apple App Store

java - 将 war 部署到不受 netbeans 管理的本地 Tomcat 实例

java - 如何删除值(value),确定剩余值(value)的最小值

java - Netbeans (OS X) 每次更改参数时都会挂起

javascript - 在 PEG.js 中测试所有替代模式

javascript - 当我在 Javascript 的对象中定义一个对象时,有没有一种方法可以一次性做到这一点?

javascript - Android 浏览器中某些键没有按键事件