70d4542515c465e6c6b05420d7cb5fbe8c4c9c80
[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.BinaryBitmap;
20 import com.google.zxing.NotFoundException;
21 import com.google.zxing.Reader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.ResultPoint;
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 = 100;
46
47   private final Reader delegate;
48
49   public GenericMultipleBarcodeReader(Reader delegate) {
50     this.delegate = delegate;
51   }
52
53   public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
54     return decodeMultiple(image, null);
55   }
56
57   public Result[] decodeMultiple(BinaryBitmap image, Hashtable hints)
58       throws NotFoundException {
59     Vector results = new Vector();
60     doDecodeMultiple(image, hints, results, 0, 0);
61     if (results.isEmpty()) {
62       throw NotFoundException.getNotFoundInstance();
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(BinaryBitmap 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     // Decode left of barcode
124     if (minX > MIN_DIMENSION_TO_RECUR) {
125       doDecodeMultiple(image.crop(0, 0, (int) minX, height),
126                        hints, results, xOffset, yOffset);
127     }
128     // Decode above barcode
129     if (minY > MIN_DIMENSION_TO_RECUR) {
130       doDecodeMultiple(image.crop(0, 0, width, (int) minY),
131                        hints, results, xOffset, yOffset);
132     }
133     // Decode right of barcode
134     if (maxX < width - MIN_DIMENSION_TO_RECUR) {
135       doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
136                        hints, results, xOffset + (int) maxX, yOffset);
137     }
138     // Decode below barcode
139     if (maxY < height - MIN_DIMENSION_TO_RECUR) {
140       doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
141                        hints, results, xOffset, yOffset + (int) maxY);
142     }
143   }
144
145   private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
146     ResultPoint[] oldResultPoints = result.getResultPoints();
147     ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
148     for (int i = 0; i < oldResultPoints.length; i++) {
149       ResultPoint oldPoint = oldResultPoints[i];
150       newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
151     }
152     return new Result(result.getText(), result.getRawBytes(), newResultPoints,
153         result.getBarcodeFormat());
154   }
155
156 }