2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing;
19 import com.google.zxing.oned.MultiFormatOneDReader;
20 import com.google.zxing.qrcode.QRCodeReader;
22 import java.util.Hashtable;
23 import java.util.Vector;
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.
30 * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
32 public final class MultiFormatReader implements Reader {
34 private Hashtable hints;
35 private Vector readers;
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.
42 * @param image The pixel data to decode
43 * @return The contents of the image
44 * @throws ReaderException Any errors which occurred
46 public Result decode(MonochromeBitmapSource image) throws ReaderException {
48 return decodeInternal(image);
52 * Decode an image using the hints provided. Does not honor existing state.
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
59 public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
61 return decodeInternal(image);
65 * Decode an image using the state set up by calling setHints() previously. Continuous scan
66 * clients will get a <b>large</b> speed increase by using this instead of decode().
68 * @param image The pixel data to decode
69 * @return The contents of the image
70 * @throws ReaderException Any errors which occurred
72 public Result decodeWithState(MonochromeBitmapSource image) throws ReaderException {
73 // Make sure to set up the default state so we don't crash
74 if (readers == null) {
77 return decodeInternal(image);
81 * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
82 * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
83 * is important for performance in continuous scan clients.
85 * @param hints The set of hints to use for subsequent calls to decode(image)
87 public void setHints(Hashtable hints) {
90 boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
91 Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
92 readers = new Vector();
93 if (possibleFormats != null) {
94 boolean addOneDReader =
95 possibleFormats.contains(BarcodeFormat.UPC_A) ||
96 possibleFormats.contains(BarcodeFormat.UPC_E) ||
97 possibleFormats.contains(BarcodeFormat.EAN_13) ||
98 possibleFormats.contains(BarcodeFormat.EAN_8) ||
99 possibleFormats.contains(BarcodeFormat.CODE_39) ||
100 possibleFormats.contains(BarcodeFormat.CODE_128);
101 // Put 1D readers upfront in "normal" mode
102 if (addOneDReader && !tryHarder) {
103 readers.addElement(new MultiFormatOneDReader(hints));
105 if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
106 readers.addElement(new QRCodeReader());
108 // TODO re-enable once Data Matrix is ready
109 //if (possibleFormats.contains(BarcodeFormat.DATAMATRIX)) {
110 // readers.addElement(new DataMatrixReader());
112 // At end in "try harder" mode
113 if (addOneDReader && tryHarder) {
114 readers.addElement(new MultiFormatOneDReader(hints));
117 if (readers.isEmpty()) {
119 readers.addElement(new MultiFormatOneDReader(hints));
121 readers.addElement(new QRCodeReader());
122 // TODO re-enable once Data Matrix is ready
123 // readers.addElement(new DataMatrixReader());
125 readers.addElement(new MultiFormatOneDReader(hints));
130 private Result decodeInternal(MonochromeBitmapSource image) throws ReaderException {
131 int size = readers.size();
132 for (int i = 0; i < size; i++) {
133 Reader reader = (Reader) readers.elementAt(i);
135 return reader.decode(image, hints);
136 } catch (ReaderException re) {
141 throw new ReaderException("No barcode was detected in this image.");