Turn down depth of recursion
[zxing.git] / core / src / com / google / zxing / multi / GenericMultipleBarcodeReader.java
index d69e614..70d4542 100644 (file)
 
 package com.google.zxing.multi;
 
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.NotFoundException;
 import com.google.zxing.Reader;
-import com.google.zxing.Result;
-import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.ReaderException;
+import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
-import com.google.zxing.CroppedMonochromeBitmapSource;
 
 import java.util.Hashtable;
 import java.util.Vector;
@@ -31,9 +31,9 @@ import java.util.Vector;
  * After one barcode is found, the areas left, above, right and below the barcode's
  * {@link com.google.zxing.ResultPoint}s are scanned, recursively.</p>
  *
- * <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to
- * find multiple 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might
- * prevent detecting any one of them.</p>
+ * <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to find multiple
+ * 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might prevent
+ * detecting any one of them.</p>
  *
  * <p>That is, instead of passing a {@link Reader} a caller might pass
  * <code>new ByQuadrantReader(reader)</code>.</p>
@@ -42,21 +42,24 @@ import java.util.Vector;
  */
 public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
 
+  private static final int MIN_DIMENSION_TO_RECUR = 100;
+
   private final Reader delegate;
 
   public GenericMultipleBarcodeReader(Reader delegate) {
     this.delegate = delegate;
   }
 
-  public Result[] decodeMultiple(MonochromeBitmapSource image) throws ReaderException {
+  public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
     return decodeMultiple(image, null);
   }
 
-  public Result[] decodeMultiple(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
+  public Result[] decodeMultiple(BinaryBitmap image, Hashtable hints)
+      throws NotFoundException {
     Vector results = new Vector();
     doDecodeMultiple(image, hints, results, 0, 0);
     if (results.isEmpty()) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
     int numResults = results.size();
     Result[] resultArray = new Result[numResults];
@@ -66,7 +69,7 @@ public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader
     return resultArray;
   }
 
-  private void doDecodeMultiple(MonochromeBitmapSource image,
+  private void doDecodeMultiple(BinaryBitmap image,
                                 Hashtable hints,
                                 Vector results,
                                 int xOffset,
@@ -77,6 +80,17 @@ public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader
     } catch (ReaderException re) {
       return;
     }
+    boolean alreadyFound = false;
+    for (int i = 0; i < results.size(); i++) {
+      Result existingResult = (Result) results.elementAt(i);
+      if (existingResult.getText().equals(result.getText())) {
+        alreadyFound = true;
+        break;
+      }
+    }
+    if (alreadyFound) {
+      return;
+    }
     results.addElement(translateResultPoints(result, xOffset, yOffset));
     ResultPoint[] resultPoints = result.getResultPoints();
     if (resultPoints == null || resultPoints.length == 0) {
@@ -106,21 +120,25 @@ public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader
       }
     }
 
-    if (minX > 0) {
-      doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0,          0,          (int) minX - 1, height),
-                       hints, results, 0,          0);
+    // Decode left of barcode
+    if (minX > MIN_DIMENSION_TO_RECUR) {
+      doDecodeMultiple(image.crop(0, 0, (int) minX, height),
+                       hints, results, xOffset, yOffset);
     }
-    if (minY > 0) {
-      doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0,          0,          width,      (int) minY - 1),
-                       hints, results, 0,          0);
+    // Decode above barcode
+    if (minY > MIN_DIMENSION_TO_RECUR) {
+      doDecodeMultiple(image.crop(0, 0, width, (int) minY),
+                       hints, results, xOffset, yOffset);
     }
-    if (maxX < width - 1) {
-      doDecodeMultiple(new CroppedMonochromeBitmapSource(image, (int) maxX, 0,          width,      height),
-                       hints, results, (int) maxX, 0);
+    // Decode right of barcode
+    if (maxX < width - MIN_DIMENSION_TO_RECUR) {
+      doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
+                       hints, results, xOffset + (int) maxX, yOffset);
     }
-    if (maxY < height - 1) {
-      doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0,          (int) maxY, width,      height),
-                       hints, results, 0,          (int) maxY);
+    // Decode below barcode
+    if (maxY < height - MIN_DIMENSION_TO_RECUR) {
+      doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
+                       hints, results, xOffset, yOffset + (int) maxY);
     }
   }
 
@@ -131,7 +149,8 @@ public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader
       ResultPoint oldPoint = oldResultPoints[i];
       newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
     }
-    return new Result(result.getText(), result.getRawBytes(), newResultPoints, result.getBarcodeFormat());
+    return new Result(result.getText(), result.getRawBytes(), newResultPoints,
+        result.getBarcodeFormat());
   }
 
 }