package com.google.zxing;
+import com.google.zxing.oned.MultiFormatOneDReader;
import com.google.zxing.qrcode.QRCodeReader;
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 {
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.");
}
}