Committed C# port from Mohamad
[zxing.git] / csharp / oned / MultiFormatUPCEANReader.cs
1 /*\r
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
5 *\r
6 *      http://www.apache.org/licenses/LICENSE-2.0\r
7 *\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
13 */\r
14 namespace com.google.zxing.oned\r
15 {\r
16     /**\r
17      * @author dswitkin@google.com (Daniel Switkin)\r
18      * @author Sean Owen\r
19      */\r
20     using System.Text;\r
21     using com.google.zxing.common;\r
22 \r
23     public sealed class MultiFormatUPCEANReader : AbstractOneDReader\r
24     { \r
25           private  System.Collections.ArrayList readers;\r
26           public MultiFormatUPCEANReader(System.Collections.Hashtable hints) {\r
27             System.Collections.ArrayList possibleFormats = hints == null ? null : (System.Collections.ArrayList) hints[DecodeHintType.POSSIBLE_FORMATS];\r
28             readers = new System.Collections.ArrayList();\r
29             if (possibleFormats != null) {\r
30               if (possibleFormats.Contains(BarcodeFormat.EAN_13)) {\r
31                 readers.Add(new EAN13Reader());\r
32               } else if (possibleFormats.Contains(BarcodeFormat.UPC_A)) {\r
33                 readers.Add(new UPCAReader());\r
34               }\r
35               if (possibleFormats.Contains(BarcodeFormat.EAN_8)) {\r
36                 readers.Add(new EAN8Reader());\r
37               }\r
38               if (possibleFormats.Contains(BarcodeFormat.UPC_E)) {\r
39                 readers.Add(new UPCEReader());\r
40               }\r
41             }\r
42             if (readers.Count==0) {\r
43               readers.Add(new EAN13Reader());\r
44               // UPC-A is covered by EAN-13\r
45               readers.Add(new EAN8Reader());\r
46               readers.Add(new UPCEReader());\r
47             }\r
48           }\r
49 \r
50           public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints) {\r
51             // Compute this location once and reuse it on multiple implementations\r
52             int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row);\r
53             int size = readers.Count;\r
54             for (int i = 0; i < size; i++) {\r
55               UPCEANReader reader = (UPCEANReader) readers[i];\r
56               Result result;\r
57               try {\r
58                 result = reader.decodeRow(rowNumber, row, startGuardPattern);\r
59               } catch (ReaderException re) {\r
60                 continue;\r
61               }\r
62               // Special case: a 12-digit code encoded in UPC-A is identical to a "0"\r
63               // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,\r
64               // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".\r
65               // Individually these are correct and their readers will both read such a code\r
66               // and correctly call it EAN-13, or UPC-A, respectively.\r
67               //\r
68               // In this case, if we've been looking for both types, we'd like to call it\r
69               // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read\r
70               // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A\r
71               // result if appropriate.\r
72               if (result.getBarcodeFormat().Equals(BarcodeFormat.EAN_13) && result.getText()[0] == '0') {\r
73                 return new Result(result.getText().Substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A);\r
74               }\r
75               return result;\r
76             }\r
77 \r
78             throw new ReaderException();\r
79           }\r
80     \r
81     }\r
82 }