2 * Copyright 2008 ZXing authors
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 using BarcodeFormat = com.google.zxing.BarcodeFormat;
\r
18 using ReaderException = com.google.zxing.ReaderException;
\r
19 using BitArray = com.google.zxing.common.BitArray;
\r
20 namespace com.google.zxing.oned
\r
23 /// <summary> <p>Implements decoding of the UPC-E format.</p>
\r
25 /// <p><a href="http://www.barcodeisland.com/upce.phtml">This</a> is a great reference for
\r
26 /// UPC-E information.</p>
\r
29 /// <author> Sean Owen
\r
31 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
33 public sealed class UPCEReader:UPCEANReader
\r
35 override internal BarcodeFormat BarcodeFormat
\r
39 return BarcodeFormat.UPC_E;
\r
44 /// <summary> The pattern that marks the middle, and end, of a UPC-E pattern.
\r
45 /// There is no "second half" to a UPC-E barcode.
\r
47 //UPGRADE_NOTE: Final was removed from the declaration of 'MIDDLE_END_PATTERN'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
48 private static readonly int[] MIDDLE_END_PATTERN = new int[]{1, 1, 1, 1, 1, 1};
\r
50 /// <summary> See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of
\r
51 /// even-odd parity encodings of digits that imply both the number system (0 or 1)
\r
52 /// used, and the check digit.
\r
54 //UPGRADE_NOTE: Final was removed from the declaration of 'NUMSYS_AND_CHECK_DIGIT_PATTERNS'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
55 private static readonly int[][] NUMSYS_AND_CHECK_DIGIT_PATTERNS = new int[][]{new int[]{0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25}, new int[]{0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}};
\r
57 //UPGRADE_NOTE: Final was removed from the declaration of 'decodeMiddleCounters '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
58 private int[] decodeMiddleCounters;
\r
62 decodeMiddleCounters = new int[4];
\r
65 protected internal override int decodeMiddle(BitArray row, int[] startRange, System.Text.StringBuilder result)
\r
67 int[] counters = decodeMiddleCounters;
\r
73 int rowOffset = startRange[1];
\r
75 int lgPatternFound = 0;
\r
77 for (int x = 0; x < 6 && rowOffset < end; x++)
\r
79 int bestMatch = decodeDigit(row, counters, rowOffset, L_AND_G_PATTERNS);
\r
80 result.Append((char) ('0' + bestMatch % 10));
\r
81 for (int i = 0; i < counters.Length; i++)
\r
83 rowOffset += counters[i];
\r
85 if (bestMatch >= 10)
\r
87 lgPatternFound |= 1 << (5 - x);
\r
91 determineNumSysAndCheckDigit(result, lgPatternFound);
\r
96 protected internal override int[] decodeEnd(BitArray row, int endStart)
\r
98 return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN);
\r
101 protected internal override bool checkChecksum(System.String s)
\r
103 return base.checkChecksum(convertUPCEtoUPCA(s));
\r
106 private static void determineNumSysAndCheckDigit(System.Text.StringBuilder resultString, int lgPatternFound)
\r
109 for (int numSys = 0; numSys <= 1; numSys++)
\r
111 for (int d = 0; d < 10; d++)
\r
113 if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d])
\r
115 resultString.Insert(0, (char) ('0' + numSys));
\r
116 resultString.Append((char) ('0' + d));
\r
121 throw ReaderException.Instance;
\r
124 /// <summary> Expands a UPC-E value back into its full, equivalent UPC-A code value.
\r
127 /// <param name="upce">UPC-E code as string of digits
\r
129 /// <returns> equivalent UPC-A code as string of digits
\r
131 public static System.String convertUPCEtoUPCA(System.String upce)
\r
133 char[] upceChars = new char[6];
\r
134 SupportClass.GetCharsFromString(upce, 1, 7, upceChars, 0);
\r
135 System.Text.StringBuilder result = new System.Text.StringBuilder(12);
\r
136 result.Append(upce[0]);
\r
137 char lastChar = upceChars[5];
\r
144 result.Append(upceChars, 0, 2);
\r
145 result.Append(lastChar);
\r
146 result.Append("0000");
\r
147 result.Append(upceChars, 2, 3);
\r
151 result.Append(upceChars, 0, 3);
\r
152 result.Append("00000");
\r
153 result.Append(upceChars, 3, 2);
\r
157 result.Append(upceChars, 0, 4);
\r
158 result.Append("00000");
\r
159 result.Append(upceChars[4]);
\r
163 result.Append(upceChars, 0, 5);
\r
164 result.Append("0000");
\r
165 result.Append(lastChar);
\r
169 result.Append(upce[7]);
\r
170 return result.ToString();
\r