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.pdf417.PDF417Reader;
21 import com.google.zxing.qrcode.QRCodeReader;
22 import com.google.zxing.datamatrix.DataMatrixReader;
24 import java.util.Hashtable;
25 import java.util.Vector;
28 * MultiFormatReader is a convenience class and the main entry point into the library for most uses.
29 * By default it attempts to decode all barcode formats that the library supports. Optionally, you
30 * can provide a hints object to request different behavior, for example only decoding QR codes.
33 * @author dswitkin@google.com (Daniel Switkin)
35 public final class MultiFormatReader implements Reader {
37 private Hashtable hints;
38 private Vector readers;
41 * This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
42 * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
43 * Use setHints() followed by decodeWithState() for continuous scan applications.
45 * @param image The pixel data to decode
46 * @return The contents of the image
47 * @throws ReaderException Any errors which occurred
49 public Result decode(BinaryBitmap image) throws ReaderException {
51 return decodeInternal(image);
55 * Decode an image using the hints provided. Does not honor existing state.
57 * @param image The pixel data to decode
58 * @param hints The hints to use, clearing the previous state.
59 * @return The contents of the image
60 * @throws ReaderException Any errors which occurred
62 public Result decode(BinaryBitmap image, Hashtable hints) throws ReaderException {
64 return decodeInternal(image);
68 * Decode an image using the state set up by calling setHints() previously. Continuous scan
69 * clients will get a <b>large</b> speed increase by using this instead of decode().
71 * @param image The pixel data to decode
72 * @return The contents of the image
73 * @throws ReaderException Any errors which occurred
75 public Result decodeWithState(BinaryBitmap image) throws ReaderException {
76 // Make sure to set up the default state so we don't crash
77 if (readers == null) {
80 return decodeInternal(image);
84 * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
85 * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
86 * is important for performance in continuous scan clients.
88 * @param hints The set of hints to use for subsequent calls to decode(image)
90 public void setHints(Hashtable hints) {
93 boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
94 Vector formats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
95 readers = new Vector();
96 if (formats != null) {
97 boolean addOneDReader =
98 formats.contains(BarcodeFormat.UPC_A) ||
99 formats.contains(BarcodeFormat.UPC_E) ||
100 formats.contains(BarcodeFormat.EAN_13) ||
101 formats.contains(BarcodeFormat.EAN_8) ||
102 formats.contains(BarcodeFormat.CODE_39) ||
103 formats.contains(BarcodeFormat.CODE_128) ||
104 formats.contains(BarcodeFormat.ITF);
105 // Put 1D readers upfront in "normal" mode
106 if (addOneDReader && !tryHarder) {
107 readers.addElement(new MultiFormatOneDReader(hints));
109 if (formats.contains(BarcodeFormat.QR_CODE)) {
110 readers.addElement(new QRCodeReader());
112 if (formats.contains(BarcodeFormat.DATAMATRIX)) {
113 readers.addElement(new DataMatrixReader());
115 if (formats.contains(BarcodeFormat.PDF417)) {
116 readers.addElement(new PDF417Reader());
118 // At end in "try harder" mode
119 if (addOneDReader && tryHarder) {
120 readers.addElement(new MultiFormatOneDReader(hints));
123 if (readers.isEmpty()) {
125 readers.addElement(new MultiFormatOneDReader(hints));
127 readers.addElement(new QRCodeReader());
129 // TODO re-enable once Data Matrix is ready
130 // readers.addElement(new DataMatrixReader());
132 // TODO: Enable once PDF417 has passed QA
133 //readers.addElement(new PDF417Reader());
136 readers.addElement(new MultiFormatOneDReader(hints));
141 private Result decodeInternal(BinaryBitmap image) throws ReaderException {
142 int size = readers.size();
143 for (int i = 0; i < size; i++) {
144 Reader reader = (Reader) readers.elementAt(i);
146 return reader.decode(image, hints);
147 } catch (ReaderException re) {
152 throw ReaderException.getInstance();