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.

ZXByQuadrantReader.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2012 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 "ZXBinaryBitmap.h"
  17. #import "ZXByQuadrantReader.h"
  18. #import "ZXDecodeHints.h"
  19. #import "ZXErrors.h"
  20. #import "ZXResult.h"
  21. #import "ZXResultPoint.h"
  22. @interface ZXByQuadrantReader ()
  23. @property (nonatomic, weak, readonly) id<ZXReader> delegate;
  24. @end
  25. @implementation ZXByQuadrantReader
  26. - (id)initWithDelegate:(id<ZXReader>)delegate {
  27. if (self = [super init]) {
  28. _delegate = delegate;
  29. }
  30. return self;
  31. }
  32. - (ZXResult *)decode:(ZXBinaryBitmap *)image error:(NSError **)error {
  33. return [self decode:image hints:nil error:error];
  34. }
  35. - (ZXResult *)decode:(ZXBinaryBitmap *)image hints:(ZXDecodeHints *)hints error:(NSError **)error {
  36. int width = image.width;
  37. int height = image.height;
  38. int halfWidth = width / 2;
  39. int halfHeight = height / 2;
  40. // No need to call makeAbsolute as results will be relative to original top left here
  41. NSError *decodeError = nil;
  42. ZXResult *result = [self.delegate decode:[image crop:0 top:0 width:halfWidth height:halfHeight]
  43. hints:hints
  44. error:&decodeError];
  45. if (result) {
  46. return result;
  47. } else if (decodeError.code != ZXNotFoundError) {
  48. if (error) *error = decodeError;
  49. return nil;
  50. }
  51. decodeError = nil;
  52. result = [self.delegate decode:[image crop:halfWidth top:0 width:halfWidth height:halfHeight]
  53. hints:hints
  54. error:&decodeError];
  55. if (result) {
  56. [self makeAbsolute:result.resultPoints leftOffset:halfWidth topOffset:0];
  57. return result;
  58. } else if (decodeError.code != ZXNotFoundError) {
  59. if (error) *error = decodeError;
  60. return nil;
  61. }
  62. decodeError = nil;
  63. result = [self.delegate decode:[image crop:0 top:halfHeight width:halfWidth height:halfHeight]
  64. hints:hints
  65. error:&decodeError];
  66. if (result) {
  67. [self makeAbsolute:result.resultPoints leftOffset:0 topOffset:halfHeight];
  68. return result;
  69. } else if (decodeError.code != ZXNotFoundError) {
  70. if (error) *error = decodeError;
  71. return nil;
  72. }
  73. decodeError = nil;
  74. result = [self.delegate decode:[image crop:halfWidth top:halfHeight width:halfWidth height:halfHeight]
  75. hints:hints
  76. error:&decodeError];
  77. if (result) {
  78. [self makeAbsolute:result.resultPoints leftOffset:halfWidth topOffset:halfHeight];
  79. return result;
  80. } else if (decodeError.code != ZXNotFoundError) {
  81. if (error) *error = decodeError;
  82. return nil;
  83. }
  84. int quarterWidth = halfWidth / 2;
  85. int quarterHeight = halfHeight / 2;
  86. ZXBinaryBitmap *center = [image crop:quarterWidth top:quarterHeight width:halfWidth height:halfHeight];
  87. result = [self.delegate decode:center hints:hints error:error];
  88. if (result) {
  89. [self makeAbsolute:result.resultPoints leftOffset:quarterWidth topOffset:quarterHeight];
  90. }
  91. return result;
  92. }
  93. - (void)reset {
  94. [self.delegate reset];
  95. }
  96. - (void)makeAbsolute:(NSMutableArray *)points leftOffset:(int)leftOffset topOffset:(int)topOffset {
  97. if (points) {
  98. for (int i = 0; i < points.count; i++) {
  99. ZXResultPoint *relative = points[i];
  100. points[i] = [[ZXResultPoint alloc] initWithX:relative.x + leftOffset y:relative.y + topOffset];
  101. }
  102. }
  103. }
  104. @end