Major refactoring of 1D barcode code. Moved into com.google.zxing.oned package. Misc...
[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
22 import java.util.Hashtable;
23
24 /**
25  * <p>This implementation can detect barcodes in one of several formats within
26  * an image, and then decode what it finds. This implementation supports all
27  * barcode formats that this library supports.</p>
28  *
29  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
30  */
31 public final class MultiFormatReader implements Reader {
32
33   public Result decode(MonochromeBitmapSource image) throws ReaderException {
34     return decode(image, null);
35   }
36
37   public Result decode(MonochromeBitmapSource image, Hashtable hints)
38       throws ReaderException {
39     Hashtable possibleFormats = hints == null ? null : (Hashtable) hints.get(DecodeHintType.POSSIBLE_FORMATS);
40
41     boolean tryOneD;
42     boolean tryQR;
43     if (possibleFormats == null) {
44       tryOneD = true;
45       tryQR = true;
46     } else {
47       tryOneD = possibleFormats.contains(BarcodeFormat.ONED);
48       tryQR = possibleFormats.contains(BarcodeFormat.QR_CODE);
49     }
50     if (!(tryOneD || tryQR)) {
51       throw new ReaderException("POSSIBLE_FORMATS specifies no supported types");
52     }
53
54     // UPC is much faster to decode, so try it first.
55     if (tryOneD) {
56       try {
57         return new MultiFormatOneDReader().decode(image, hints);
58       } catch (ReaderException re) {
59       }
60     }
61     
62     // Then fall through to QR codes.
63     if (tryQR) {
64       try {
65         return new QRCodeReader().decode(image, hints);
66       } catch (ReaderException re) {
67       }
68     }
69     
70     throw new ReaderException("No barcode was detected in this image.");
71   }
72
73 }