2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 using MultiFormatOneDReader = com.google.zxing.oned.MultiFormatOneDReader;
\r
18 using PDF417Reader = com.google.zxing.pdf417.PDF417Reader;
\r
19 using QRCodeReader = com.google.zxing.qrcode.QRCodeReader;
\r
20 using DataMatrixReader = com.google.zxing.datamatrix.DataMatrixReader;
\r
21 namespace com.google.zxing
\r
24 /// <summary> MultiFormatReader is a convenience class and the main entry point into the library for most uses.
\r
25 /// By default it attempts to decode all barcode formats that the library supports. Optionally, you
\r
26 /// can provide a hints object to request different behavior, for example only decoding QR codes.
\r
29 /// <author> Sean Owen
\r
31 /// <author> dswitkin@google.com (Daniel Switkin)
\r
33 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
35 public sealed class MultiFormatReader : Reader
\r
37 /// <summary> This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
\r
38 /// to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
\r
39 /// is important for performance in continuous scan clients.
\r
42 /// <param name="hints">The set of hints to use for subsequent calls to decode(image)
\r
44 public System.Collections.Hashtable Hints
\r
50 bool tryHarder = value != null && value.ContainsKey(DecodeHintType.TRY_HARDER);
\r
51 System.Collections.ArrayList formats = value == null?null:(System.Collections.ArrayList) value[DecodeHintType.POSSIBLE_FORMATS];
\r
52 readers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
\r
53 if (formats != null)
\r
55 bool addOneDReader = formats.Contains(BarcodeFormat.UPC_A) || formats.Contains(BarcodeFormat.UPC_E) || formats.Contains(BarcodeFormat.EAN_13) || formats.Contains(BarcodeFormat.EAN_8) || formats.Contains(BarcodeFormat.CODE_39) || formats.Contains(BarcodeFormat.CODE_128) || formats.Contains(BarcodeFormat.ITF);
\r
56 // Put 1D readers upfront in "normal" mode
\r
57 if (addOneDReader && !tryHarder)
\r
59 readers.Add(new MultiFormatOneDReader(value));
\r
61 if (formats.Contains(BarcodeFormat.QR_CODE))
\r
63 readers.Add(new QRCodeReader());
\r
65 if (formats.Contains(BarcodeFormat.DATAMATRIX))
\r
67 readers.Add(new DataMatrixReader());
\r
69 if (formats.Contains(BarcodeFormat.PDF417))
\r
71 readers.Add(new PDF417Reader());
\r
73 // At end in "try harder" mode
\r
74 if (addOneDReader && tryHarder)
\r
76 readers.Add(new MultiFormatOneDReader(value));
\r
79 if ((readers.Count == 0))
\r
83 readers.Add(new MultiFormatOneDReader(value));
\r
85 readers.Add(new QRCodeReader());
\r
87 // TODO re-enable once Data Matrix is ready
\r
88 // readers.addElement(new DataMatrixReader());
\r
90 // TODO: Enable once PDF417 has passed QA
\r
91 //readers.addElement(new PDF417Reader());
\r
95 readers.Add(new MultiFormatOneDReader(value));
\r
102 private System.Collections.Hashtable hints;
\r
103 private System.Collections.ArrayList readers;
\r
105 /// <summary> This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
\r
106 /// passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
\r
107 /// Use setHints() followed by decodeWithState() for continuous scan applications.
\r
110 /// <param name="image">The pixel data to decode
\r
112 /// <returns> The contents of the image
\r
114 /// <throws> ReaderException Any errors which occurred </throws>
\r
115 public Result decode(BinaryBitmap image)
\r
118 return decodeInternal(image);
\r
121 /// <summary> Decode an image using the hints provided. Does not honor existing state.
\r
124 /// <param name="image">The pixel data to decode
\r
126 /// <param name="hints">The hints to use, clearing the previous state.
\r
128 /// <returns> The contents of the image
\r
130 /// <throws> ReaderException Any errors which occurred </throws>
\r
131 public Result decode(BinaryBitmap image, System.Collections.Hashtable hints)
\r
134 return decodeInternal(image);
\r
137 /// <summary> Decode an image using the state set up by calling setHints() previously. Continuous scan
\r
138 /// clients will get a <b>large</b> speed increase by using this instead of decode().
\r
141 /// <param name="image">The pixel data to decode
\r
143 /// <returns> The contents of the image
\r
145 /// <throws> ReaderException Any errors which occurred </throws>
\r
146 public Result decodeWithState(BinaryBitmap image)
\r
148 // Make sure to set up the default state so we don't crash
\r
149 if (readers == null)
\r
153 return decodeInternal(image);
\r
156 private Result decodeInternal(BinaryBitmap image)
\r
158 int size = readers.Count;
\r
159 for (int i = 0; i < size; i++)
\r
161 Reader reader = (Reader) readers[i];
\r
164 return reader.decode(image, hints);
\r
166 catch (ReaderException re)
\r
172 throw ReaderException.Instance;
\r