BinaryBitmap.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2009 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 IllegalArgumentException from './IllegalArgumentException';
  17. export default class BinaryBitmap {
  18. constructor(binarizer) {
  19. this.binarizer = binarizer;
  20. if (binarizer === null) {
  21. throw new IllegalArgumentException('Binarizer must be non-null.');
  22. }
  23. }
  24. /**
  25. * @return The width of the bitmap.
  26. */
  27. getWidth() {
  28. return this.binarizer.getWidth();
  29. }
  30. /**
  31. * @return The height of the bitmap.
  32. */
  33. getHeight() {
  34. return this.binarizer.getHeight();
  35. }
  36. /**
  37. * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  38. * cached data. Callers should assume this method is expensive and call it as seldom as possible.
  39. * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  40. *
  41. * @param y The row to fetch, which must be in [0, bitmap height)
  42. * @param row An optional preallocated array. If null or too small, it will be ignored.
  43. * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  44. * @return The array of bits for this row (true means black).
  45. * @throws NotFoundException if row can't be binarized
  46. */
  47. getBlackRow(y /*int*/, row) {
  48. return this.binarizer.getBlackRow(y, row);
  49. }
  50. /**
  51. * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
  52. * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  53. * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  54. * fetched using getBlackRow(), so don't mix and match between them.
  55. *
  56. * @return The 2D array of bits for the image (true means black).
  57. * @throws NotFoundException if image can't be binarized to make a matrix
  58. */
  59. getBlackMatrix() {
  60. // The matrix is created on demand the first time it is requested, then cached. There are two
  61. // reasons for this:
  62. // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
  63. // 1D Reader finds a barcode before the 2D Readers run.
  64. // 2. This work will only be done once even if the caller installs multiple 2D Readers.
  65. if (this.matrix === null || this.matrix === undefined) {
  66. this.matrix = this.binarizer.getBlackMatrix();
  67. }
  68. return this.matrix;
  69. }
  70. /**
  71. * @return Whether this bitmap can be cropped.
  72. */
  73. isCropSupported() {
  74. return this.binarizer.getLuminanceSource().isCropSupported();
  75. }
  76. /**
  77. * Returns a new object with cropped image data. Implementations may keep a reference to the
  78. * original data rather than a copy. Only callable if isCropSupported() is true.
  79. *
  80. * @param left The left coordinate, which must be in [0,getWidth())
  81. * @param top The top coordinate, which must be in [0,getHeight())
  82. * @param width The width of the rectangle to crop.
  83. * @param height The height of the rectangle to crop.
  84. * @return A cropped version of this object.
  85. */
  86. crop(left /*int*/, top /*int*/, width /*int*/, height /*int*/) {
  87. const newSource = this.binarizer.getLuminanceSource().crop(left, top, width, height);
  88. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  89. }
  90. /**
  91. * @return Whether this bitmap supports counter-clockwise rotation.
  92. */
  93. isRotateSupported() {
  94. return this.binarizer.getLuminanceSource().isRotateSupported();
  95. }
  96. /**
  97. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  98. * Only callable if {@link #isRotateSupported()} is true.
  99. *
  100. * @return A rotated version of this object.
  101. */
  102. rotateCounterClockwise() {
  103. const newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise();
  104. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  105. }
  106. /**
  107. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  108. * Only callable if {@link #isRotateSupported()} is true.
  109. *
  110. * @return A rotated version of this object.
  111. */
  112. rotateCounterClockwise45() {
  113. const newSource = this.binarizer.getLuminanceSource().rotateCounterClockwise45();
  114. return new BinaryBitmap(this.binarizer.createBinarizer(newSource));
  115. }
  116. /*@Override*/
  117. toString() {
  118. try {
  119. return this.getBlackMatrix().toString();
  120. }
  121. catch (e /*: NotFoundException*/) {
  122. return '';
  123. }
  124. }
  125. }