Korean translation from Chang Hyun Park
[zxing.git] / csharp / MultiFormatReader.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\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
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\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
15 */\r
16 using System;\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
22 {\r
23         \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
27         /// \r
28         /// </summary>\r
29         /// <author>  Sean Owen\r
30         /// </author>\r
31         /// <author>  dswitkin@google.com (Daniel Switkin)\r
32         /// </author>\r
33         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
34         /// </author>\r
35         public sealed class MultiFormatReader : Reader\r
36         {\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
40                 /// \r
41                 /// </summary>\r
42                 /// <param name="hints">The set of hints to use for subsequent calls to decode(image)\r
43                 /// </param>\r
44                 public System.Collections.Hashtable Hints\r
45                 {\r
46                         set\r
47                         {\r
48                                 this.hints = value;\r
49                                 \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
54                                 {\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
58                                         {\r
59                                                 readers.Add(new MultiFormatOneDReader(value));\r
60                                         }\r
61                                         if (formats.Contains(BarcodeFormat.QR_CODE))\r
62                                         {\r
63                                                 readers.Add(new QRCodeReader());\r
64                                         }\r
65                                         if (formats.Contains(BarcodeFormat.DATAMATRIX))\r
66                                         {\r
67                                                 readers.Add(new DataMatrixReader());\r
68                                         }\r
69                                         if (formats.Contains(BarcodeFormat.PDF417))\r
70                                         {\r
71                                                 readers.Add(new PDF417Reader());\r
72                                         }\r
73                                         // At end in "try harder" mode\r
74                                         if (addOneDReader && tryHarder)\r
75                                         {\r
76                                                 readers.Add(new MultiFormatOneDReader(value));\r
77                                         }\r
78                                 }\r
79                                 if ((readers.Count == 0))\r
80                                 {\r
81                                         if (!tryHarder)\r
82                                         {\r
83                                                 readers.Add(new MultiFormatOneDReader(value));\r
84                                         }\r
85                                         readers.Add(new QRCodeReader());\r
86                                         \r
87                                         // TODO re-enable once Data Matrix is ready\r
88                                         // readers.addElement(new DataMatrixReader());\r
89                                         \r
90                                         // TODO: Enable once PDF417 has passed QA\r
91                                         //readers.addElement(new PDF417Reader());\r
92                                         \r
93                                         if (tryHarder)\r
94                                         {\r
95                                                 readers.Add(new MultiFormatOneDReader(value));\r
96                                         }\r
97                                 }\r
98                         }\r
99                         \r
100                 }\r
101                 \r
102                 private System.Collections.Hashtable hints;\r
103                 private System.Collections.ArrayList readers;\r
104                 \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
108                 /// \r
109                 /// </summary>\r
110                 /// <param name="image">The pixel data to decode\r
111                 /// </param>\r
112                 /// <returns> The contents of the image\r
113                 /// </returns>\r
114                 /// <throws>  ReaderException Any errors which occurred </throws>\r
115                 public Result decode(BinaryBitmap image)\r
116                 {\r
117                         Hints = null;\r
118                         return decodeInternal(image);\r
119                 }\r
120                 \r
121                 /// <summary> Decode an image using the hints provided. Does not honor existing state.\r
122                 /// \r
123                 /// </summary>\r
124                 /// <param name="image">The pixel data to decode\r
125                 /// </param>\r
126                 /// <param name="hints">The hints to use, clearing the previous state.\r
127                 /// </param>\r
128                 /// <returns> The contents of the image\r
129                 /// </returns>\r
130                 /// <throws>  ReaderException Any errors which occurred </throws>\r
131                 public Result decode(BinaryBitmap image, System.Collections.Hashtable hints)\r
132                 {\r
133                         Hints = hints;\r
134                         return decodeInternal(image);\r
135                 }\r
136                 \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
139                 /// \r
140                 /// </summary>\r
141                 /// <param name="image">The pixel data to decode\r
142                 /// </param>\r
143                 /// <returns> The contents of the image\r
144                 /// </returns>\r
145                 /// <throws>  ReaderException Any errors which occurred </throws>\r
146                 public Result decodeWithState(BinaryBitmap image)\r
147                 {\r
148                         // Make sure to set up the default state so we don't crash\r
149                         if (readers == null)\r
150                         {\r
151                                 Hints = null;\r
152                         }\r
153                         return decodeInternal(image);\r
154                 }\r
155                 \r
156                 private Result decodeInternal(BinaryBitmap image)\r
157                 {\r
158                         int size = readers.Count;\r
159                         for (int i = 0; i < size; i++)\r
160                         {\r
161                                 Reader reader = (Reader) readers[i];\r
162                                 try\r
163                                 {\r
164                                         return reader.decode(image, hints);\r
165                                 }\r
166                                 catch (ReaderException re)\r
167                                 {\r
168                                         // continue\r
169                                 }\r
170                         }\r
171                         \r
172                         throw ReaderException.Instance;\r
173                 }\r
174         }\r
175 }