2 * Copyright 2007 Google Inc.
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 * <p>This implementation can detect barcodes in one of several formats within
27 * an image, and then decode what it finds. This implementation supports all
28 * barcode formats that this library supports.</p>
30 * @author srowen@google.com (Sean Owen), dswitkin@google.com (Daniel Switkin)
32 public final class MultiFormatReader implements Reader {
34 public Result decode(MonochromeBitmapSource image) throws ReaderException {
35 return decode(image, null);
38 public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
40 Vector possibleFormats = hints == null ? null : (Vector) hints.get(DecodeHintType.POSSIBLE_FORMATS);
41 Vector readers = new Vector();
42 if (possibleFormats != null) {
43 if (possibleFormats.contains(BarcodeFormat.UPC_A) ||
44 possibleFormats.contains(BarcodeFormat.UPC_E) ||
45 possibleFormats.contains(BarcodeFormat.EAN_13) ||
46 possibleFormats.contains(BarcodeFormat.EAN_8) ||
47 possibleFormats.contains(BarcodeFormat.CODE_39) ||
48 possibleFormats.contains(BarcodeFormat.CODE_128)) {
49 readers.addElement(new MultiFormatOneDReader());
51 if (possibleFormats.contains(BarcodeFormat.QR_CODE)) {
52 readers.addElement(new QRCodeReader());
55 if (readers.isEmpty()) {
56 readers.addElement(new MultiFormatOneDReader());
57 readers.addElement(new QRCodeReader());
60 for (int i = 0; i < readers.size(); i++) {
61 Reader reader = (Reader) readers.elementAt(i);
63 return reader.decode(image, hints);
64 } catch (ReaderException re) {
69 throw new ReaderException("No barcode was detected in this image.");