JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright 2012 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#import "ZXBinaryBitmap.h"
#import "ZXByQuadrantReader.h"
#import "ZXDecodeHints.h"
#import "ZXErrors.h"
#import "ZXResult.h"
#import "ZXResultPoint.h"
 
@interface ZXByQuadrantReader ()
 
@property (nonatomic, weak, readonly) id<ZXReader> delegate;
 
@end
 
@implementation ZXByQuadrantReader
 
- (id)initWithDelegate:(id<ZXReader>)delegate {
  if (self = [super init]) {
    _delegate = delegate;
  }
 
  return self;
}
 
- (ZXResult *)decode:(ZXBinaryBitmap *)image error:(NSError **)error {
  return [self decode:image hints:nil error:error];
}
 
- (ZXResult *)decode:(ZXBinaryBitmap *)image hints:(ZXDecodeHints *)hints error:(NSError **)error {
  int width = image.width;
  int height = image.height;
  int halfWidth = width / 2;
  int halfHeight = height / 2;
 
  // No need to call makeAbsolute as results will be relative to original top left here
  NSError *decodeError = nil;
  ZXResult *result = [self.delegate decode:[image crop:0 top:0 width:halfWidth height:halfHeight]
                                     hints:hints
                                     error:&decodeError];
  if (result) {
    return result;
  } else if (decodeError.code != ZXNotFoundError) {
    if (error) *error = decodeError;
    return nil;
  }
 
  decodeError = nil;
  result = [self.delegate decode:[image crop:halfWidth top:0 width:halfWidth height:halfHeight]
                           hints:hints
                           error:&decodeError];
  if (result) {
    [self makeAbsolute:result.resultPoints leftOffset:halfWidth topOffset:0];
    return result;
  } else if (decodeError.code != ZXNotFoundError) {
    if (error) *error = decodeError;
    return nil;
  }
 
  decodeError = nil;
  result = [self.delegate decode:[image crop:0 top:halfHeight width:halfWidth height:halfHeight]
                           hints:hints
                           error:&decodeError];
  if (result) {
    [self makeAbsolute:result.resultPoints leftOffset:0 topOffset:halfHeight];
    return result;
  } else if (decodeError.code != ZXNotFoundError) {
    if (error) *error = decodeError;
    return nil;
  }
 
  decodeError = nil;
  result = [self.delegate decode:[image crop:halfWidth top:halfHeight width:halfWidth height:halfHeight]
                           hints:hints
                           error:&decodeError];
  if (result) {
    [self makeAbsolute:result.resultPoints leftOffset:halfWidth topOffset:halfHeight];
    return result;
  } else if (decodeError.code != ZXNotFoundError) {
    if (error) *error = decodeError;
    return nil;
  }
 
  int quarterWidth = halfWidth / 2;
  int quarterHeight = halfHeight / 2;
  ZXBinaryBitmap *center = [image crop:quarterWidth top:quarterHeight width:halfWidth height:halfHeight];
  result = [self.delegate decode:center hints:hints error:error];
  if (result) {
    [self makeAbsolute:result.resultPoints leftOffset:quarterWidth topOffset:quarterHeight];
  }
  return result;
}
 
- (void)reset {
  [self.delegate reset];
}
 
- (void)makeAbsolute:(NSMutableArray *)points leftOffset:(int)leftOffset topOffset:(int)topOffset {
  if (points) {
    for (int i = 0; i < points.count; i++) {
      ZXResultPoint *relative = points[i];
      points[i] = [[ZXResultPoint alloc] initWithX:relative.x + leftOffset y:relative.y + topOffset];
    }
  }
}
 
@end