d1619b4c10882ef682293c0e5fff80cc879d8e9f
[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 =
42         hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
43     boolean tryUPC = false;
44     boolean tryQR = false;
45     
46     if (possibleFormats == null) {
47       tryUPC = true;
48       tryQR = true;
49     } else if (possibleFormats.contains(BarcodeFormat.UPC)) {
50       tryUPC = true;
51     } else if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
52       tryQR = true;
53     } else {
54       throw new ReaderException();
55     }
56     
57     // UPC is much faster to decode, so try it first.
58     if (tryUPC) {
59       try {
60         return new UPCReader().decode(image, hints);
61       } catch (ReaderException e) {
62       }
63     }
64     
65     // Then fall through to QR codes.
66     if (tryQR) {
67       try {
68         return new QRCodeReader().decode(image, hints);
69       } catch (ReaderException e) {
70       }
71     }
72     
73     throw new ReaderException();
74   }
75
76 }