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