Removed three interfaces which weren't doing any good and were making the 1D class...
[zxing.git] / core / src / com / google / zxing / oned / MultiFormatUPCEANReader.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.ReaderException;
22 import com.google.zxing.Result;
23 import com.google.zxing.common.BitArray;
24
25 import java.util.Hashtable;
26 import java.util.Vector;
27
28 /**
29  * <p>A reader that can read all available UPC/EAN formats. If a caller wants to try to
30  * read all such formats, it is most efficient to use this implementation rather than invoke
31  * individual readers.</p>
32  *
33  * @author Sean Owen
34  */
35 public final class MultiFormatUPCEANReader extends OneDReader {
36
37   private final Vector readers;
38
39   public MultiFormatUPCEANReader(Hashtable hints) {
40     Vector possibleFormats = hints == null ? null :
41         (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
42     readers = new Vector();
43     if (possibleFormats != null) {
44       if (possibleFormats.contains(BarcodeFormat.EAN_13)) {
45         readers.addElement(new EAN13Reader());
46       } else if (possibleFormats.contains(BarcodeFormat.UPC_A)) {
47         readers.addElement(new UPCAReader());
48       }
49       if (possibleFormats.contains(BarcodeFormat.EAN_8)) {
50         readers.addElement(new EAN8Reader());
51       }
52       if (possibleFormats.contains(BarcodeFormat.UPC_E)) {
53         readers.addElement(new UPCEReader());
54       }
55     }
56     if (readers.isEmpty()) {
57       readers.addElement(new EAN13Reader());
58       // UPC-A is covered by EAN-13
59       readers.addElement(new EAN8Reader());
60       readers.addElement(new UPCEReader());
61     }
62   }
63
64   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
65     // Compute this location once and reuse it on multiple implementations
66     int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
67     int size = readers.size();
68     for (int i = 0; i < size; i++) {
69       UPCEANReader reader = (UPCEANReader) readers.elementAt(i);
70       Result result;
71       try {
72         result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
73       } catch (ReaderException re) {
74         continue;
75       }
76       // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
77       // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
78       // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
79       // Individually these are correct and their readers will both read such a code
80       // and correctly call it EAN-13, or UPC-A, respectively.
81       //
82       // In this case, if we've been looking for both types, we'd like to call it
83       // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
84       // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
85       // result if appropriate.
86       if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) &&
87           result.getText().charAt(0) == '0') {
88         return new Result(result.getText().substring(1), null, result.getResultPoints(),
89             BarcodeFormat.UPC_A);
90       }
91       return result;
92     }
93
94     throw ReaderException.getInstance();
95   }
96
97 }