2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.oned;
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;
25 import java.util.Hashtable;
26 import java.util.Vector;
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>
33 * @author srowen@google.com (Sean Owen)
35 public final class MultiFormatUPCEANReader extends AbstractOneDReader {
37 public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
38 Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
39 Vector readers = new Vector();
40 if (possibleFormats != null) {
41 if (possibleFormats.contains(BarcodeFormat.EAN_13)) {
42 readers.addElement(new EAN13Reader());
43 } else if (possibleFormats.contains(BarcodeFormat.UPC_A)) {
44 readers.addElement(new UPCAReader());
46 if (possibleFormats.contains(BarcodeFormat.EAN_8)) {
47 readers.addElement(new EAN8Reader());
49 if (possibleFormats.contains(BarcodeFormat.UPC_E)) {
50 readers.addElement(new UPCEReader());
53 if (readers.isEmpty()) {
54 readers.addElement(new EAN13Reader());
55 // UPC-A is covered by EAN-13
56 readers.addElement(new EAN8Reader());
57 readers.addElement(new UPCEReader());
60 // Compute this location once and reuse it on multiple implementations
61 int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row);
62 for (int i = 0; i < readers.size(); i++) {
63 UPCEANReader reader = (UPCEANReader) readers.elementAt(i);
66 result = reader.decodeRow(rowNumber, row, startGuardPattern);
67 } catch (ReaderException re) {
70 // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
71 // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
72 // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
73 // Individually these are correct and their readers will both read such a code
74 // and correctly call it EAN-13, or UPC-A, respectively.
76 // In this case, if we've been looking for both types, we'd like to call it
77 // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
78 // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
79 // result if appropriate.
80 if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) && result.getText().charAt(0) == '0') {
81 return new Result(result.getText().substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);
86 throw new ReaderException("No barcode was detected in this image.");