2 * Copyright 2009 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.multi;
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;
25 import java.util.Hashtable;
26 import java.util.Vector;
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>
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>
37 * <p>That is, instead of passing a {@link Reader} a caller might pass
38 * <code>new ByQuadrantReader(reader)</code>.</p>
42 public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
44 private static final int MIN_DIMENSION_TO_RECUR = 30;
46 private final Reader delegate;
48 public GenericMultipleBarcodeReader(Reader delegate) {
49 this.delegate = delegate;
52 public Result[] decodeMultiple(BinaryBitmap image) throws ReaderException {
53 return decodeMultiple(image, null);
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();
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);
71 private void doDecodeMultiple(BinaryBitmap image,
78 result = delegate.decode(image, hints);
79 } catch (ReaderException re) {
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())) {
93 results.addElement(translateResultPoints(result, xOffset, yOffset));
94 ResultPoint[] resultPoints = result.getResultPoints();
95 if (resultPoints == null || resultPoints.length == 0) {
98 int width = image.getWidth();
99 int height = image.getHeight();
104 for (int i = 0; i < resultPoints.length; i++) {
105 ResultPoint point = resultPoints[i];
106 float x = point.getX();
107 float y = point.getY();
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);
126 // Decode above barcode
127 if (minY > MIN_DIMENSION_TO_RECUR) {
128 doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, 0, 0);
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);
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);
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);
149 return new Result(result.getText(), result.getRawBytes(), newResultPoints,
150 result.getBarcodeFormat());