BinaryBitmap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. /*
  3. * Copyright 2009 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. var IllegalArgumentException_1 = require("./IllegalArgumentException");
  19. var BinaryBitmap = /** @class */ (function () {
  20. function BinaryBitmap(binarizer) {
  21. this.binarizer = binarizer;
  22. if (binarizer === null) {
  23. throw new IllegalArgumentException_1.default('Binarizer must be non-null.');
  24. }
  25. }
  26. /**
  27. * @return The width of the bitmap.
  28. */
  29. BinaryBitmap.prototype.getWidth = function () {
  30. return this.binarizer.getWidth();
  31. };
  32. /**
  33. * @return The height of the bitmap.
  34. */
  35. BinaryBitmap.prototype.getHeight = function () {
  36. return this.binarizer.getHeight();
  37. };
  38. /**
  39. * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  40. * cached data. Callers should assume this method is expensive and call it as seldom as possible.
  41. * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  42. *
  43. * @param y The row to fetch, which must be in [0, bitmap height)
  44. * @param row An optional preallocated array. If null or too small, it will be ignored.
  45. * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  46. * @return The array of bits for this row (true means black).
  47. * @throws NotFoundException if row can't be binarized
  48. */
  49. BinaryBitmap.prototype.getBlackRow = function (y /*int*/, row) {
  50. return this.binarizer.getBlackRow(y, row);
  51. };
  52. /**
  53. * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
  54. * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  55. * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  56. * fetched using getBlackRow(), so don't mix and match between them.
  57. *
  58. * @return The 2D array of bits for the image (true means black).
  59. * @throws NotFoundException if image can't be binarized to make a matrix
  60. */
  61. BinaryBitmap.prototype.getBlackMatrix = function () {
  62. // The matrix is created on demand the first time it is requested, then cached. There are two
  63. // reasons for this:
  64. // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
  65. // 1D Reader finds a barcode before the 2D Readers run.
  66. // 2. This work will only be done once even if the caller installs multiple 2D Readers.
  67. if (this.matrix === null || this.matrix === undefined) {
  68. this.matrix = this.binarizer.getBlackMatrix();
  69. }
  70. return this.matrix;
  71. };
  72. /**
  73. * @return Whether this bitmap can be cropped.
  74. */
  75. BinaryBitmap.prototype.isCropSupported = function () {
  76. return this.binarizer.getLuminanceSource().isCropSupported();
  77. };
  78. /**
  79. * Returns a new object with cropped image data. Implementations may keep a reference to the
  80. * original data rather than a copy. Only callable if isCropSupported() is true.
  81. *
  82. * @param left The left coordinate, which must be in [0,getWidth())
  83. * @param top The top coordinate, which must be in [0,getHeight())
  84. * @param width The width of the rectangle to crop.
  85. * @param height The height of the rectangle to crop.
  86. * @return A cropped version of this object.
  87. */
  88. BinaryBitmap.prototype.crop = function (left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  89. var newSource = this.binarizer.getLuminanceSource().crop(left, top, width, height);
  90. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  91. };
  92. /**
  93. * @return Whether this bitmap supports counter-clockwise rotation.
  94. */
  95. BinaryBitmap.prototype.isRotateSupported = function () {
  96. return this.binarizer.getLuminanceSource().isRotateSupported();
  97. };
  98. /**
  99. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  100. * Only callable if {@link #isRotateSupported()} is true.
  101. *
  102. * @return A rotated version of this object.
  103. */
  104. BinaryBitmap.prototype.rotateCounterClockwise = function () {
  105. var newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise();
  106. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  107. };
  108. /**
  109. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  110. * Only callable if {@link #isRotateSupported()} is true.
  111. *
  112. * @return A rotated version of this object.
  113. */
  114. BinaryBitmap.prototype.rotateCounterClockwise45 = function () {
  115. var newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise45();
  116. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  117. };
  118. /*@Override*/
  119. BinaryBitmap.prototype.toString = function () {
  120. try {
  121. return this.getBlackMatrix().toString();
  122. }
  123. catch (e /*: NotFoundException*/) {
  124. return '';
  125. }
  126. };
  127. return BinaryBitmap;
  128. }());
  129. exports.default = BinaryBitmap;