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 ResultPoint a = new ResultPoint(left, down-i);
157 ResultPoint b = new ResultPoint(left+i, down);
158 z = getBlackPointOnSegment(a, b);
165 throw NotFoundException.getNotFoundInstance();
169 for (int i = 1; i < max_size; i++){
170 ResultPoint a = new ResultPoint(left, up+i);
171 ResultPoint b = new ResultPoint(left+i, up);
172 t = getBlackPointOnSegment(a, b);
179 throw NotFoundException.getNotFoundInstance();
183 for (int i = 1; i < max_size; i++){
184 ResultPoint a = new ResultPoint(right, up+i);
185 ResultPoint b = new ResultPoint(right-i, up);
186 x = getBlackPointOnSegment(a, b);
193 throw NotFoundException.getNotFoundInstance();
197 for (int i = 1; i < max_size; i++){
198 ResultPoint a = new ResultPoint(right, down-i);
199 ResultPoint b = new ResultPoint(right-i, down);
200 y = getBlackPointOnSegment(a, b);
207 throw NotFoundException.getNotFoundInstance();
210 return centerEdges(y, z, x, t);
213 throw NotFoundException.getNotFoundInstance();
218 private ResultPoint getBlackPointOnSegment(ResultPoint a, ResultPoint b) {
219 int dist = distanceL2(a, b);
220 float xStep = (b.getX()-a.getX())/dist;
221 float yStep = (b.getY()-a.getY())/dist;
223 for (int i = 0; i < dist; i++){
224 if (image.get(Math.round(a.getX()+i*xStep), Math.round(a.getY()+i*yStep))){
225 return new ResultPoint(Math.round(a.getX()+i*xStep), Math.round(a.getY()+i*yStep));
231 private static int distanceL2(ResultPoint a, ResultPoint b) {
232 return (int) Math.round(Math.sqrt((a.getX() - b.getX())
233 * (a.getX() - b.getX()) + (a.getY() - b.getY())
234 * (a.getY() - b.getY())));
238 * recenters the points of a constant distance towards the center
240 * @param y bottom most point
241 * @param z left most point
242 * @param x right most point
243 * @param t top most point
244 * @return {@link ResultPoint}[] describing the corners of the rectangular
245 * region. The first and last points are opposed on the diagonal, as
246 * are the second and third. The first point will be the topmost
247 * point and the last, the bottommost. The second point will be
248 * leftmost and the third, the rightmost
250 private ResultPoint[] centerEdges(ResultPoint y, ResultPoint z,
251 ResultPoint x, ResultPoint t) {
260 float yi = y.getX(), yj = y.getY(), zi = z.getX(), zj = z.getY(), xi = x
261 .getX(), xj = x.getY(), ti = t.getX(), tj = t.getY();
264 if (yi < width / 2) {
265 return new ResultPoint[]{new ResultPoint(ti - corr, tj + corr),
266 new ResultPoint(zi + corr, zj + corr),
267 new ResultPoint(xi - corr, xj - corr),
268 new ResultPoint(yi + corr, yj - corr)};
270 return new ResultPoint[]{new ResultPoint(ti + corr, tj + corr),
271 new ResultPoint(zi + corr, zj - corr),
272 new ResultPoint(xi - corr, xj + corr),
273 new ResultPoint(yi - corr, yj - corr)};
278 * Determines whether a segment contains a black point
280 * @param a min value of the scanned coordinate
281 * @param b max value of the scanned coordinate
282 * @param fixed value of fixed coordinate
283 * @param horizontal set to true if scan must be horizontal, false if vertical
284 * @return true if a black point has been found, else false.
286 private boolean containsBlackPoint(int a, int b, int fixed, boolean horizontal) {
289 for (int x = a; x <= b; x++) {
290 if (image.get(x, fixed)) {
295 for (int y = a; y <= b; y++) {
296 if (image.get(fixed, y)) {