Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ZXPDF417BoundingBox.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2013 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 "ZXBitMatrix.h"
  17. #import "ZXPDF417BoundingBox.h"
  18. #import "ZXResultPoint.h"
  19. @interface ZXPDF417BoundingBox ()
  20. @property (nonatomic, strong, readonly) ZXBitMatrix *image;
  21. @property (nonatomic, assign) int minX;
  22. @property (nonatomic, assign) int maxX;
  23. @property (nonatomic, assign) int minY;
  24. @property (nonatomic, assign) int maxY;
  25. @end
  26. @implementation ZXPDF417BoundingBox
  27. - (id)initWithImage:(ZXBitMatrix *)image topLeft:(ZXResultPoint *)topLeft bottomLeft:(ZXResultPoint *)bottomLeft
  28. topRight:(ZXResultPoint *)topRight bottomRight:(ZXResultPoint *)bottomRight {
  29. if ((!topLeft && !topRight) || (!bottomLeft && !bottomRight) ||
  30. (topLeft && !bottomLeft) || (topRight && !bottomRight)) {
  31. return nil;
  32. }
  33. self = [super init];
  34. if (self) {
  35. _image = image;
  36. _topLeft = topLeft;
  37. _bottomLeft = bottomLeft;
  38. _topRight = topRight;
  39. _bottomRight = bottomRight;
  40. [self calculateMinMaxValues];
  41. }
  42. return self;
  43. }
  44. - (id)initWithBoundingBox:(ZXPDF417BoundingBox *)boundingBox {
  45. return [self initWithImage:boundingBox.image topLeft:boundingBox.topLeft bottomLeft:boundingBox.bottomLeft
  46. topRight:boundingBox.topRight bottomRight:boundingBox.bottomRight];
  47. }
  48. + (ZXPDF417BoundingBox *)mergeLeftBox:(ZXPDF417BoundingBox *)leftBox rightBox:(ZXPDF417BoundingBox *)rightBox {
  49. if (!leftBox) {
  50. return rightBox;
  51. }
  52. if (!rightBox) {
  53. return leftBox;
  54. }
  55. return [[self alloc] initWithImage:leftBox.image topLeft:leftBox.topLeft bottomLeft:leftBox.bottomLeft
  56. topRight:rightBox.topRight bottomRight:rightBox.bottomRight];
  57. }
  58. - (ZXPDF417BoundingBox *)addMissingRows:(int)missingStartRows missingEndRows:(int)missingEndRows isLeft:(BOOL)isLeft {
  59. ZXResultPoint *newTopLeft = self.topLeft;
  60. ZXResultPoint *newBottomLeft = self.bottomLeft;
  61. ZXResultPoint *newTopRight = self.topRight;
  62. ZXResultPoint *newBottomRight = self.bottomRight;
  63. if (missingStartRows > 0) {
  64. ZXResultPoint *top = isLeft ? self.topLeft : self.topRight;
  65. int newMinY = (int) top.y - missingStartRows;
  66. if (newMinY < 0) {
  67. newMinY = 0;
  68. }
  69. // TODO use existing points to better interpolate the new x positions
  70. ZXResultPoint *newTop = [[ZXResultPoint alloc] initWithX:top.x y:newMinY];
  71. if (isLeft) {
  72. newTopLeft = newTop;
  73. } else {
  74. newTopRight = newTop;
  75. }
  76. }
  77. if (missingEndRows > 0) {
  78. ZXResultPoint *bottom = isLeft ? self.bottomLeft : self.bottomRight;
  79. int newMaxY = (int) bottom.y + missingEndRows;
  80. if (newMaxY >= self.image.height) {
  81. newMaxY = self.image.height - 1;
  82. }
  83. // TODO use existing points to better interpolate the new x positions
  84. ZXResultPoint *newBottom = [[ZXResultPoint alloc] initWithX:bottom.x y:newMaxY];
  85. if (isLeft) {
  86. newBottomLeft = newBottom;
  87. } else {
  88. newBottomRight = newBottom;
  89. }
  90. }
  91. [self calculateMinMaxValues];
  92. return [[ZXPDF417BoundingBox alloc] initWithImage:self.image topLeft:newTopLeft bottomLeft:newBottomLeft topRight:newTopRight bottomRight:newBottomRight];
  93. }
  94. - (void)calculateMinMaxValues {
  95. if (!self.topLeft) {
  96. _topLeft = [[ZXResultPoint alloc] initWithX:0 y:self.topRight.y];
  97. _bottomLeft = [[ZXResultPoint alloc] initWithX:0 y:self.bottomRight.y];
  98. } else if (!self.topRight) {
  99. _topRight = [[ZXResultPoint alloc] initWithX:self.image.width - 1 y:self.topLeft.y];
  100. _bottomRight = [[ZXResultPoint alloc] initWithX:self.image.width - 1 y:self.bottomLeft.y];
  101. }
  102. self.minX = (int) MIN(self.topLeft.x, self.bottomLeft.x);
  103. self.maxX = (int) MAX(self.topRight.x, self.bottomRight.x);
  104. self.minY = (int) MIN(self.topLeft.y, self.topRight.y);
  105. self.maxY = (int) MAX(self.bottomLeft.y, self.bottomRight.y);
  106. }
  107. /*
  108. - (void)setTopRight:(ZXResultPoint *)topRight {
  109. _topRight = topRight;
  110. [self calculateMinMaxValues];
  111. }
  112. - (void)setBottomRight:(ZXResultPoint *)bottomRight {
  113. _bottomRight = bottomRight;
  114. [self calculateMinMaxValues];
  115. }
  116. */
  117. @end