More intelligence to avoid very deep recursion
[zxing.git] / core / src / com / google / zxing / multi / GenericMultipleBarcodeReader.java
1 /*
2  * Copyright 2009 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.multi;
18
19 import com.google.zxing.Reader;
20 import com.google.zxing.Result;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.ResultPoint;
24 import com.google.zxing.common.CroppedMonochromeBitmapSource;
25
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 /**
30  * <p>Attempts to locate multiple barcodes in an image by repeatedly decoding portion of the image.
31  * After one barcode is found, the areas left, above, right and below the barcode's
32  * {@link com.google.zxing.ResultPoint}s are scanned, recursively.</p>
33  *
34  * <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to find multiple
35  * 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might prevent
36  * detecting any one of them.</p>
37  *
38  * <p>That is, instead of passing a {@link Reader} a caller might pass
39  * <code>new ByQuadrantReader(reader)</code>.</p>
40  *
41  * @author Sean Owen
42  */
43 public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
44
45   private static final int MIN_DIMENSION_TO_RECUR = 30;
46
47   private final Reader delegate;
48
49   public GenericMultipleBarcodeReader(Reader delegate) {
50     this.delegate = delegate;
51   }
52
53   public Result[] decodeMultiple(MonochromeBitmapSource image) throws ReaderException {
54     return decodeMultiple(image, null);
55   }
56
57   public Result[] decodeMultiple(MonochromeBitmapSource image, Hashtable hints)
58       throws ReaderException {
59     Vector results = new Vector();
60     doDecodeMultiple(image, hints, results, 0, 0);
61     if (results.isEmpty()) {
62       throw ReaderException.getInstance();
63     }
64     int numResults = results.size();
65     Result[] resultArray = new Result[numResults];
66     for (int i = 0; i < numResults; i++) {
67       resultArray[i] = (Result) results.elementAt(i);
68     }
69     return resultArray;
70   }
71
72   private void doDecodeMultiple(MonochromeBitmapSource image,
73                                 Hashtable hints,
74                                 Vector results,
75                                 int xOffset,
76                                 int yOffset) {
77     Result result;
78     try {
79       result = delegate.decode(image, hints);
80     } catch (ReaderException re) {
81       return;
82     }
83     boolean alreadyFound = false;
84     for (int i = 0; i < results.size(); i++) {
85       Result existingResult = (Result) results.elementAt(i);
86       if (existingResult.getText().equals(result.getText())) {
87         alreadyFound = true;
88         break;
89       }
90     }
91     if (alreadyFound) {
92       return;
93     }
94     results.addElement(translateResultPoints(result, xOffset, yOffset));
95     ResultPoint[] resultPoints = result.getResultPoints();
96     if (resultPoints == null || resultPoints.length == 0) {
97       return;
98     }
99     int width = image.getWidth();
100     int height = image.getHeight();
101     float minX = width;
102     float minY = height;
103     float maxX = 0.0f;
104     float maxY = 0.0f;
105     for (int i = 0; i < resultPoints.length; i++) {
106       ResultPoint point = resultPoints[i];
107       float x = point.getX();
108       float y = point.getY();
109       if (x < minX) {
110         minX = x;
111       }
112       if (y < minY) {
113         minY = y;
114       }
115       if (x > maxX) {
116         maxX = x;
117       }
118       if (y > maxY) {
119         maxY = y;
120       }
121     }
122
123     if (minX > MIN_DIMENSION_TO_RECUR) {
124       doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, (int) minX, height),
125                        hints, results, 0, 0);
126     }
127     if (minY > MIN_DIMENSION_TO_RECUR) {
128       doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, width, (int) minY),
129                        hints, results, 0, 0);
130     }
131     if (maxX < width - MIN_DIMENSION_TO_RECUR) {
132       doDecodeMultiple(new CroppedMonochromeBitmapSource(image, (int) maxX, 0, width, height),
133                        hints, results, (int) maxX, 0);
134     }
135     if (maxY < height - MIN_DIMENSION_TO_RECUR) {
136       doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, (int) maxY, width, height),
137                        hints, results, 0, (int) maxY);
138     }
139   }
140
141   private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
142     ResultPoint[] oldResultPoints = result.getResultPoints();
143     ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
144     for (int i = 0; i < oldResultPoints.length; i++) {
145       ResultPoint oldPoint = oldResultPoints[i];
146       newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
147     }
148     return new Result(result.getText(), result.getRawBytes(), newResultPoints,
149         result.getBarcodeFormat());
150   }
151
152 }