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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * 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 "ZXErrors.h"
#import "ZXMathUtils.h"
#import "ZXWhiteRectangleDetector.h"
 
@interface ZXWhiteRectangleDetector ()
 
@property (nonatomic, strong, readonly) ZXBitMatrix *image;
@property (nonatomic, assign, readonly) int height;
@property (nonatomic, assign, readonly) int width;
@property (nonatomic, assign, readonly) int leftInit;
@property (nonatomic, assign, readonly) int rightInit;
@property (nonatomic, assign, readonly) int downInit;
@property (nonatomic, assign, readonly) int upInit;
 
@end
 
const int ZX_INIT_SIZE = 10;
const int ZX_CORR = 1;
 
@implementation ZXWhiteRectangleDetector
 
- (id)initWithImage:(ZXBitMatrix *)image error:(NSError **)error {
  return [self initWithImage:image initSize:ZX_INIT_SIZE x:image.width / 2 y:image.height / 2 error:error];
}
 
- (id)initWithImage:(ZXBitMatrix *)image initSize:(int)initSize x:(int)x y:(int)y error:(NSError **)error {
  if (self = [super init]) {
    _image = image;
    _height = image.height;
    _width = image.width;
    int halfsize = initSize / 2;
    _leftInit = x - halfsize;
    _rightInit = x + halfsize;
    _upInit = y - halfsize;
    _downInit = y + halfsize;
    if (_upInit < 0 || _leftInit < 0 || _downInit >= _height || _rightInit >= _width) {
      if (error) *error = ZXNotFoundErrorInstance();
      return nil;
    }
  }
 
  return self;
}
 
- (NSArray *)detectWithError:(NSError **)error {
  int left = self.leftInit;
  int right = self.rightInit;
  int up = self.upInit;
  int down = self.downInit;
  BOOL sizeExceeded = NO;
  BOOL aBlackPointFoundOnBorder = YES;
  BOOL atLeastOneBlackPointFoundOnBorder = NO;
 
  BOOL atLeastOneBlackPointFoundOnRight = NO;
  BOOL atLeastOneBlackPointFoundOnBottom = NO;
  BOOL atLeastOneBlackPointFoundOnLeft = NO;
  BOOL atLeastOneBlackPointFoundOnTop = NO;
 
  while (aBlackPointFoundOnBorder) {
    aBlackPointFoundOnBorder = NO;
 
    // .....
    // .   |
    // .....
    BOOL rightBorderNotWhite = YES;
    while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < self.width) {
      rightBorderNotWhite = [self containsBlackPoint:up b:down fixed:right horizontal:NO];
      if (rightBorderNotWhite) {
        right++;
        aBlackPointFoundOnBorder = YES;
        atLeastOneBlackPointFoundOnRight = YES;
      } else if (!atLeastOneBlackPointFoundOnRight) {
        right++;
      }
    }
 
    if (right >= self.width) {
      sizeExceeded = YES;
      break;
    }
 
    // .....
    // .   .
    // .___.
    BOOL bottomBorderNotWhite = YES;
    while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < self.height) {
      bottomBorderNotWhite = [self containsBlackPoint:left b:right fixed:down horizontal:YES];
      if (bottomBorderNotWhite) {
        down++;
        aBlackPointFoundOnBorder = YES;
        atLeastOneBlackPointFoundOnBottom = YES;
      } else if (!atLeastOneBlackPointFoundOnBottom) {
        down++;
      }
    }
 
    if (down >= self.height) {
      sizeExceeded = YES;
      break;
    }
 
    // .....
    // |   .
    // .....
    BOOL leftBorderNotWhite = YES;
    while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
      leftBorderNotWhite = [self containsBlackPoint:up b:down fixed:left horizontal:NO];
      if (leftBorderNotWhite) {
        left--;
        aBlackPointFoundOnBorder = YES;
        atLeastOneBlackPointFoundOnLeft = YES;
      } else if (!atLeastOneBlackPointFoundOnLeft) {
        left--;
      }
    }
 
    if (left < 0) {
      sizeExceeded = YES;
      break;
    }
 
    // .___.
    // .   .
    // .....
    BOOL topBorderNotWhite = YES;
    while ((topBorderNotWhite  || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
      topBorderNotWhite = [self containsBlackPoint:left b:right fixed:up horizontal:YES];
      if (topBorderNotWhite) {
        up--;
        aBlackPointFoundOnBorder = YES;
        atLeastOneBlackPointFoundOnTop = YES;
      } else if (!atLeastOneBlackPointFoundOnTop) {
        up--;
      }
    }
 
    if (up < 0) {
      sizeExceeded = YES;
      break;
    }
 
    if (aBlackPointFoundOnBorder) {
      atLeastOneBlackPointFoundOnBorder = YES;
    }
  }
 
  if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
    int maxSize = right - left;
 
    ZXResultPoint *z = nil;
    for (int i = 1; i < maxSize; i++) {
      z = [self blackPointOnSegment:left aY:down - i bX:left + i bY:down];
      if (z != nil) {
        break;
      }
    }
 
    if (z == nil) {
      if (error) *error = ZXNotFoundErrorInstance();
      return nil;
    }
 
    ZXResultPoint *t = nil;
    for (int i = 1; i < maxSize; i++) {
      t = [self blackPointOnSegment:left aY:up + i bX:left + i bY:up];
      if (t != nil) {
        break;
      }
    }
 
    if (t == nil) {
      if (error) *error = ZXNotFoundErrorInstance();
      return nil;
    }
 
    ZXResultPoint *x = nil;
    for (int i = 1; i < maxSize; i++) {
      x = [self blackPointOnSegment:right aY:up + i bX:right - i bY:up];
      if (x != nil) {
        break;
      }
    }
 
    if (x == nil) {
      if (error) *error = ZXNotFoundErrorInstance();
      return nil;
    }
 
    ZXResultPoint *y = nil;
    for (int i = 1; i < maxSize; i++) {
      y = [self blackPointOnSegment:right aY:down - i bX:right - i bY:down];
      if (y != nil) {
        break;
      }
    }
 
    if (y == nil) {
      if (error) *error = ZXNotFoundErrorInstance();
      return nil;
    }
    return [self centerEdges:y z:z x:x t:t];
  } else {
    if (error) *error = ZXNotFoundErrorInstance();
    return nil;
  }
}
 
 
- (ZXResultPoint *)blackPointOnSegment:(float)aX aY:(float)aY bX:(float)bX bY:(float)bY {
  int dist = [ZXMathUtils round:[ZXMathUtils distance:aX aY:aY bX:bX bY:bY]];
  float xStep = (bX - aX) / dist;
  float yStep = (bY - aY) / dist;
 
  for (int i = 0; i < dist; i++) {
    int x = [ZXMathUtils round:aX + i * xStep];
    int y = [ZXMathUtils round:aY + i * yStep];
    if ([self.image getX:x y:y]) {
      return [[ZXResultPoint alloc] initWithX:x y:y];
    }
  }
 
  return nil;
}
 
