Don't need to block multiple thread access. Refactor and update a bit for an upcoming...
[zxing.git] / csharp / MultiFormatReader.cs
index 9c0f09c..0c58f9f 100755 (executable)
@@ -1,4 +1,6 @@
-/*\r
+/*\r
+* Copyright 2007 ZXing authors\r
+*\r
 * Licensed under the Apache License, Version 2.0 (the "License");\r
 * you may not use this file except in compliance with the License.\r
 * You may obtain a copy of the License at\r
 * See the License for the specific language governing permissions and\r
 * limitations under the License.\r
 */\r
-\r
 using System;\r
-using System.Collections;\r
-using com.google.zxing.qrcode;\r
-using com.google.zxing.oned;\r
-\r
+using MultiFormatOneDReader = com.google.zxing.oned.MultiFormatOneDReader;\r
+using PDF417Reader = com.google.zxing.pdf417.PDF417Reader;\r
+using QRCodeReader = com.google.zxing.qrcode.QRCodeReader;\r
+using DataMatrixReader = com.google.zxing.datamatrix.DataMatrixReader;\r
 namespace com.google.zxing\r
 {\r
-    public sealed class MultiFormatReader : Reader\r
-    { \r
-          private Hashtable hints;\r
-          private ArrayList readers;\r
-\r
-          /**\r
-           * This version of decode honors the intent of Reader.decode(MonochromeBitmapSource) in that it\r
-           * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.\r
-           * Use setHints() followed by decodeWithState() for continuous scan applications.\r
-           *\r
-           * @param image The pixel data to decode\r
-           * @return The contents of the image\r
-           * @throws ReaderException Any errors which occurred\r
-           */\r
-          public Result decode(MonochromeBitmapSource image){\r
-              try{\r
-                setHints(null);\r
-                return decodeInternal(image);\r
-              }\r
-              catch(Exception e){\r
-                throw new ReaderException(e.Message);\r
-              }           \r
-          }\r
-\r
-          /**\r
-           * Decode an image using the hints provided. Does not honor existing state.\r
-           *\r
-           * @param image The pixel data to decode\r
-           * @param hints The hints to use, clearing the previous state.\r
-           * @return The contents of the image\r
-           * @throws ReaderException Any errors which occurred\r
-           */\r
-          public Result decode(MonochromeBitmapSource image, Hashtable hints){\r
-              try{\r
-                 setHints(hints);\r
-                 return decodeInternal(image);\r
-              }catch(Exception e){\r
-                throw new ReaderException (e.Message);\r
-              }           \r
-          }\r
-\r
-          /**\r
-           * Decode an image using the state set up by calling setHints() previously. Continuous scan\r
-           * clients will get a <b>large</b> speed increase by using this instead of decode().\r
-           *\r
-           * @param image The pixel data to decode\r
-           * @return The contents of the image\r
-           * @throws ReaderException Any errors which occurred\r
-           */\r
-          public Result decodeWithState(MonochromeBitmapSource image){\r
-              try{\r
-                // Make sure to set up the default state so we don't crash\r
-                if (readers == null) {\r
-                  setHints(null);\r
-                }\r
-                return decodeInternal(image);\r
-              }catch(Exception e){\r
-                throw new ReaderException(e.Message);\r
-              }            \r
-          }\r
-\r
-          /**\r
-           * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls\r
-           * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This\r
-           * is important for performance in continuous scan clients.\r
-           *\r
-           * @param hints The set of hints to use for subsequent calls to decode(image)\r
-           */\r
-          public void setHints(Hashtable hints) {\r
-              this.hints = hints;\r
-\r
-              bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);\r
-\r
-              ArrayList possibleFormats = hints == null ? null : (ArrayList)hints[(DecodeHintType.POSSIBLE_FORMATS)];\r
-              readers = new ArrayList();\r
-              if (possibleFormats != null)\r
-              {\r
-                  bool addOneDReader =\r
-                  possibleFormats.Contains(BarcodeFormat.UPC_A) ||\r
-                      possibleFormats.Contains(BarcodeFormat.UPC_E) ||\r
-                      possibleFormats.Contains(BarcodeFormat.EAN_13) ||\r
-                      possibleFormats.Contains(BarcodeFormat.EAN_8) ||\r
-                      possibleFormats.Contains(BarcodeFormat.CODE_39) ||\r
-                      possibleFormats.Contains(BarcodeFormat.CODE_128);\r
-                  // Put 1D readers upfront in "normal" mode\r
-\r
-                  if (addOneDReader && !tryHarder)\r
-                  {\r
-                      readers.Add(new MultiFormatOneDReader(hints));\r
-                  }\r
-\r
-                  if (possibleFormats.Contains(BarcodeFormat.QR_CODE))\r
-                  {\r
-                      readers.Add(new QRCodeReader());\r
-                  }\r
-                  // TODO re-enable once Data Matrix is ready\r
-                  if (possibleFormats.Contains(BarcodeFormat.DATAMATRIX)) {\r
-                    //readers.Add(new DataMatrixReader());\r
-                  }\r
-                  // At end in "try harder" mode\r
-                  if (addOneDReader && tryHarder)\r
-                  {\r
-                      readers.Add(new MultiFormatOneDReader(hints));\r
-                  }\r
-              }\r
-\r
-              if (readers.Count == 0)\r
-              {\r
-                  if (!tryHarder)\r
-                  {\r
-                      //readers.Add(new MultiFormatOneDReader(hints));\r
-                  }\r
-                  readers.Add(new QRCodeReader());\r
-                  // TODO re-enable once Data Matrix is ready\r
-                  // readers.addElement(new DataMatrixReader());\r
-                  if (tryHarder)\r
-                  {\r
-                      //readers.Add(new MultiFormatOneDReader(hints));\r
-                  }\r
-              }\r
-          }\r
-\r
-          private Result decodeInternal(MonochromeBitmapSource image) {\r
-              try\r
-              {\r
-                  int size = readers.Count;\r
-                  for (int i = 0; i < size; i++)\r
-                  {\r
-                      Reader reader = (Reader)readers[i];\r
-                      try\r
-                      {\r
-                          return reader.decode(image, hints);\r
-                      }\r
-                      catch (ReaderException re)\r
-                      {\r
-                          // continue\r
-                      }\r
-                  }\r
-\r
-                  throw new ReaderException("");\r
-              }\r
-              catch (Exception e) {\r
-                  throw new ReaderException(e.Message);\r
-              }\r
-          }\r
-    \r
-    \r
-    }\r
+       \r
+       /// <summary> MultiFormatReader is a convenience class and the main entry point into the library for most uses.\r
+       /// By default it attempts to decode all barcode formats that the library supports. Optionally, you\r
+       /// can provide a hints object to request different behavior, for example only decoding QR codes.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class MultiFormatReader : Reader\r
+       {\r
+               /// <summary> This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls\r
+               /// to decodeWithState(image) can reuse the same set of readers without reallocating memory. This\r
+               /// is important for performance in continuous scan clients.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="hints">The set of hints to use for subsequent calls to decode(image)\r
+               /// </param>\r
+               public System.Collections.Hashtable Hints\r
+               {\r
+                       set\r
+                       {\r
+                               this.hints = value;\r
+                               \r
+                               bool tryHarder = value != null && value.ContainsKey(DecodeHintType.TRY_HARDER);\r
+                               System.Collections.ArrayList formats = value == null?null:(System.Collections.ArrayList) value[DecodeHintType.POSSIBLE_FORMATS];\r
+                               readers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
+                               if (formats != null)\r
+                               {\r
+                                       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
+                                       // Put 1D readers upfront in "normal" mode\r
+                                       if (addOneDReader && !tryHarder)\r
+                                       {\r
+                                               readers.Add(new MultiFormatOneDReader(value));\r
+                                       }\r
+                                       if (formats.Contains(BarcodeFormat.QR_CODE))\r
+                                       {\r
+                                               readers.Add(new QRCodeReader());\r
+                                       }\r
+                                       if (formats.Contains(BarcodeFormat.DATAMATRIX))\r
+                                       {\r
+                                               readers.Add(new DataMatrixReader());\r
+                                       }\r
+                                       if (formats.Contains(BarcodeFormat.PDF417))\r
+                                       {\r
+                                               readers.Add(new PDF417Reader());\r
+                                       }\r
+                                       // At end in "try harder" mode\r
+                                       if (addOneDReader && tryHarder)\r
+                                       {\r
+                                               readers.Add(new MultiFormatOneDReader(value));\r
+                                       }\r
+                               }\r
+                               if ((readers.Count == 0))\r
+                               {\r
+                                       if (!tryHarder)\r
+                                       {\r
+                                               readers.Add(new MultiFormatOneDReader(value));\r
+                                       }\r
+                                       readers.Add(new QRCodeReader());\r
+                                       \r
+                                       // TODO re-enable once Data Matrix is ready\r
+                                       // readers.addElement(new DataMatrixReader());\r
+                                       \r
+                                       // TODO: Enable once PDF417 has passed QA\r
+                                       //readers.addElement(new PDF417Reader());\r
+                                       \r
+                                       if (tryHarder)\r
+                                       {\r
+                                               readers.Add(new MultiFormatOneDReader(value));\r
+                                       }\r
+                               }\r
+                       }\r
+                       \r
+               }\r
+               \r
+               private System.Collections.Hashtable hints;\r
+               private System.Collections.ArrayList readers;\r
+               \r
+               /// <summary> This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it\r
+               /// passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.\r
+               /// Use setHints() followed by decodeWithState() for continuous scan applications.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">The pixel data to decode\r
+               /// </param>\r
+               /// <returns> The contents of the image\r
+               /// </returns>\r
+               /// <throws>  ReaderException Any errors which occurred </throws>\r
+               public Result decode(BinaryBitmap image)\r
+               {\r
+                       Hints = null;\r
+                       return decodeInternal(image);\r
+               }\r
+               \r
+               /// <summary> Decode an image using the hints provided. Does not honor existing state.\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">The pixel data to decode\r
+               /// </param>\r
+               /// <param name="hints">The hints to use, clearing the previous state.\r
+               /// </param>\r
+               /// <returns> The contents of the image\r
+               /// </returns>\r
+               /// <throws>  ReaderException Any errors which occurred </throws>\r
+               public Result decode(BinaryBitmap image, System.Collections.Hashtable hints)\r
+               {\r
+                       Hints = hints;\r
+                       return decodeInternal(image);\r
+               }\r
+               \r
+               /// <summary> Decode an image using the state set up by calling setHints() previously. Continuous scan\r
+               /// clients will get a <b>large</b> speed increase by using this instead of decode().\r
+               /// \r
+               /// </summary>\r
+               /// <param name="image">The pixel data to decode\r
+               /// </param>\r
+               /// <returns> The contents of the image\r
+               /// </returns>\r
+               /// <throws>  ReaderException Any errors which occurred </throws>\r
+               public Result decodeWithState(BinaryBitmap image)\r
+               {\r
+                       // Make sure to set up the default state so we don't crash\r
+                       if (readers == null)\r
+                       {\r
+                               Hints = null;\r
+                       }\r
+                       return decodeInternal(image);\r
+               }\r
+               \r
+               private Result decodeInternal(BinaryBitmap image)\r
+               {\r
+                       int size = readers.Count;\r
+                       for (int i = 0; i < size; i++)\r
+                       {\r
+                               Reader reader = (Reader) readers[i];\r
+                               try\r
+                               {\r
+                                       return reader.decode(image, hints);\r
+                               }\r
+                               catch (ReaderException re)\r
+                               {\r
+                                       // continue\r
+                               }\r
+                       }\r
+                       \r
+                       throw ReaderException.Instance;\r
+               }\r
+       }\r
 }
\ No newline at end of file