Code tweaks and so forth with Daniel
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing;
18
19 import com.google.zxing.qrcode.QRCodeReader;
20 import com.google.zxing.upc.UPCReader;
21
22 import java.util.Hashtable;
23
24 /**
25  * <p>This implementation can detect barcodes in one of several formats within
26  * an image, and then decode what it finds. This implementation supports all
27  * barcode formats that this library supports.</p>
28  *
29  * <p>For now, only delegates to {@link QRCodeReader}.</p>
30  *
31  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
32  */
33 public final class MultiFormatReader implements Reader {
34
35   public Result decode(MonochromeBitmapSource image) throws ReaderException {
36     return decode(image, null);
37   }
38
39   public Result decode(MonochromeBitmapSource image, Hashtable hints)
40       throws ReaderException {
41     Hashtable possibleFormats = hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
42     boolean tryUPC = false;
43     boolean tryQR = false;
44     
45     if (possibleFormats == null) {
46       tryUPC = true;
47       tryQR = true;
48     } else if (possibleFormats.contains(BarcodeFormat.UPC)) {
49       tryUPC = true;
50     } else if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
51       tryQR = true;
52     } else {
53       throw new ReaderException("POSSIBLE_FORMATS specifies no supported types");
54     }
55     
56     // UPC is much faster to decode, so try it first.
57     if (tryUPC) {
58       try {
59         return new UPCReader().decode(image, hints);
60       } catch (ReaderException e) {
61       }
62     }
63     
64     // Then fall through to QR codes.
65     if (tryQR) {
66       try {
67         return new QRCodeReader().decode(image, hints);
68       } catch (ReaderException e) {
69       }
70     }
71     
72     throw new ReaderException("Could not locate and decode a barcode in the image");
73   }
74
75 }