Added code for the Data Matrix decoder.
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
1 /*
2  * Copyright 2007 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;
18
19 import com.google.zxing.oned.MultiFormatOneDReader;
20 import com.google.zxing.qrcode.QRCodeReader;
21 import com.google.zxing.datamatrix.DataMatrixReader;
22
23 import java.util.Hashtable;
24 import java.util.Vector;
25
26 /**
27  * <p>This implementation can detect barcodes in one of several formats within
28  * an image, and then decode what it finds. This implementation supports all
29  * barcode formats that this library supports.</p>
30  *
31  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
32  */
33 public final class MultiFormatReader implements Reader {
34
35   public Result decode(MonochromeBitmapSource image) throws ReaderException {
36     return decode(image, null);
37   }
38
39   public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
40
41     Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
42     Vector readers = new Vector();
43     if (possibleFormats != null) {
44       if (possibleFormats.contains(BarcodeFormat.UPC_A) ||
45           possibleFormats.contains(BarcodeFormat.UPC_E) ||
46           possibleFormats.contains(BarcodeFormat.EAN_13) ||
47           possibleFormats.contains(BarcodeFormat.EAN_8) ||
48           possibleFormats.contains(BarcodeFormat.CODE_39) ||
49           possibleFormats.contains(BarcodeFormat.CODE_128)) {
50         readers.addElement(new MultiFormatOneDReader());
51       }
52       if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
53         readers.addElement(new QRCodeReader());
54       }
55       if (possibleFormats.contains(BarcodeFormat.DATAMATRIX)) {
56         readers.addElement(new DataMatrixReader());
57       }
58     }
59     if (readers.isEmpty()) {
60       readers.addElement(new MultiFormatOneDReader());
61       readers.addElement(new QRCodeReader());
62       readers.addElement(new DataMatrixReader());
63     }
64
65     for (int i = 0; i < readers.size(); i++) {
66       Reader reader = (Reader) readers.elementAt(i);
67       try {
68         return reader.decode(image, hints);
69       } catch (ReaderException re) {
70         // continue
71       }
72     }
73
74     throw new ReaderException("No barcode was detected in this image.");
75   }
76
77 }