cameraHelper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*jshint node: true */
  2. /* global Q, resolveLocalFileSystemURL, Camera, cordova */
  3. /*
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. 'use strict';
  24. var cameraConstants = require('../../www/CameraConstants');
  25. function findKeyByValue(set, value) {
  26. for (var k in set) {
  27. if (set.hasOwnProperty(k)) {
  28. if (set[k] == value) {
  29. return k;
  30. }
  31. }
  32. }
  33. return undefined;
  34. }
  35. function getDescription(spec) {
  36. var desc = '';
  37. desc += 'sourceType: ' + findKeyByValue(cameraConstants.PictureSourceType, spec.options.sourceType);
  38. desc += ', destinationType: ' + findKeyByValue(cameraConstants.DestinationType, spec.options.destinationType);
  39. desc += ', encodingType: ' + findKeyByValue(cameraConstants.EncodingType, spec.options.encodingType);
  40. desc += ', allowEdit: ' + spec.options.allowEdit.toString();
  41. desc += ', correctOrientation: ' + spec.options.correctOrientation.toString();
  42. return desc;
  43. }
  44. module.exports.generateSpecs = function (sourceTypes, destinationTypes, encodingTypes, allowEditOptions, correctOrientationOptions) {
  45. var destinationType,
  46. sourceType,
  47. encodingType,
  48. allowEdit,
  49. correctOrientation,
  50. specs = [],
  51. id = 1;
  52. for (destinationType in destinationTypes) {
  53. if (destinationTypes.hasOwnProperty(destinationType)) {
  54. for (sourceType in sourceTypes) {
  55. if (sourceTypes.hasOwnProperty(sourceType)) {
  56. for (encodingType in encodingTypes) {
  57. if (encodingTypes.hasOwnProperty(encodingType)) {
  58. for (allowEdit in allowEditOptions) {
  59. if (allowEditOptions.hasOwnProperty(allowEdit)) {
  60. for (correctOrientation in correctOrientationOptions) {
  61. // if taking picture from photolibrary, don't vary 'correctOrientation' option
  62. if ((sourceTypes[sourceType] === cameraConstants.PictureSourceType.PHOTOLIBRARY ||
  63. sourceTypes[sourceType] === cameraConstants.PictureSourceType.SAVEDPHOTOALBUM) &&
  64. correctOrientation === true) { continue; }
  65. var spec = {
  66. 'id': id++,
  67. 'options': {
  68. 'destinationType': destinationTypes[destinationType],
  69. 'sourceType': sourceTypes[sourceType],
  70. 'encodingType': encodingTypes[encodingType],
  71. 'allowEdit': allowEditOptions[allowEdit],
  72. 'saveToPhotoAlbum': false,
  73. 'correctOrientation': correctOrientationOptions[correctOrientation]
  74. }
  75. };
  76. spec.description = getDescription(spec);
  77. specs.push(spec);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return specs;
  88. };
  89. // calls getPicture() and saves the result in promise
  90. // note that this function is executed in the context of tested app
  91. // and not in the context of tests
  92. module.exports.getPicture = function (opts, pid) {
  93. if (navigator._appiumPromises[pid - 1]) {
  94. navigator._appiumPromises[pid - 1] = null;
  95. }
  96. navigator._appiumPromises[pid] = Q.defer();
  97. navigator.camera.getPicture(function (result) {
  98. navigator._appiumPromises[pid].resolve(result);
  99. }, function (err) {
  100. navigator._appiumPromises[pid].reject(err);
  101. }, opts);
  102. };
  103. // verifies taken picture when the promise is resolved,
  104. // calls a callback with 'OK' if everything is good,
  105. // calls a callback with 'ERROR: <error message>' if something is wrong
  106. // note that this function is executed in the context of tested app
  107. // and not in the context of tests
  108. module.exports.checkPicture = function (pid, options, cb) {
  109. var isIos = cordova.platformId === "ios";
  110. var isAndroid = cordova.platformId === "android";
  111. // skip image type check if it's unmodified on Android:
  112. // https://github.com/apache/cordova-plugin-camera/#android-quirks-1
  113. var skipFileTypeCheckAndroid = isAndroid && options.quality === 100 &&
  114. !options.targetWidth && !options.targetHeight &&
  115. !options.correctOrientation;
  116. // Skip image type check if destination is NATIVE_URI and source - device's photoalbum
  117. // https://github.com/apache/cordova-plugin-camera/#ios-quirks-1
  118. var skipFileTypeCheckiOS = isIos && options.destinationType === Camera.DestinationType.NATIVE_URI &&
  119. (options.sourceType === Camera.PictureSourceType.PHOTOLIBRARY ||
  120. options.sourceType === Camera.PictureSourceType.SAVEDPHOTOALBUM);
  121. var skipFileTypeCheck = skipFileTypeCheckAndroid || skipFileTypeCheckiOS;
  122. var desiredType = 'JPEG';
  123. var mimeType = 'image/jpeg';
  124. if (options.encodingType === Camera.EncodingType.PNG) {
  125. desiredType = 'PNG';
  126. mimeType = 'image/png';
  127. }
  128. function errorCallback(msg) {
  129. if (msg.hasOwnProperty('message')) {
  130. msg = msg.message;
  131. }
  132. cb('ERROR: ' + msg);
  133. }
  134. // verifies the image we get from plugin
  135. function verifyResult(result) {
  136. if (result.length === 0) {
  137. errorCallback('The result is empty.');
  138. return;
  139. } else if (isIos && options.destinationType === Camera.DestinationType.NATIVE_URI && result.indexOf('assets-library:') !== 0) {
  140. errorCallback('Expected "' + result.substring(0, 150) + '"to start with "assets-library:"');
  141. return;
  142. } else if (isIos && options.destinationType === Camera.DestinationType.FILE_URI && result.indexOf('file:') !== 0) {
  143. errorCallback('Expected "' + result.substring(0, 150) + '"to start with "file:"');
  144. return;
  145. }
  146. try {
  147. window.atob(result);
  148. // if we got here it is a base64 string (DATA_URL)
  149. result = "data:" + mimeType + ";base64," + result;
  150. } catch (e) {
  151. // not DATA_URL
  152. if (options.destinationType === Camera.DestinationType.DATA_URL) {
  153. errorCallback('Expected ' + result.substring(0, 150) + 'not to be DATA_URL');
  154. return;
  155. }
  156. }
  157. try {
  158. if (result.indexOf('file:') === 0 ||
  159. result.indexOf('content:') === 0 ||
  160. result.indexOf('assets-library:') === 0) {
  161. if (!window.resolveLocalFileSystemURL) {
  162. errorCallback('Cannot read file. Please install cordova-plugin-file to fix this.');
  163. return;
  164. }
  165. resolveLocalFileSystemURL(result, function (entry) {
  166. if (skipFileTypeCheck) {
  167. displayFile(entry);
  168. } else {
  169. verifyFile(entry);
  170. }
  171. });
  172. } else {
  173. displayImage(result);
  174. }
  175. } catch (e) {
  176. errorCallback(e);
  177. }
  178. }
  179. // verifies that the file type matches the requested type
  180. function verifyFile(entry) {
  181. try {
  182. var reader = new FileReader();
  183. reader.onloadend = function(e) {
  184. var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
  185. var header = '';
  186. for(var i = 0; i < arr.length; i++) {
  187. header += arr[i].toString(16);
  188. }
  189. var actualType = 'unknown';
  190. switch (header) {
  191. case "89504e47":
  192. actualType = 'PNG';
  193. break;
  194. case 'ffd8ffe0':
  195. case 'ffd8ffe1':
  196. case 'ffd8ffe2':
  197. actualType = 'JPEG';
  198. break;
  199. }
  200. if (actualType === desiredType) {
  201. displayFile(entry);
  202. } else {
  203. errorCallback('File type mismatch. Expected ' + desiredType + ', got ' + actualType);
  204. }
  205. };
  206. reader.onerror = function (e) {
  207. errorCallback(e);
  208. };
  209. entry.file(function (file) {
  210. reader.readAsArrayBuffer(file);
  211. }, function (e) {
  212. errorCallback(e);
  213. });
  214. } catch (e) {
  215. errorCallback(e);
  216. }
  217. }
  218. // reads the file, then displays the image
  219. function displayFile(entry) {
  220. function onFileReceived(file) {
  221. var reader = new FileReader();
  222. reader.onerror = function (e) {
  223. errorCallback(e);
  224. };
  225. reader.onloadend = function (evt) {
  226. displayImage(evt.target.result);
  227. };
  228. reader.readAsDataURL(file);
  229. }
  230. entry.file(onFileReceived, function (e) {
  231. errorCallback(e);
  232. });
  233. }
  234. function displayImage(image) {
  235. try {
  236. var imgEl = document.getElementById('camera_test_image');
  237. if (!imgEl) {
  238. imgEl = document.createElement('img');
  239. imgEl.id = 'camera_test_image';
  240. document.body.appendChild(imgEl);
  241. }
  242. var timedOut = false;
  243. var loadTimeout = setTimeout(function () {
  244. timedOut = true;
  245. imgEl.src = '';
  246. errorCallback('The image did not load: ' + image.substring(0, 150));
  247. }, 10000);
  248. var done = function (status) {
  249. if (!timedOut) {
  250. clearTimeout(loadTimeout);
  251. imgEl.src = '';
  252. cb(status);
  253. }
  254. };
  255. imgEl.onload = function () {
  256. try {
  257. // aspect ratio is preserved so only one dimension should match
  258. if ((typeof options.targetWidth === 'number' && imgEl.naturalWidth !== options.targetWidth) &&
  259. (typeof options.targetHeight === 'number' && imgEl.naturalHeight !== options.targetHeight))
  260. {
  261. done('ERROR: Wrong image size: ' + imgEl.naturalWidth + 'x' + imgEl.naturalHeight +
  262. '. Requested size: ' + options.targetWidth + 'x' + options.targetHeight);
  263. } else {
  264. done('OK');
  265. }
  266. } catch (e) {
  267. errorCallback(e);
  268. }
  269. };
  270. imgEl.src = image;
  271. } catch (e) {
  272. errorCallback(e);
  273. }
  274. }
  275. navigator._appiumPromises[pid].promise
  276. .then(function (result) {
  277. verifyResult(result);
  278. })
  279. .fail(function (e) {
  280. errorCallback(e);
  281. });
  282. };