Use TRY_HARDER hint in javase CommandLineRunner. TRY_HARDER now tries rotating the...
[zxing.git] / core / src / com / google / zxing / oned / MultiFormatUPCEANReader.java
1 /*
2  * Copyright 2008 Google Inc.
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 srowen@google.com (Sean Owen)
34  */
35 public final class MultiFormatUPCEANReader extends AbstractOneDReader {
36
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       }
44       if (possibleFormats.contains(BarcodeFormat.UPC_A)) {
45         readers.addElement(new UPCAReader());
46       }
47       if (possibleFormats.contains(BarcodeFormat.EAN_8)) {
48         readers.addElement(new EAN8Reader());
49       }
50       if (possibleFormats.contains(BarcodeFormat.UPC_E)) {
51         readers.addElement(new UPCEReader());
52       }
53     }
54     if (readers.isEmpty()) {
55       readers.addElement(new EAN13Reader());      
56       readers.addElement(new UPCAReader());
57       readers.addElement(new EAN8Reader());
58       readers.addElement(new UPCEReader());
59     }
60
61     // Compute this location once and reuse it on multiple implementations
62     int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row);
63     for (int i = 0; i < readers.size(); i++) {
64       UPCEANReader reader = (UPCEANReader) readers.elementAt(i);
65       Result result;
66       try {
67         result = reader.decodeRow(rowNumber, row, startGuardPattern);
68       } catch (ReaderException re) {
69         continue;
70       }
71       // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
72       // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
73       // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
74       // Individually these are correct.
75       //
76       // These cases can't be distinguished, so we defer to the UPC-A decoder and
77       // treat this case as a UPC-A code. But if we let the UPC-A decoder look at
78       // symbols first, it will recognize any EAN-13 code as a 12-digit string,
79       // which is wrong. So EAN-13 has to try first.
80       //
81       // Here is, therefore, where we implement this logic:
82       if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) &&
83           result.getText().charAt(0) == '0') {
84         return new Result(result.getText().substring(1), result.getResultPoints(), BarcodeFormat.UPC_A);
85       }
86       return result;
87     }
88
89     throw new ReaderException("No barcode was detected in this image.");
90   }
91
92 }