arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-camera/src/browser/CameraProxy.js
plugins/cordova-plugin-camera/src/browser/CameraProxy.jsRaw
  1/*
  2 *
  3 * Licensed to the Apache Software Foundation (ASF) under one
  4 * or more contributor license agreements.  See the NOTICE file
  5 * distributed with this work for additional information
  6 * regarding copyright ownership.  The ASF licenses this file
  7 * to you under the Apache License, Version 2.0 (the
  8 * "License"); you may not use this file except in compliance
  9 * with the License.  You may obtain a copy of the License at
 10 *
 11 *   http://www.apache.org/licenses/LICENSE-2.0
 12 *
 13 * Unless required by applicable law or agreed to in writing,
 14 * software distributed under the License is distributed on an
 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16 * KIND, either express or implied.  See the License for the
 17 * specific language governing permissions and limitations
 18 * under the License.
 19 *
 20 */
 21
 22var HIGHEST_POSSIBLE_Z_INDEX = 2147483647;
 23
 24function takePicture(success, error, opts) {
 25    if (opts && opts[2] === 1) {
 26        capture(success, error, opts);
 27    } else {
 28        var input = document.createElement('input');
 29        input.style.position = 'relative';
 30        input.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
 31        input.className = 'cordova-camera-select';
 32        input.type = 'file';
 33        input.name = 'files[]';
 34
 35        input.onchange = function(inputEvent) {
 36            var reader = new FileReader();
 37            reader.onload = function(readerEvent) {
 38                input.parentNode.removeChild(input);
 39
 40                var imageData = readerEvent.target.result;
 41
 42                return success(imageData.substr(imageData.indexOf(',') + 1));
 43            };
 44
 45            reader.readAsDataURL(inputEvent.target.files[0]);
 46        };
 47
 48        document.body.appendChild(input);
 49    }
 50}
 51
 52function capture(success, errorCallback, opts) {
 53    var localMediaStream;
 54    var targetWidth = opts[3];
 55    var targetHeight = opts[4];
 56
 57    targetWidth = targetWidth == -1?320:targetWidth;
 58    targetHeight = targetHeight == -1?240:targetHeight;
 59
 60    var video = document.createElement('video');
 61    var button = document.createElement('button');
 62    var parent = document.createElement('div');
 63    parent.style.position = 'relative';
 64    parent.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
 65    parent.className = 'cordova-camera-capture';
 66    parent.appendChild(video);
 67    parent.appendChild(button);
 68
 69    video.width = targetWidth;
 70    video.height = targetHeight;
 71    button.innerHTML = 'Capture!';
 72
 73    button.onclick = function() {
 74        // create a canvas and capture a frame from video stream
 75        var canvas = document.createElement('canvas');
 76        canvas.width = targetWidth;
 77        canvas.height = targetHeight;
 78        canvas.getContext('2d').drawImage(video, 0, 0, targetWidth, targetHeight);
 79
 80        // convert image stored in canvas to base64 encoded image
 81        var imageData = canvas.toDataURL('image/png');
 82        imageData = imageData.replace('data:image/png;base64,', '');
 83
 84        // stop video stream, remove video and button.
 85        // Note that MediaStream.stop() is deprecated as of Chrome 47.
 86        if (localMediaStream.stop) {
 87            localMediaStream.stop();
 88        } else {
 89            localMediaStream.getTracks().forEach(function (track) {
 90                track.stop();
 91            });
 92        }
 93        parent.parentNode.removeChild(parent);
 94
 95        return success(imageData);
 96    };
 97
 98    navigator.getUserMedia = navigator.getUserMedia ||
 99                             navigator.webkitGetUserMedia ||
100                             navigator.mozGetUserMedia ||
101                             navigator.msGetUserMedia;
102
103    var successCallback = function(stream) {
104        localMediaStream = stream;
105        video.src = window.URL.createObjectURL(localMediaStream);
106        video.play();
107
108        document.body.appendChild(parent);
109    };
110
111    if (navigator.getUserMedia) {
112        navigator.getUserMedia({video: true, audio: true}, successCallback, errorCallback);
113    } else {
114        alert('Browser does not support camera :(');
115    }
116}
117
118module.exports = {
119    takePicture: takePicture,
120    cleanup: function(){}
121};
122
123require("cordova/exec/proxy").add("Camera",module.exports);