Added code for the Data Matrix decoder.
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
index 09c66fa..0409436 100644 (file)
@@ -18,8 +18,10 @@ package com.google.zxing;
 
 import com.google.zxing.oned.MultiFormatOneDReader;
 import com.google.zxing.qrcode.QRCodeReader;
+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
@@ -34,39 +36,41 @@ 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);
+  public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
 
-    boolean tryOneD;
-    boolean tryQR;
-    if (possibleFormats == null) {
-      tryOneD = true;
-      tryQR = true;
-    } else {
-      tryOneD = possibleFormats.contains(BarcodeFormat.ONED);
-      tryQR = possibleFormats.contains(BarcodeFormat.QR_CODE);
+    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 (!(tryOneD || tryQR)) {
-      throw new ReaderException("POSSIBLE_FORMATS specifies no supported types");
+    if (readers.isEmpty()) {
+      readers.addElement(new MultiFormatOneDReader());
+      readers.addElement(new QRCodeReader());
+      readers.addElement(new DataMatrixReader());
     }
 
-    // UPC is much faster to decode, so try it first.
-    if (tryOneD) {
-      try {
-        return new MultiFormatOneDReader().decode(image, hints);
-      } catch (ReaderException re) {
-      }
-    }
-    
-    // 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);
+        return reader.decode(image, hints);
       } catch (ReaderException re) {
+        // continue
       }
     }
-    
+
     throw new ReaderException("No barcode was detected in this image.");
   }