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.MonochromeBitmapSource;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.ResultPoint;
24 import com.google.zxing.common.CroppedMonochromeBitmapSource;
26 import java.util.Hashtable;
27 import java.util.Vector;
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>
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>
38 * <p>That is, instead of passing a {@link Reader} a caller might pass
39 * <code>new ByQuadrantReader(reader)</code>.</p>
43 public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
45 private static final int MIN_DIMENSION_TO_RECUR = 30;
47 private final Reader delegate;
49 public GenericMultipleBarcodeReader(Reader delegate) {
50 this.delegate = delegate;
53 public Result[] decodeMultiple(MonochromeBitmapSource image) throws ReaderException {
54 return decodeMultiple(image, null);
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();
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);
72 private void doDecodeMultiple(MonochromeBitmapSource image,
79 result = delegate.decode(image, hints);
80 } catch (ReaderException re) {
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())) {
94 results.addElement(translateResultPoints(result, xOffset, yOffset));
95 ResultPoint[] resultPoints = result.getResultPoints();
96 if (resultPoints == null || resultPoints.length == 0) {
99 int width = image.getWidth();
100 int height = image.getHeight();
105 for (int i = 0; i < resultPoints.length; i++) {
106 ResultPoint point = resultPoints[i];
107 float x = point.getX();
108 float y = point.getY();
123 if (minX > MIN_DIMENSION_TO_RECUR) {
124 doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, (int) minX, height),
125 hints, results, 0, 0);
127 if (minY > MIN_DIMENSION_TO_RECUR) {
128 doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, width, (int) minY),
129 hints, results, 0, 0);
131 if (maxX < width - MIN_DIMENSION_TO_RECUR) {
132 doDecodeMultiple(new CroppedMonochromeBitmapSource(image, (int) maxX, 0, width, height),
133 hints, results, (int) maxX, 0);
135 if (maxY < height - MIN_DIMENSION_TO_RECUR) {
136 doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, (int) maxY, width, height),
137 hints, results, 0, (int) maxY);
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);
148 return new Result(result.getText(), result.getRawBytes(), newResultPoints,
149 result.getBarcodeFormat());