096374556fc468b68ecef118b697e072063c5fc3
[zxing.git] / core / src / com / google / zxing / oned / MultiFormatOneDReader.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.DecodeHintType;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.Result;
22 import com.google.zxing.BarcodeFormat;
23 import com.google.zxing.common.BitArray;
24
25 import java.util.Hashtable;
26 import java.util.Vector;
27
28 /**
29  * @author dswitkin@google.com (Daniel Switkin)
30  * @author srowen@google.com (Sean Owen)
31  */
32 public final class MultiFormatOneDReader extends AbstractOneDReader {
33
34   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
35
36     Hashtable possibleFormats = hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
37     Vector readers = new Vector();
38     if (possibleFormats != null) {
39       if (possibleFormats.contains(BarcodeFormat.EAN_13) ||
40           possibleFormats.contains(BarcodeFormat.UPC_A) ||
41           possibleFormats.contains(BarcodeFormat.EAN_8) ||
42           possibleFormats.contains(BarcodeFormat.UPC_E)) {
43         readers.addElement(new MultiFormatUPCEANReader());
44       }
45       if (possibleFormats.contains(BarcodeFormat.CODE_39)) {
46         readers.addElement(new Code39Reader());
47       }
48       if (possibleFormats.contains(BarcodeFormat.CODE_128)) {
49         readers.addElement(new Code128Reader());
50       }
51     }
52     if (readers.isEmpty()) {
53       readers.addElement(new MultiFormatUPCEANReader());
54       readers.addElement(new Code39Reader());
55       readers.addElement(new Code128Reader());
56     }
57
58     for (int i = 0; i < readers.size(); i++) {
59       OneDReader reader = (OneDReader) readers.elementAt(i);
60       try {
61         return reader.decodeRow(rowNumber, row, hints);
62       } catch (ReaderException re) {
63         // continue
64       }
65     }
66
67     throw new ReaderException("No barcode was detected in this image.");
68   }
69
70 }