Add iCal support, plus many small changes suggested by code inspection -- mostly...
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
1 /*
2  * Copyright 2007 ZXing authors
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.oned.MultiFormatOneDReader;
20 import com.google.zxing.qrcode.QRCodeReader;
21
22 import java.util.Hashtable;
23 import java.util.Vector;
24
25 /**
26  * <p>This implementation can detect barcodes in one of several formats within
27  * an image, and then decode what it finds. This implementation supports all
28  * barcode formats that this library supports.</p>
29  *
30  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
31  */
32 public final class MultiFormatReader implements Reader {
33
34   public Result decode(MonochromeBitmapSource image) throws ReaderException {
35     return decode(image, null);
36   }
37
38   public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
39
40     boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
41     Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
42     Vector readers = new Vector();
43     if (possibleFormats != null) {
44       boolean addOneDReader =
45           possibleFormats.contains(BarcodeFormat.UPC_A) ||
46           possibleFormats.contains(BarcodeFormat.UPC_E) ||
47           possibleFormats.contains(BarcodeFormat.EAN_13) ||
48           possibleFormats.contains(BarcodeFormat.EAN_8) ||
49           possibleFormats.contains(BarcodeFormat.CODE_39) ||
50           possibleFormats.contains(BarcodeFormat.CODE_128);
51       // Put 1D readers upfront in "normal" mode
52       if (addOneDReader && !tryHarder) {
53         readers.addElement(new MultiFormatOneDReader());
54       }
55       if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
56         readers.addElement(new QRCodeReader());
57       }
58       // TODO re-enable once Data Matrix is ready
59       //if (possibleFormats.contains(BarcodeFormat.DATAMATRIX)) {
60       //  readers.addElement(new DataMatrixReader());
61       //}
62       // At end in "try harder" mode
63       if (addOneDReader && tryHarder) {
64         readers.addElement(new MultiFormatOneDReader());
65       }
66     }
67     if (readers.isEmpty()) {
68       if (!tryHarder) {
69         readers.addElement(new MultiFormatOneDReader());
70       }
71       readers.addElement(new QRCodeReader());
72       // TODO re-enable once Data Matrix is ready
73       // readers.addElement(new DataMatrixReader());
74       if (tryHarder) {
75         readers.addElement(new MultiFormatOneDReader());
76       }
77     }
78
79     for (int i = 0; i < readers.size(); i++) {
80       Reader reader = (Reader) readers.elementAt(i);
81       try {
82         return reader.decode(image, hints);
83       } catch (ReaderException re) {
84         // continue
85       }
86     }
87
88     throw new ReaderException("No barcode was detected in this image.");
89   }
90
91 }