phonegap project

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jordy R

Jordy R

04/01/2015 10:57:12
Quote Anchor link
Ik ben bezig met een simpele app die een foto maakt en vervolgens upload naar mijn server.
Nu kan ik echter geen PHP gebruiken in een app en moet ik het met ajax en javascript doen.

Het is me gelukt om een foto te maken en te weergeven in de app.
En om een foto te selecteren uit de Library en deze up te Loaden naar mijn server.

Nu wil ik graag standaard de gemaakte foto selecteren en deze uploaden naar mijn server.
Ik heb een geen idee hoe ik dat doe en ik kan er ook maar weinig documentatie over vinden.

Dit is mijn script

Kunnen jullie mij helpen ?

Start.html
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  <!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
        <title>Foto maken</title>
        
        
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>

    <script type="text/javascript" charset="utf-8">

 

        // Wait for PhoneGap to load

        document.addEventListener("deviceready", onDeviceReady, false);

 

        // PhoneGap is ready

        function onDeviceReady() {

         // Do cool things here...

        }

 

        function getImage() {

            // Retrieve image file location from specified source

            navigator.camera.getPicture(uploadPhoto, function(message) {

            alert('get picture failed');

        },{

            quality: 50,

            destinationType: navigator.camera.DestinationType.FILE_URI,

            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY

        }

            );

 

        }

 

        function uploadPhoto(imageURI) {

            var options = new FileUploadOptions();

            options.fileKey="file";

            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);

            options.mimeType="image/jpeg";

 

            var params = new Object();

            params.value1 = "test";

            params.value2 = "param";

 

            options.params = params;

            options.chunkedMode = false;

 

            var ft = new FileTransfer();

            ft.upload(imageURI, "http://yourdomain.com/upload.php", win, fail, options);

        }

 

        function win(r) {

            console.log("Code = " + r.responseCode);

            console.log("Response = " + r.response);

            console.log("Sent = " + r.bytesSent);

            alert(r.response);

        }

 

        function fail(error) {

            alert("An error has occurred: Code = " = error.code);

        }

 

        </script>


    </head>
    <body>
        <div id="container"style="width:100%;">
        
                 <button id="maak-foto">foto</button>
      <button onclick="getImage();">Upload a Photo</button>
        </div>
                    
          
        
      
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>


Index.js
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // 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');
    },
    // 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);
    }
};
// Anonieme functie openen
(function()
{
    // Wachten tot het apparaat klaar is
    document.addEventListener('deviceready', onDeviceReady, false);
 
    // Het apparaat is klaar
    function onDeviceReady()
    {
        // Telefoon functies kunnen pas aangesproken worden
        // wanneer het apparaat klaar is. Ofwel wanneer deze
        // functie aangesproken wordt kunnen deze pas gebruikt
        // worden. Aangezien de gebruiker hoogst waarschijnlijk
        // niet eerder op de foto maken knop klikt dan dat het
        // apparaat klaar is, maak ik hier nu geen gebruik van.
    }
 
    // Foto nemen is gelukt
    function cameraSuccess(imageData)
    {
        // Na de foto maken knop de afbeelding weergeven
        $('#maak-foto').after('<img src="' + "data:image/jpeg;base64," + imageData + '"width="250px">');
    }
 
    // Foto nemen is mislukt
    function cameraError(message)
    {
        // Foutmelding weergeven
        alert('Er ging iets fout! ' + message);
    }
 
    // Op de foto maken knop geklikt
    $('#maak-foto').click(function()
    {
        // Eventueel eerder gemaakte foto verwijderen
        $('img').remove();
 
        // Foto maken
        navigator.camera.getPicture( cameraSuccess, cameraError, {
            quality: 50, // Foto kwaliteit
            destinationType: Camera.DestinationType.DATA_URL // Base64 gecodeerde afbeelding als resultaat
        });
    });
 
// Functie direct uitvoeren
})();


Toevoeging op 04/01/2015 17:28:47:

Oplossing gevonden op regel 48 sourceType: navigator.camera.PictureSourceType.CAMERA
 
Er zijn nog geen reacties op dit bericht.



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.