Preliminary RSS-14 support. Not enabled yet in Android client.
[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
28 import java.util.Hashtable;
29 import java.util.Vector;
30
31 /**
32  * @author dswitkin@google.com (Daniel Switkin)
33  * @author Sean Owen
34  */
35 public final class MultiFormatOneDReader extends OneDReader {
36
37   private final Vector readers;
38
39   public MultiFormatOneDReader(Hashtable hints) {
40     Vector possibleFormats = hints == null ? null :
41         (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
42     boolean useCode39CheckDigit = hints != null &&
43         hints.get(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT) != null;
44     readers = new Vector();
45     if (possibleFormats != null) {
46       if (possibleFormats.contains(BarcodeFormat.EAN_13) ||
47           possibleFormats.contains(BarcodeFormat.UPC_A) ||
48           possibleFormats.contains(BarcodeFormat.EAN_8) ||
49           possibleFormats.contains(BarcodeFormat.UPC_E)) {
50         readers.addElement(new MultiFormatUPCEANReader(hints));
51       }
52       if (possibleFormats.contains(BarcodeFormat.CODE_39)) {
53         readers.addElement(new Code39Reader(useCode39CheckDigit));
54       }
55       if (possibleFormats.contains(BarcodeFormat.CODE_128)) {
56         readers.addElement(new Code128Reader());
57       }
58       if (possibleFormats.contains(BarcodeFormat.ITF)) {
59          readers.addElement(new ITFReader());
60       }
61       if (possibleFormats.contains(BarcodeFormat.RSS14)) {
62          readers.addElement(new RSS14Reader());
63       }
64     }
65     if (readers.isEmpty()) {
66       readers.addElement(new MultiFormatUPCEANReader(hints));
67       readers.addElement(new Code39Reader());
68       readers.addElement(new Code128Reader());
69       readers.addElement(new ITFReader());
70       readers.addElement(new RSS14Reader());      
71     }
72   }
73
74   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws NotFoundException {
75     int size = readers.size();
76     for (int i = 0; i < size; i++) {
77       OneDReader reader = (OneDReader) readers.elementAt(i);
78       try {
79         return reader.decodeRow(rowNumber, row, hints);
80       } catch (ReaderException re) {
81         // continue
82       }
83     }
84
85     throw NotFoundException.getNotFoundInstance();
86   }
87
88   public void reset() {
89     int size = readers.size();
90     for (int i = 0; i < size; i++) {
91       Reader reader = (Reader) readers.elementAt(i);
92       reader.reset();
93     }
94   }
95
96 }