Modified the library entry point in a way which lets continuous scan clients reuse...
[zxing.git] / core / src / com / google / zxing / MultiFormatReader.java
1 /*
2  * Copyright 2007 ZXing authors
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 import java.util.Vector;
24
25 /**
26  * MultiFormatReader is a convenience class and the main entry point into the library for most uses.
27  * By default it attempts to decode all barcode formats that the library supports. Optionally, you
28  * can provide a hints object to request different behavior, for example only decoding QR codes.
29  *
30  * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
31  */
32 public final class MultiFormatReader implements Reader {
33
34   private Hashtable hints;
35   private Vector readers;
36
37   /**
38    * This version of decode honors the intent of Reader.decode(MonochromeBitmapSource) in that it
39    * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
40    * Use setHints() followed by decodeWithState() for continuous scan applications.
41    *
42    * @param image The pixel data to decode
43    * @return The contents of the image
44    * @throws ReaderException Any errors which occurred
45    */
46   public Result decode(MonochromeBitmapSource image) throws ReaderException {
47     setHints(null);
48     return decodeInternal(image);
49   }
50
51   /**
52    * Decode an image using the hints provided. Does not honor existing state.
53    *
54    * @param image The pixel data to decode
55    * @param hints The hints to use, clearing the previous state.
56    * @return The contents of the image
57    * @throws ReaderException Any errors which occurred
58    */
59   public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
60     setHints(hints);
61     return decodeInternal(image);
62   }
63
64   /**
65    * Decode an image using the state set up by calling setHints() previously.
66    *
67    * @param image The pixel data to decode
68    * @return The contents of the image
69    * @throws ReaderException Any errors which occurred
70    */
71   public Result decodeWithState(MonochromeBitmapSource image) throws ReaderException {
72     // Make sure to set up the default state so we don't crash
73     if (readers == null) {
74       setHints(null);
75     }
76     return decodeInternal(image);
77   }
78
79   /**
80    * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
81    * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
82    * is important for performance in continuous scan clients.
83    *
84    * @param hints The set of hints to use for subsequent calls to decode(image)
85    */
86   public void setHints(Hashtable hints) {
87     this.hints = hints;
88
89     boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
90     Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
91     readers = new Vector();
92     if (possibleFormats != null) {
93       boolean addOneDReader =
94           possibleFormats.contains(BarcodeFormat.UPC_A) ||
95               possibleFormats.contains(BarcodeFormat.UPC_E) ||
96               possibleFormats.contains(BarcodeFormat.EAN_13) ||
97               possibleFormats.contains(BarcodeFormat.EAN_8) ||
98               possibleFormats.contains(BarcodeFormat.CODE_39) ||
99               possibleFormats.contains(BarcodeFormat.CODE_128);
100       // Put 1D readers upfront in "normal" mode
101       if (addOneDReader && !tryHarder) {
102         readers.addElement(new MultiFormatOneDReader());
103       }
104       if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
105         readers.addElement(new QRCodeReader());
106       }
107       // TODO re-enable once Data Matrix is ready
108       //if (possibleFormats.contains(BarcodeFormat.DATAMATRIX)) {
109       //  readers.addElement(new DataMatrixReader());
110       //}
111       // At end in "try harder" mode
112       if (addOneDReader && tryHarder) {
113         readers.addElement(new MultiFormatOneDReader());
114       }
115     }
116     if (readers.isEmpty()) {
117       if (!tryHarder) {
118         readers.addElement(new MultiFormatOneDReader());
119       }
120       readers.addElement(new QRCodeReader());
121       // TODO re-enable once Data Matrix is ready
122       // readers.addElement(new DataMatrixReader());
123       if (tryHarder) {
124         readers.addElement(new MultiFormatOneDReader());
125       }
126     }
127   }
128
129   private Result decodeInternal(MonochromeBitmapSource image) throws ReaderException {
130     for (int i = 0; i < readers.size(); i++) {
131       Reader reader = (Reader) readers.elementAt(i);
132       try {
133         return reader.decode(image, hints);
134       } catch (ReaderException re) {
135         // continue
136       }
137     }
138
139     throw new ReaderException("No barcode was detected in this image.");
140   }
141
142 }