Major refactoring of 1D barcode code. Moved into com.google.zxing.oned package. Misc...
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
index 1f78c86..09c66fa 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.google.zxing;
 
+import com.google.zxing.oned.MultiFormatOneDReader;
 import com.google.zxing.qrcode.QRCodeReader;
 
 import java.util.Hashtable;
@@ -25,8 +26,6 @@ import java.util.Hashtable;
  * 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 {
@@ -37,15 +36,38 @@ public final class MultiFormatReader implements Reader {
 
   public Result decode(MonochromeBitmapSource image, Hashtable hints)
       throws ReaderException {
-    Hashtable possibleFormats =
-        hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
-    // TODO for now we are only support QR Code so this behaves accordingly. This needs to
-    // become more sophisticated
-    if (possibleFormats == null || possibleFormats.contains(BarcodeFormat.QR_CODE)) {
-      return new QRCodeReader().decode(image, hints);
+    Hashtable possibleFormats = hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
+
+    boolean tryOneD;
+    boolean tryQR;
+    if (possibleFormats == null) {
+      tryOneD = true;
+      tryQR = true;
     } else {
-      throw new ReaderException();
+      tryOneD = possibleFormats.contains(BarcodeFormat.ONED);
+      tryQR = possibleFormats.contains(BarcodeFormat.QR_CODE);
+    }
+    if (!(tryOneD || tryQR)) {
+      throw new ReaderException("POSSIBLE_FORMATS specifies no supported types");
+    }
+
+    // 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) {
+      try {
+        return new QRCodeReader().decode(image, hints);
+      } catch (ReaderException re) {
+      }
     }
+    
+    throw new ReaderException("No barcode was detected in this image.");
   }
 
 }