Capture photo with camera in cordova app

I keep learning on cordova and javascript (I need it). I am trying to create a very small application that uses a camera. For this I am using the code below [ http://cordova.apache.org/docs/en/2.5.0/cordova_camera_camera.md.html#Camera] and I am trying to adapt this code in the cordova project in netbeans.

I get the code below. I provide my index.html and my index.js. I have a problem when I am testing an emulator. When I click on the button, nothing happens, there is no error message, nothing and obviously not being captured by the camera. there seems to be a line issue in the capturePhoto method because I have a warning in netbeans (the destinationType global variable 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 for the help for the help

+3


source to share


1 answer


In your appeal to navigator.camera.getPicture

you need destinationType: Camera.DestinationType.DATA_URL

, not destinationType: destinationType.DATA_URL

. You must also specify sourceType and mediaType so that you can 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);
}

      

0


source







All Articles