/**
 * recenters the points of a constant distance towards the center
 *
 * @param y bottom most point
 * @param z left most point
 * @param x right most point
 * @param t top most point
 * @return ZXResultPoint array describing the corners of the rectangular
 *         region. The first and last points are opposed on the diagonal, as
 *         are the second and third. The first point will be the topmost
 *         point and the last, the bottommost. The second point will be
 *         leftmost and the third, the rightmost
 */
- (NSArray *)centerEdges:(ZXResultPoint *)y z:(ZXResultPoint *)z x:(ZXResultPoint *)x t:(ZXResultPoint *)t {
  //
  //       t            t
  //  z                      x
  //        x    OR    z
  //   y                    y
  //
 
  float yi = y.x;
  float yj = y.y;
  float zi = z.x;
  float zj = z.y;
  float xi = x.x;
  float xj = x.y;
  float ti = t.x;
  float tj = t.y;
 
  if (yi < self.width / 2.0f) {
    return @[[[ZXResultPoint alloc] initWithX:ti - ZX_CORR y:tj + ZX_CORR],
             [[ZXResultPoint alloc] initWithX:zi + ZX_CORR y:zj + ZX_CORR],
             [[ZXResultPoint alloc] initWithX:xi - ZX_CORR y:xj - ZX_CORR],
             [[ZXResultPoint alloc] initWithX:yi + ZX_CORR y:yj - ZX_CORR]];
  } else {
    return @[[[ZXResultPoint alloc] initWithX:ti + ZX_CORR y:tj + ZX_CORR],
             [[ZXResultPoint alloc] initWithX:zi + ZX_CORR y:zj - ZX_CORR],
             [[ZXResultPoint alloc] initWithX:xi - ZX_CORR y:xj + ZX_CORR],
             [[ZXResultPoint alloc] initWithX:yi - ZX_CORR y:yj - ZX_CORR]];
  }
}
 
/**
 * Determines whether a segment contains a black point
 *
 * @param a          min value of the scanned coordinate
 * @param b          max value of the scanned coordinate
 * @param fixed      value of fixed coordinate
 * @param horizontal set to true if scan must be horizontal, false if vertical
 * @return true if a black point has been found, else false.
 */
- (BOOL)containsBlackPoint:(int)a b:(int)b fixed:(int)fixed horizontal:(BOOL)horizontal {
  if (horizontal) {
    for (int x = a; x <= b; x++) {
      if ([self.image getX:x y:fixed]) {
        return YES;
      }
    }
  } else {
    for (int y = a; y <= b; y++) {
      if ([self.image getX:fixed y:y]) {
        return YES;
      }
    }
  }
 
  return NO;
}
 
@end