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;
37 private final BitMatrix image;
38 private final int height;
39 private final int width;
41 public WhiteRectangleDetector(BitMatrix image) {
43 height = image.getHeight();
44 width = image.getWidth();
49 * Detects a candidate barcode-like rectangular region within an image. It
50 * starts around the center of the image, increases the size of the candidate
51 * region until it finds a white rectangular region.
54 * @return {@link ResultPoint}[] describing the corners of the rectangular
55 * region. The first and last points are opposed on the diagonal, as
56 * are the second and third. The first point will be the topmost
57 * point and the last, the bottommost. The second point will be
58 * leftmost and the third, the rightmost
59 * @throws NotFoundException if no Data Matrix Code can be found
61 public ResultPoint[] detect() throws NotFoundException {
63 int left = (width - INIT_SIZE) / 2;
64 int right = (width + INIT_SIZE) / 2;
65 int up = (height - INIT_SIZE) / 2;
66 int down = (height + INIT_SIZE) / 2;
67 boolean sizeExceeded = false;
68 boolean aBlackPointFoundOnBorder = true;
69 boolean atLeastOneBlackPointFoundOnBorder = false;
71 while (aBlackPointFoundOnBorder) {
73 aBlackPointFoundOnBorder = false;
78 boolean rightBorderNotWhite = true;
79 while (rightBorderNotWhite && right < width) {
80 rightBorderNotWhite = containsBlackPoint(up, down, right, false);
81 if (rightBorderNotWhite) {
83 aBlackPointFoundOnBorder = true;
95 boolean bottomBorderNotWhite = true;
96 while (bottomBorderNotWhite && down < height) {
97 bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
98 if (bottomBorderNotWhite) {
100 aBlackPointFoundOnBorder = true;
104 if (down >= height) {
112 boolean leftBorderNotWhite = true;
113 while (leftBorderNotWhite && left >= 0) {
114 leftBorderNotWhite = containsBlackPoint(up, down, left, false);
115 if (leftBorderNotWhite) {
117 aBlackPointFoundOnBorder = true;
129 boolean topBorderNotWhite = true;
130 while (topBorderNotWhite && up >= 0) {
131 topBorderNotWhite = containsBlackPoint(left, right, up, true);
132 if (topBorderNotWhite) {
134 aBlackPointFoundOnBorder = true;
143 if (aBlackPointFoundOnBorder) {
144 atLeastOneBlackPointFoundOnBorder = true;
149 if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
151 ResultPoint x = null, y = null, z = null, t = null;
153 final int max_size = right - left;
155 for (int i = 1; i < max_size; i++) {
156 z = getBlackPointOnSegment(left, down - i, left + i, down);
163 throw NotFoundException.getNotFoundInstance();
167 for (int i = 1; i < max_size; i++) {
168 t = getBlackPointOnSegment(left, up + i, left + i, up);
175 throw NotFoundException.getNotFoundInstance();
179 for (int i = 1; i < max_size; i++) {
180 x = getBlackPointOnSegment(right, up + i, right - i, up);
187 throw NotFoundException.getNotFoundInstance();
191 for (int i = 1; i < max_size; i++) {
192 y = getBlackPointOnSegment(right, down - i, right - i, down);
199 throw NotFoundException.getNotFoundInstance();
202 return centerEdges(y, z, x, t);
205 throw NotFoundException.getNotFoundInstance();
210 * Ends up being a bit faster than Math.round(). This merely rounds its
211 * argument to the nearest int, where x.5 rounds up.
213 private static int round(float d) {
214 return (int) (d + 0.5f);
217 private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
218 int dist = distanceL2(aX, aY, bX, bY);
219 float xStep = (bX - aX) / dist;
220 float yStep = (bY - aY) / dist;
222 for (int i = 0; i < dist; i++) {
223 int x = round(aX + i * xStep);
224 int y = round(aY + i * yStep);
225 if (image.get(x, y)) {
226 return new ResultPoint(x, y);
232 private static int distanceL2(float aX, float aY, float bX, float bY) {
233 float xDiff = aX - bX;
234 float yDiff = aY - bY;
235 return round((float) Math.sqrt(xDiff * xDiff + yDiff * yDiff));
239 * recenters the points of a constant distance towards the center
241 * @param y bottom most point
242 * @param z left most point
243 * @param x right most point
244 * @param t top most point
245 * @return {@link ResultPoint}[] describing the corners of the rectangular
246 * region. The first and last points are opposed on the diagonal, as
247 * are the second and third. The first point will be the topmost
248 * point and the last, the bottommost. The second point will be
249 * leftmost and the third, the rightmost
251 private ResultPoint[] centerEdges(ResultPoint y, ResultPoint z,
252 ResultPoint x, ResultPoint t) {
271 if (yi < width / 2) {
272 return new ResultPoint[]{
273 new ResultPoint(ti - corr, tj + corr),
274 new ResultPoint(zi + corr, zj + corr),
275 new ResultPoint(xi - corr, xj - corr),
276 new ResultPoint(yi + corr, yj - corr)};
278 return new ResultPoint[]{
279 new ResultPoint(ti + corr, tj + corr),
280 new ResultPoint(zi + corr, zj - corr),
281 new ResultPoint(xi - corr, xj + corr),
282 new ResultPoint(yi - corr, yj - corr)};
287 * Determines whether a segment contains a black point
289 * @param a min value of the scanned coordinate
290 * @param b max value of the scanned coordinate
291 * @param fixed value of fixed coordinate
292 * @param horizontal set to true if scan must be horizontal, false if vertical
293 * @return true if a black point has been found, else false.
295 private boolean containsBlackPoint(int a, int b, int fixed, boolean horizontal) {
298 for (int x = a; x <= b; x++) {
299 if (image.get(x, fixed)) {
304 for (int y = a; y <= b; y++) {
305 if (image.get(fixed, y)) {