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