Add Code 93 support. Update tests to reflect new (better) number of successes.
[zxing.git] / core / src / com / google / zxing / oned / MultiFormatOneDReader.java
1 /*
2  * Copyright 2008 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.oned;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.NotFoundException;
22 import com.google.zxing.Reader;
23 import com.google.zxing.ReaderException;
24 import com.google.zxing.Result;
25 import com.google.zxing.common.BitArray;
26 import com.google.zxing.oned.rss.RSS14Reader;
27 import com.google.zxing.oned.rss.expanded.RSSExpandedReader;
28
29 import java.util.Hashtable;
30 import java.util.Vector;
31
32 /**
33  * @author dswitkin@google.com (Daniel Switkin)
34  * @author Sean Owen
35  */
36 public final class MultiFormatOneDReader extends OneDReader {
37
38   private final Vector readers;
39
40   public MultiFormatOneDReader(Hashtable hints) {
41     Vector possibleFormats = hints == null ? null :
42         (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
43     boolean useCode39CheckDigit = hints != null &&
44         hints.get(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT) != null;
45     readers = new Vector();
46     if (possibleFormats != null) {
47       if (possibleFormats.contains(BarcodeFormat.EAN_13) ||
48           possibleFormats.contains(BarcodeFormat.UPC_A) ||
49           possibleFormats.contains(BarcodeFormat.EAN_8) ||
50           possibleFormats.contains(BarcodeFormat.UPC_E)) {
51         readers.addElement(new MultiFormatUPCEANReader(hints));
52       }
53       if (possibleFormats.contains(BarcodeFormat.CODE_39)) {
54         readers.addElement(new Code39Reader(useCode39CheckDigit));
55       }
56       if (possibleFormats.contains(BarcodeFormat.CODE_93)) {
57         readers.addElement(new Code93Reader());
58       }
59       if (possibleFormats.contains(BarcodeFormat.CODE_128)) {
60         readers.addElement(new Code128Reader());
61       }
62       if (possibleFormats.contains(BarcodeFormat.ITF)) {
63          readers.addElement(new ITFReader());
64       }
65       if (possibleFormats.contains(BarcodeFormat.RSS14)) {
66          readers.addElement(new RSS14Reader());
67       }
68       if (possibleFormats.contains(BarcodeFormat.RSS_EXPANDED)){
69         readers.addElement(new RSSExpandedReader());
70       }
71     }
72     if (readers.isEmpty()) {
73       readers.addElement(new MultiFormatUPCEANReader(hints));
74       readers.addElement(new Code39Reader());
75       readers.addElement(new Code93Reader());      
76       readers.addElement(new Code128Reader());
77       readers.addElement(new ITFReader());
78       readers.addElement(new RSS14Reader());      
79       readers.addElement(new RSSExpandedReader());      
80     }
81   }
82
83   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws NotFoundException {
84     int size = readers.size();
85     for (int i = 0; i < size; i++) {
86       OneDReader reader = (OneDReader) readers.elementAt(i);
87       try {
88         return reader.decodeRow(rowNumber, row, hints);
89       } catch (ReaderException re) {
90         // continue
91       }
92     }
93
94     throw NotFoundException.getNotFoundInstance();
95   }
96
97   public void reset() {
98     int size = readers.size();
99     for (int i = 0; i < size; i++) {
100       Reader reader = (Reader) readers.elementAt(i);
101       reader.reset();
102     }
103   }
104
105 }