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