Result.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import System from './util/System';
  17. /**
  18. * <p>Encapsulates the result of decoding a barcode within an image.</p>
  19. *
  20. * @author Sean Owen
  21. */
  22. export default class Result {
  23. // public constructor(private text: string,
  24. // Uint8Array rawBytes,
  25. // ResultPoconst resultPoints: Int32Array,
  26. // BarcodeFormat format) {
  27. // this(text, rawBytes, resultPoints, format, System.currentTimeMillis())
  28. // }
  29. // public constructor(text: string,
  30. // Uint8Array rawBytes,
  31. // ResultPoconst resultPoints: Int32Array,
  32. // BarcodeFormat format,
  33. // long timestamp) {
  34. // this(text, rawBytes, rawBytes == null ? 0 : 8 * rawBytes.length,
  35. // resultPoints, format, timestamp)
  36. // }
  37. constructor(text, rawBytes, numBits = rawBytes == null ? 0 : 8 * rawBytes.length, resultPoints, format, timestamp = System.currentTimeMillis()) {
  38. this.text = text;
  39. this.rawBytes = rawBytes;
  40. this.numBits = numBits;
  41. this.resultPoints = resultPoints;
  42. this.format = format;
  43. this.timestamp = timestamp;
  44. this.text = text;
  45. this.rawBytes = rawBytes;
  46. if (undefined === numBits || null === numBits) {
  47. this.numBits = (rawBytes === null || rawBytes === undefined) ? 0 : 8 * rawBytes.length;
  48. }
  49. else {
  50. this.numBits = numBits;
  51. }
  52. this.resultPoints = resultPoints;
  53. this.format = format;
  54. this.resultMetadata = null;
  55. if (undefined === timestamp || null === timestamp) {
  56. this.timestamp = System.currentTimeMillis();
  57. }
  58. else {
  59. this.timestamp = timestamp;
  60. }
  61. }
  62. /**
  63. * @return raw text encoded by the barcode
  64. */
  65. getText() {
  66. return this.text;
  67. }
  68. /**
  69. * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
  70. */
  71. getRawBytes() {
  72. return this.rawBytes;
  73. }
  74. /**
  75. * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
  76. * @since 3.3.0
  77. */
  78. getNumBits() {
  79. return this.numBits;
  80. }
  81. /**
  82. * @return points related to the barcode in the image. These are typically points
  83. * identifying finder patterns or the corners of the barcode. The exact meaning is
  84. * specific to the type of barcode that was decoded.
  85. */
  86. getResultPoints() {
  87. return this.resultPoints;
  88. }
  89. /**
  90. * @return {@link BarcodeFormat} representing the format of the barcode that was decoded
  91. */
  92. getBarcodeFormat() {
  93. return this.format;
  94. }
  95. /**
  96. * @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
  97. * {@code null}. This contains optional metadata about what was detected about the barcode,
  98. * like orientation.
  99. */
  100. getResultMetadata() {
  101. return this.resultMetadata;
  102. }
  103. putMetadata(type, value) {
  104. if (this.resultMetadata === null) {
  105. this.resultMetadata = new Map();
  106. }
  107. this.resultMetadata.set(type, value);
  108. }
  109. putAllMetadata(metadata) {
  110. if (metadata !== null) {
  111. if (this.resultMetadata === null) {
  112. this.resultMetadata = metadata;
  113. }
  114. else {
  115. this.resultMetadata = new Map(metadata);
  116. }
  117. }
  118. }
  119. addResultPoints(newPoints) {
  120. const oldPoints = this.resultPoints;
  121. if (oldPoints === null) {
  122. this.resultPoints = newPoints;
  123. }
  124. else if (newPoints !== null && newPoints.length > 0) {
  125. const allPoints = new Array(oldPoints.length + newPoints.length);
  126. System.arraycopy(oldPoints, 0, allPoints, 0, oldPoints.length);
  127. System.arraycopy(newPoints, 0, allPoints, oldPoints.length, newPoints.length);
  128. this.resultPoints = allPoints;
  129. }
  130. }
  131. getTimestamp() {
  132. return this.timestamp;
  133. }
  134. /*@Override*/
  135. toString() {
  136. return this.text;
  137. }
  138. }