2 * Copyright 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.common.detector;
19 import com.google.zxing.NotFoundException;
20 import com.google.zxing.ResultPoint;
21 import com.google.zxing.common.BitMatrix;
25 * Detects a candidate barcode-like rectangular region within an image. It
26 * starts around the center of the image, increases the size of the candidate
27 * region until it finds a white rectangular region. By keeping track of the
28 * last black points it encountered, it determines the corners of the barcode.
31 * @author David Olivier
33 public final class WhiteRectangleDetector {
35 private static final int INIT_SIZE = 40;
36 private static final int CORR = 1;
38 private final BitMatrix image;
39 private final int height;
40 private final int width;
42 public WhiteRectangleDetector(BitMatrix image) {
44 height = image.getHeight();
45 width = image.getWidth();
50 * Detects a candidate barcode-like rectangular region within an image. It
51 * starts around the center of the image, increases the size of the candidate
52 * region until it finds a white rectangular region.
55 * @return {@link ResultPoint}[] describing the corners of the rectangular
56 * region. The first and last points are opposed on the diagonal, as
57 * are the second and third. The first point will be the topmost
58 * point and the last, the bottommost. The second point will be
59 * leftmost and the third, the rightmost
60 * @throws NotFoundException if no Data Matrix Code can be found
62 public ResultPoint[] detect() throws NotFoundException {
64 int left = (width - INIT_SIZE) >> 1;
65 int right = (width + INIT_SIZE) >> 1;
66 int up = (height - INIT_SIZE) >> 1;
67 int down = (height + INIT_SIZE) >> 1;
68 boolean sizeExceeded = false;
69 boolean aBlackPointFoundOnBorder = true;
70 boolean atLeastOneBlackPointFoundOnBorder = false;
72 while (aBlackPointFoundOnBorder) {
74 aBlackPointFoundOnBorder = false;
79 boolean rightBorderNotWhite = true;
80 while (rightBorderNotWhite && right < width) {
81 rightBorderNotWhite = containsBlackPoint(up, down, right, false);
82 if (rightBorderNotWhite) {
84 aBlackPointFoundOnBorder = true;
96 boolean bottomBorderNotWhite = true;
97 while (bottomBorderNotWhite && down < height) {
98 bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
99 if (bottomBorderNotWhite) {
101 aBlackPointFoundOnBorder = true;
105 if (down >= height) {
113 boolean leftBorderNotWhite = true;
114 while (leftBorderNotWhite && left >= 0) {
115 leftBorderNotWhite = containsBlackPoint(up, down, left, false);
116 if (leftBorderNotWhite) {
118 aBlackPointFoundOnBorder = true;
130 boolean topBorderNotWhite = true;
131 while (topBorderNotWhite && up >= 0) {
132 topBorderNotWhite = containsBlackPoint(left, right, up, true);
133 if (topBorderNotWhite) {
135 aBlackPointFoundOnBorder = true;
144 if (aBlackPointFoundOnBorder) {
145 atLeastOneBlackPointFoundOnBorder = true;
150 if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
152 int maxSize = right - left;
154 ResultPoint z = null;
155 for (int i = 1; i < maxSize; i++) {
156 z = getBlackPointOnSegment(left, down - i, left + i, down);
163 throw NotFoundException.getNotFoundInstance();
166 ResultPoint t = null;
168 for (int i = 1; i < maxSize; i++) {
169 t = getBlackPointOnSegment(left, up + i, left + i, up);
176 throw NotFoundException.getNotFoundInstance();
179 ResultPoint x = null;
181 for (int i = 1; i < maxSize; i++) {
182 x = getBlackPointOnSegment(right, up + i, right - i, up);
189 throw NotFoundException.getNotFoundInstance();
192 ResultPoint y = null;
194 for (int i = 1; i < maxSize; i++) {
195 y = getBlackPointOnSegment(right, down - i, right - i, down);
202 throw NotFoundException.getNotFoundInstance();
205 return centerEdges(y, z, x, t);
208 throw NotFoundException.getNotFoundInstance();
213 * Ends up being a bit faster than Math.round(). This merely rounds its
214 * argument to the nearest int, where x.5 rounds up.
216 private static int round(float d) {
217 return (int) (d + 0.5f);
220 private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
221 int dist = distanceL2(aX, aY, bX, bY);
222 float xStep = (bX - aX) / dist;
223 float yStep = (bY - aY) / dist;
225 for (int i = 0; i < dist; i++) {
226 int x = round(aX + i * xStep);
227 int y = round(aY + i * yStep);
228 if (image.get(x, y)) {
229 return new ResultPoint(x, y);
235 private static int distanceL2(float aX, float aY, float bX, float bY) {
236 float xDiff = aX - bX;
237 float yDiff = aY - bY;
238 return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff));
242 * recenters the points of a constant distance towards the center
244 * @param y bottom most point
245 * @param z left most point
246 * @param x right most point
247 * @param t top most point
248 * @return {@link ResultPoint}[] describing the corners of the rectangular
249 * region. The first and last points are opposed on the diagonal, as
250 * are the second and third. The first point will be the topmost
251 * point and the last, the bottommost. The second point will be
252 * leftmost and the third, the rightmost
254 private ResultPoint[] centerEdges(ResultPoint y, ResultPoint z,
255 ResultPoint x, ResultPoint t) {
273 if (yi < width / 2) {
274 return new ResultPoint[]{
275 new ResultPoint(ti - CORR, tj + CORR),
276 new ResultPoint(zi + CORR, zj + CORR),
277 new ResultPoint(xi - CORR, xj - CORR),
278 new ResultPoint(yi + CORR, yj - CORR)};
280 return new ResultPoint[]{
281 new ResultPoint(ti + CORR, tj + CORR),
282 new ResultPoint(zi + CORR, zj - CORR),
283 new ResultPoint(xi - CORR, xj + CORR),
284 new ResultPoint(yi - CORR, yj - CORR)};
289 * Determines whether a segment contains a black point
291 * @param a min value of the scanned coordinate
292 * @param b max value of the scanned coordinate
293 * @param fixed value of fixed coordinate
294 * @param horizontal set to true if scan must be horizontal, false if vertical
295 * @return true if a black point has been found, else false.
297 private boolean containsBlackPoint(int a, int b, int fixed, boolean horizontal) {
300 for (int x = a; x <= b; x++) {
301 if (image.get(x, fixed)) {
306 for (int y = a; y <= b; y++) {
307 if (image.get(fixed, y)) {