Added code for the Data Matrix decoder.
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
index d1619b4..0409436 100644 (file)
 
 package com.google.zxing;
 
+import com.google.zxing.oned.MultiFormatOneDReader;
 import com.google.zxing.qrcode.QRCodeReader;
-import com.google.zxing.upc.UPCReader;
+import com.google.zxing.datamatrix.DataMatrixReader;
 
 import java.util.Hashtable;
+import java.util.Vector;
 
 /**
  * <p>This implementation can detect barcodes in one of several formats within
  * an image, and then decode what it finds. This implementation supports all
  * barcode formats that this library supports.</p>
  *
- * <p>For now, only delegates to {@link QRCodeReader}.</p>
- *
  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
  */
 public final class MultiFormatReader implements Reader {
@@ -36,41 +36,42 @@ public final class MultiFormatReader implements Reader {
     return decode(image, null);
   }
 
-  public Result decode(MonochromeBitmapSource image, Hashtable hints)
-      throws ReaderException {
-    Hashtable possibleFormats =
-        hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
-    boolean tryUPC = false;
-    boolean tryQR = false;
-    
-    if (possibleFormats == null) {
-      tryUPC = true;
-      tryQR = true;
-    } else if (possibleFormats.contains(BarcodeFormat.UPC)) {
-      tryUPC = true;
-    } else if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
-      tryQR = true;
-    } else {
-      throw new ReaderException();
-    }
-    
-    // UPC is much faster to decode, so try it first.
-    if (tryUPC) {
-      try {
-        return new UPCReader().decode(image, hints);
-      } catch (ReaderException e) {
+  public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
+
+    Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
+    Vector readers = new Vector();
+    if (possibleFormats != null) {
+      if (possibleFormats.contains(BarcodeFormat.UPC_A) ||
+          possibleFormats.contains(BarcodeFormat.UPC_E) ||
+          possibleFormats.contains(BarcodeFormat.EAN_13) ||
+          possibleFormats.contains(BarcodeFormat.EAN_8) ||
+          possibleFormats.contains(BarcodeFormat.CODE_39) ||
+          possibleFormats.contains(BarcodeFormat.CODE_128)) {
+        readers.addElement(new MultiFormatOneDReader());
+      }
+      if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
+        readers.addElement(new QRCodeReader());
       }
+      if (possibleFormats.contains(BarcodeFormat.DATAMATRIX)) {
+        readers.addElement(new DataMatrixReader());
+      }
+    }
+    if (readers.isEmpty()) {
+      readers.addElement(new MultiFormatOneDReader());
+      readers.addElement(new QRCodeReader());
+      readers.addElement(new DataMatrixReader());
     }
-    
-    // Then fall through to QR codes.
-    if (tryQR) {
+
+    for (int i = 0; i < readers.size(); i++) {
+      Reader reader = (Reader) readers.elementAt(i);
       try {
-        return new QRCodeReader().decode(image, hints);
-      } catch (ReaderException e) {
+        return reader.decode(image, hints);
+      } catch (ReaderException re) {
+        // continue
       }
     }
-    
-    throw new ReaderException();
+
+    throw new ReaderException("No barcode was detected in this image.");
   }
 
 }