CameraProxy.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. cordova.define("cordova-plugin-camera.CameraProxy", function(require, exports, module) { /*
  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. var HIGHEST_POSSIBLE_Z_INDEX = 2147483647;
  22. function takePicture (success, error, opts) {
  23. if (opts && opts[2] === 1) {
  24. capture(success, error, opts);
  25. } else {
  26. var input = document.createElement('input');
  27. input.style.position = 'relative';
  28. input.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
  29. input.className = 'cordova-camera-select';
  30. input.type = 'file';
  31. input.name = 'files[]';
  32. input.onchange = function (inputEvent) {
  33. var reader = new FileReader(); /* eslint no-undef : 0 */
  34. reader.onload = function (readerEvent) {
  35. input.parentNode.removeChild(input);
  36. var imageData = readerEvent.target.result;
  37. return success(imageData.substr(imageData.indexOf(',') + 1));
  38. };
  39. reader.readAsDataURL(inputEvent.target.files[0]);
  40. };
  41. document.body.appendChild(input);
  42. }
  43. }
  44. function capture (success, errorCallback, opts) {
  45. var localMediaStream;
  46. var targetWidth = opts[3];
  47. var targetHeight = opts[4];
  48. targetWidth = targetWidth === -1 ? 320 : targetWidth;
  49. targetHeight = targetHeight === -1 ? 240 : targetHeight;
  50. var video = document.createElement('video');
  51. var button = document.createElement('button');
  52. var parent = document.createElement('div');
  53. parent.style.position = 'relative';
  54. parent.style.zIndex = HIGHEST_POSSIBLE_Z_INDEX;
  55. parent.className = 'cordova-camera-capture';
  56. parent.appendChild(video);
  57. parent.appendChild(button);
  58. video.width = targetWidth;
  59. video.height = targetHeight;
  60. button.innerHTML = 'Capture!';
  61. button.onclick = function () {
  62. // create a canvas and capture a frame from video stream
  63. var canvas = document.createElement('canvas');
  64. canvas.width = targetWidth;
  65. canvas.height = targetHeight;
  66. canvas.getContext('2d').drawImage(video, 0, 0, targetWidth, targetHeight);
  67. // convert image stored in canvas to base64 encoded image
  68. var imageData = canvas.toDataURL('image/png');
  69. imageData = imageData.replace('data:image/png;base64,', '');
  70. // stop video stream, remove video and button.
  71. // Note that MediaStream.stop() is deprecated as of Chrome 47.
  72. if (localMediaStream.stop) {
  73. localMediaStream.stop();
  74. } else {
  75. localMediaStream.getTracks().forEach(function (track) {
  76. track.stop();
  77. });
  78. }
  79. parent.parentNode.removeChild(parent);
  80. return success(imageData);
  81. };
  82. navigator.getUserMedia = navigator.getUserMedia ||
  83. navigator.webkitGetUserMedia ||
  84. navigator.mozGetUserMedia ||
  85. navigator.msGetUserMedia;
  86. var successCallback = function (stream) {
  87. localMediaStream = stream;
  88. if ('srcObject' in video) {
  89. video.srcObject = localMediaStream;
  90. } else {
  91. video.src = window.URL.createObjectURL(localMediaStream);
  92. }
  93. video.play();
  94. document.body.appendChild(parent);
  95. };
  96. if (navigator.getUserMedia) {
  97. navigator.getUserMedia({video: true, audio: false}, successCallback, errorCallback);
  98. } else {
  99. alert('Browser does not support camera :(');
  100. }
  101. }
  102. module.exports = {
  103. takePicture: takePicture,
  104. cleanup: function () {}
  105. };
  106. require('cordova/exec/proxy').add('Camera', module.exports);
  107. });