2 * Licensed under the Apache License, Version 2.0 (the "License");
\r
3 * you may not use this file except in compliance with the License.
\r
4 * You may obtain a copy of the License at
\r
6 * http://www.apache.org/licenses/LICENSE-2.0
\r
8 * Unless required by applicable law or agreed to in writing, software
\r
9 * distributed under the License is distributed on an "AS IS" BASIS,
\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
11 * See the License for the specific language governing permissions and
\r
12 * limitations under the License.
\r
16 using System.Collections;
\r
17 using com.google.zxing.qrcode;
\r
18 using com.google.zxing.oned;
\r
19 using com.google.zxing.datamatrix;
\r
21 namespace com.google.zxing
\r
23 public sealed class MultiFormatReader : Reader
\r
25 private Hashtable hints;
\r
26 private ArrayList readers;
\r
29 * This version of decode honors the intent of Reader.decode(MonochromeBitmapSource) in that it
\r
30 * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
\r
31 * Use setHints() followed by decodeWithState() for continuous scan applications.
\r
33 * @param image The pixel data to decode
\r
34 * @return The contents of the image
\r
35 * @throws ReaderException Any errors which occurred
\r
37 public Result decode(MonochromeBitmapSource image){
\r
40 return decodeInternal(image);
\r
43 throw new ReaderException(e.Message);
\r
48 * Decode an image using the hints provided. Does not honor existing state.
\r
50 * @param image The pixel data to decode
\r
51 * @param hints The hints to use, clearing the previous state.
\r
52 * @return The contents of the image
\r
53 * @throws ReaderException Any errors which occurred
\r
55 public Result decode(MonochromeBitmapSource image, Hashtable hints){
\r
58 return decodeInternal(image);
\r
59 }catch(Exception e){
\r
60 throw new ReaderException (e.Message);
\r
65 * Decode an image using the state set up by calling setHints() previously. Continuous scan
\r
66 * clients will get a <b>large</b> speed increase by using this instead of decode().
\r
68 * @param image The pixel data to decode
\r
69 * @return The contents of the image
\r
70 * @throws ReaderException Any errors which occurred
\r
72 public Result decodeWithState(MonochromeBitmapSource image){
\r
74 // Make sure to set up the default state so we don't crash
\r
75 if (readers == null) {
\r
78 return decodeInternal(image);
\r
79 }catch(Exception e){
\r
80 throw new ReaderException(e.Message);
\r
85 * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
\r
86 * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
\r
87 * is important for performance in continuous scan clients.
\r
89 * @param hints The set of hints to use for subsequent calls to decode(image)
\r
91 public void setHints(Hashtable hints) {
\r
94 bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);
\r
96 ArrayList possibleFormats = hints == null ? null : (ArrayList)hints[(DecodeHintType.POSSIBLE_FORMATS)];
\r
97 readers = new ArrayList();
\r
98 if (possibleFormats != null)
\r
100 bool addOneDReader =
\r
101 possibleFormats.Contains(BarcodeFormat.UPC_A) ||
\r
102 possibleFormats.Contains(BarcodeFormat.UPC_E) ||
\r
103 possibleFormats.Contains(BarcodeFormat.EAN_13) ||
\r
104 possibleFormats.Contains(BarcodeFormat.EAN_8) ||
\r
105 possibleFormats.Contains(BarcodeFormat.CODE_39) ||
\r
106 possibleFormats.Contains(BarcodeFormat.CODE_128);
\r
107 // Put 1D readers upfront in "normal" mode
\r
109 if (addOneDReader && !tryHarder)
\r
111 readers.Add(new MultiFormatOneDReader(hints));
\r
114 if (possibleFormats.Contains(BarcodeFormat.QR_CODE))
\r
116 readers.Add(new QRCodeReader());
\r
118 // TODO re-enable once Data Matrix is ready
\r
119 if (possibleFormats.Contains(BarcodeFormat.DATAMATRIX)) {
\r
120 readers.Add(new DataMatrixReader());
\r
122 // At end in "try harder" mode
\r
123 if (addOneDReader && tryHarder)
\r
125 readers.Add(new MultiFormatOneDReader(hints));
\r
129 if (readers.Count == 0)
\r
133 readers.Add(new MultiFormatOneDReader(hints));
\r
135 readers.Add(new QRCodeReader());
\r
136 // TODO re-enable once Data Matrix is ready
\r
137 readers.Add(new DataMatrixReader());
\r
140 readers.Add(new MultiFormatOneDReader(hints));
\r
145 private Result decodeInternal(MonochromeBitmapSource image) {
\r
148 int size = readers.Count;
\r
149 for (int i = 0; i < size; i++)
\r
151 Reader reader = (Reader)readers[i];
\r
154 return reader.decode(image, hints);
\r
156 catch (ReaderException re)
\r
162 throw new ReaderException("");
\r
164 catch (Exception e) {
\r
165 throw new ReaderException(e.Message);
\r