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.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;
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 = 100;
47 private final Reader delegate;
49 public GenericMultipleBarcodeReader(Reader delegate) {
50 this.delegate = delegate;
53 public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
54 return decodeMultiple(image, null);
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();
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(BinaryBitmap 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 // 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);
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);
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);
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);
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);
152 return new Result(result.getText(), result.getRawBytes(), newResultPoints,
153 result.getBarcodeFormat());