You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
4.8 KiB

  1. /**
  2. * cordova is available under the MIT License (2008).
  3. * See http://opensource.org/licenses/alphabetical for full text.
  4. *
  5. * Copyright (c) Matt Kane 2010
  6. * Copyright (c) 2011, IBM Corporation
  7. * Copyright (c) 2012-2017, Adobe Systems
  8. */
  9. var exec = cordova.require("cordova/exec");
  10. var scanInProgress = false;
  11. /**
  12. * Constructor.
  13. *
  14. * @returns {BarcodeScanner}
  15. */
  16. function BarcodeScanner() {
  17. /**
  18. * Encoding constants.
  19. *
  20. * @type Object
  21. */
  22. this.Encode = {
  23. TEXT_TYPE: "TEXT_TYPE",
  24. EMAIL_TYPE: "EMAIL_TYPE",
  25. PHONE_TYPE: "PHONE_TYPE",
  26. SMS_TYPE: "SMS_TYPE"
  27. // CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascript to Java
  28. // LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascript to Java
  29. };
  30. /**
  31. * Barcode format constants, defined in ZXing library.
  32. *
  33. * @type Object
  34. */
  35. this.format = {
  36. "all_1D": 61918,
  37. "aztec": 1,
  38. "codabar": 2,
  39. "code_128": 16,
  40. "code_39": 4,
  41. "code_93": 8,
  42. "data_MATRIX": 32,
  43. "ean_13": 128,
  44. "ean_8": 64,
  45. "itf": 256,
  46. "maxicode": 512,
  47. "msi": 131072,
  48. "pdf_417": 1024,
  49. "plessey": 262144,
  50. "qr_CODE": 2048,
  51. "rss_14": 4096,
  52. "rss_EXPANDED": 8192,
  53. "upc_A": 16384,
  54. "upc_E": 32768,
  55. "upc_EAN_EXTENSION": 65536
  56. };
  57. }
  58. /**
  59. * Read code from scanner.
  60. *
  61. * @param {Function} successCallback This function will recieve a result object: {
  62. * text : '12345-mock', // The code that was scanned.
  63. * format : 'FORMAT_NAME', // Code format.
  64. * cancelled : true/false, // Was canceled.
  65. * }
  66. * @param {Function} errorCallback
  67. * @param config
  68. */
  69. BarcodeScanner.prototype.scan = function (successCallback, errorCallback, config) {
  70. if (config instanceof Array) {
  71. // do nothing
  72. } else {
  73. if (typeof(config) === 'object') {
  74. // string spaces between formats, ZXing does not like that
  75. if (config.formats) {
  76. config.formats = config.formats.replace(/\s+/g, '');
  77. }
  78. config = [ config ];
  79. } else {
  80. config = [];
  81. }
  82. }
  83. if (errorCallback == null) {
  84. errorCallback = function () {
  85. };
  86. }
  87. if (typeof errorCallback != "function") {
  88. console.log("BarcodeScanner.scan failure: failure parameter not a function");
  89. return;
  90. }
  91. if (typeof successCallback != "function") {
  92. console.log("BarcodeScanner.scan failure: success callback parameter must be a function");
  93. return;
  94. }
  95. if (scanInProgress) {
  96. errorCallback('Scan is already in progress');
  97. return;
  98. }
  99. scanInProgress = true;
  100. exec(
  101. function(result) {
  102. scanInProgress = false;
  103. // work around bug in ZXing library
  104. if (result.format === 'UPC_A' && result.text.length === 13) {
  105. result.text = result.text.substring(1);
  106. }
  107. successCallback(result);
  108. },
  109. function(error) {
  110. scanInProgress = false;
  111. errorCallback(error);
  112. },
  113. 'BarcodeScanner',
  114. 'scan',
  115. config
  116. );
  117. };
  118. //-------------------------------------------------------------------
  119. BarcodeScanner.prototype.encode = function (type, data, successCallback, errorCallback, options) {
  120. if (errorCallback == null) {
  121. errorCallback = function () {
  122. };
  123. }
  124. if (typeof errorCallback != "function") {
  125. console.log("BarcodeScanner.encode failure: failure parameter not a function");
  126. return;
  127. }
  128. if (typeof successCallback != "function") {
  129. console.log("BarcodeScanner.encode failure: success callback parameter must be a function");
  130. return;
  131. }
  132. exec(successCallback, errorCallback, 'BarcodeScanner', 'encode', [
  133. {"type": type, "data": data, "options": options}
  134. ]);
  135. };
  136. var barcodeScanner = new BarcodeScanner();
  137. module.exports = barcodeScanner;