Code tweaks and so forth with Daniel
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
index d19cc94..a4d21ed 100644 (file)
 package com.google.zxing;
 
 import com.google.zxing.qrcode.QRCodeReader;
+import com.google.zxing.upc.UPCReader;
 
 import java.util.Hashtable;
 
 /**
- * For now, only delegates to {@link QRCodeReader}.
+ * <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)
  */
@@ -33,13 +38,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);
-    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 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();
+      throw new ReaderException("POSSIBLE_FORMATS specifies no supported types");
+    }
+    
+    // UPC is much faster to decode, so try it first.
+    if (tryUPC) {
+      try {
+        return new UPCReader().decode(image, hints);
+      } catch (ReaderException e) {
+      }
+    }
+    
+    // Then fall through to QR codes.
+    if (tryQR) {
+      try {
+        return new QRCodeReader().decode(image, hints);
+      } catch (ReaderException e) {
+      }
     }
+    
+    throw new ReaderException("Could not locate and decode a barcode in the image");
   }
 
 }