Committed C# port from Mohamad
[zxing.git] / csharp / oned / MultiFormatUPCEANReader.cs
diff --git a/csharp/oned/MultiFormatUPCEANReader.cs b/csharp/oned/MultiFormatUPCEANReader.cs
new file mode 100755 (executable)
index 0000000..98dbf79
--- /dev/null
@@ -0,0 +1,82 @@
+/*\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
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+namespace com.google.zxing.oned\r
+{\r
+    /**\r
+     * @author dswitkin@google.com (Daniel Switkin)\r
+     * @author Sean Owen\r
+     */\r
+    using System.Text;\r
+    using com.google.zxing.common;\r
+\r
+    public sealed class MultiFormatUPCEANReader : AbstractOneDReader\r
+    { \r
+          private  System.Collections.ArrayList readers;\r
+          public MultiFormatUPCEANReader(System.Collections.Hashtable hints) {\r
+            System.Collections.ArrayList possibleFormats = hints == null ? null : (System.Collections.ArrayList) hints[DecodeHintType.POSSIBLE_FORMATS];\r
+            readers = new System.Collections.ArrayList();\r
+            if (possibleFormats != null) {\r
+              if (possibleFormats.Contains(BarcodeFormat.EAN_13)) {\r
+                readers.Add(new EAN13Reader());\r
+              } else if (possibleFormats.Contains(BarcodeFormat.UPC_A)) {\r
+                readers.Add(new UPCAReader());\r
+              }\r
+              if (possibleFormats.Contains(BarcodeFormat.EAN_8)) {\r
+                readers.Add(new EAN8Reader());\r
+              }\r
+              if (possibleFormats.Contains(BarcodeFormat.UPC_E)) {\r
+                readers.Add(new UPCEReader());\r
+              }\r
+            }\r
+            if (readers.Count==0) {\r
+              readers.Add(new EAN13Reader());\r
+              // UPC-A is covered by EAN-13\r
+              readers.Add(new EAN8Reader());\r
+              readers.Add(new UPCEReader());\r
+            }\r
+          }\r
+\r
+          public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints) {\r
+            // Compute this location once and reuse it on multiple implementations\r
+            int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row);\r
+            int size = readers.Count;\r
+            for (int i = 0; i < size; i++) {\r
+              UPCEANReader reader = (UPCEANReader) readers[i];\r
+              Result result;\r
+              try {\r
+                result = reader.decodeRow(rowNumber, row, startGuardPattern);\r
+              } catch (ReaderException re) {\r
+                continue;\r
+              }\r
+              // Special case: a 12-digit code encoded in UPC-A is identical to a "0"\r
+              // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,\r
+              // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".\r
+              // Individually these are correct and their readers will both read such a code\r
+              // and correctly call it EAN-13, or UPC-A, respectively.\r
+              //\r
+              // In this case, if we've been looking for both types, we'd like to call it\r
+              // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read\r
+              // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A\r
+              // result if appropriate.\r
+              if (result.getBarcodeFormat().Equals(BarcodeFormat.EAN_13) && result.getText()[0] == '0') {\r
+                return new Result(result.getText().Substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);\r
+              }\r
+              return result;\r
+            }\r
+\r
+            throw new ReaderException();\r
+          }\r
+    \r
+    }\r
+}
\ No newline at end of file