3
votes

I continue my training on cordova and javascript (i need it). I try to create a very little application using camera. For that i have take the code provide below [http://cordova.apache.org/docs/en/2.5.0/cordova_camera_camera.md.html#Camera] and i'm trying to adapt this code in a cordova project in netbeans.

i get the code below. I provide my index.html and my index.js. i have a problem when i test in the emulator. When i click on the button, nothing happened, there is not error message, nothing and obviously it take not picture with camera. it seem there is a problem with the line in the method capturePhoto because i have a warning in netbeans (global variable destinationType is not declared). Could you help me please ?

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(); 

thanks again by advance for your help

1

1 Answers

0
votes

In your call to navigator.camera.getPicture, you want destinationType: Camera.DestinationType.DATA_URL rather than destinationType: destinationType.DATA_URL. You should also specify the sourceType and mediaType, so you might want to do something like this:

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);
}