Improved datamatrix reader with new algorithm
[zxing.git] / core / src / com / google / zxing / multi / GenericMultipleBarcodeReader.java
index 9468691..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.common.CroppedMonochromeBitmapSource;
 
 import java.util.Hashtable;
 import java.util.Vector;
@@ -42,22 +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];
@@ -67,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,
@@ -78,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) {
@@ -107,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);
     }
   }