Prettify scan result screen, I think
[zxing.git] / csharp / oned / MultiFormatUPCEANReader.cs
1 /*\r
2 * Copyright 2008 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 BarcodeFormat = com.google.zxing.BarcodeFormat;\r
18 using DecodeHintType = com.google.zxing.DecodeHintType;\r
19 using ReaderException = com.google.zxing.ReaderException;\r
20 using Result = com.google.zxing.Result;\r
21 using BitArray = com.google.zxing.common.BitArray;\r
22 namespace com.google.zxing.oned\r
23 {\r
24         \r
25         /// <summary> <p>A reader that can read all available UPC/EAN formats. If a caller wants to try to\r
26         /// read all such formats, it is most efficient to use this implementation rather than invoke\r
27         /// individual readers.</p>\r
28         /// \r
29         /// </summary>\r
30         /// <author>  Sean Owen\r
31         /// </author>\r
32         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
33         /// </author>\r
34         public sealed class MultiFormatUPCEANReader:OneDReader\r
35         {\r
36                 \r
37                 //UPGRADE_NOTE: Final was removed from the declaration of 'readers '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
38                 private System.Collections.ArrayList readers;\r
39                 \r
40                 public MultiFormatUPCEANReader(System.Collections.Hashtable hints)\r
41                 {\r
42                         System.Collections.ArrayList possibleFormats = hints == null?null:(System.Collections.ArrayList) hints[DecodeHintType.POSSIBLE_FORMATS];\r
43                         readers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));\r
44                         if (possibleFormats != null)\r
45                         {\r
46                                 if (possibleFormats.Contains(BarcodeFormat.EAN_13))\r
47                                 {\r
48                                         readers.Add(new EAN13Reader());\r
49                                 }\r
50                                 else if (possibleFormats.Contains(BarcodeFormat.UPC_A))\r
51                                 {\r
52                                         readers.Add(new UPCAReader());\r
53                                 }\r
54                                 if (possibleFormats.Contains(BarcodeFormat.EAN_8))\r
55                                 {\r
56                                         readers.Add(new EAN8Reader());\r
57                                 }\r
58                                 if (possibleFormats.Contains(BarcodeFormat.UPC_E))\r
59                                 {\r
60                                         readers.Add(new UPCEReader());\r
61                                 }\r
62                         }\r
63                         if ((readers.Count == 0))\r
64                         {\r
65                                 readers.Add(new EAN13Reader());\r
66                                 // UPC-A is covered by EAN-13\r
67                                 readers.Add(new EAN8Reader());\r
68                                 readers.Add(new UPCEReader());\r
69                         }\r
70                 }\r
71                 \r
72                 public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints)\r
73                 {\r
74                         // Compute this location once and reuse it on multiple implementations\r
75                         int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);\r
76                         int size = readers.Count;\r
77                         for (int i = 0; i < size; i++)\r
78                         {\r
79                                 UPCEANReader reader = (UPCEANReader) readers[i];\r
80                                 Result result;\r
81                                 try\r
82                                 {\r
83                                         result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);\r
84                                 }\r
85                                 catch (ReaderException re)\r
86                                 {\r
87                                         continue;\r
88                                 }\r
89                                 // Special case: a 12-digit code encoded in UPC-A is identical to a "0"\r
90                                 // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,\r
91                                 // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".\r
92                                 // Individually these are correct and their readers will both read such a code\r
93                                 // and correctly call it EAN-13, or UPC-A, respectively.\r
94                                 //\r
95                                 // In this case, if we've been looking for both types, we'd like to call it\r
96                                 // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read\r
97                                 // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A\r
98                                 // result if appropriate.\r
99                                 if (result.BarcodeFormat.Equals(BarcodeFormat.EAN_13) && result.Text[0] == '0')\r
100                                 {\r
101                                         return new Result(result.Text.Substring(1), null, result.ResultPoints, BarcodeFormat.UPC_A);\r
102                                 }\r
103                                 return result;\r
104                         }\r
105                         \r
106                         throw ReaderException.Instance;\r
107                 }\r
108         }\r
109 }