--- /dev/null
+/*\r
+* Copyright 2008 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
+*\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
+using System;\r
+using BarcodeFormat = com.google.zxing.BarcodeFormat;\r
+using ReaderException = com.google.zxing.ReaderException;\r
+using BitArray = com.google.zxing.common.BitArray;\r
+namespace com.google.zxing.oned\r
+{\r
+ \r
+ /// <summary> <p>Implements decoding of the UPC-E format.</p>\r
+ /// <p/>\r
+ /// <p><a href="http://www.barcodeisland.com/upce.phtml">This</a> is a great reference for\r
+ /// UPC-E information.</p>\r
+ /// \r
+ /// </summary>\r
+ /// <author> Sean Owen\r
+ /// </author>\r
+ /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+ /// </author>\r
+ public sealed class UPCEReader:UPCEANReader\r
+ {\r
+ override internal BarcodeFormat BarcodeFormat\r
+ {\r
+ get\r
+ {\r
+ return BarcodeFormat.UPC_E;\r
+ }\r
+ \r
+ }\r
+ \r
+ /// <summary> The pattern that marks the middle, and end, of a UPC-E pattern.\r
+ /// There is no "second half" to a UPC-E barcode.\r
+ /// </summary>\r
+ //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
+ private static readonly int[] MIDDLE_END_PATTERN = new int[]{1, 1, 1, 1, 1, 1};\r
+ \r
+ /// <summary> See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of\r
+ /// even-odd parity encodings of digits that imply both the number system (0 or 1)\r
+ /// used, and the check digit.\r
+ /// </summary>\r
+ //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
+ 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
+ \r
+ //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
+ private int[] decodeMiddleCounters;\r
+ \r
+ public UPCEReader()\r
+ {\r
+ decodeMiddleCounters = new int[4];\r
+ }\r
+ \r
+ protected internal override int decodeMiddle(BitArray row, int[] startRange, System.Text.StringBuilder result)\r
+ {\r
+ int[] counters = decodeMiddleCounters;\r
+ counters[0] = 0;\r
+ counters[1] = 0;\r
+ counters[2] = 0;\r
+ counters[3] = 0;\r
+ int end = row.Size;\r
+ int rowOffset = startRange[1];\r
+ \r
+ int lgPatternFound = 0;\r
+ \r
+ for (int x = 0; x < 6 && rowOffset < end; x++)\r
+ {\r
+ int bestMatch = decodeDigit(row, counters, rowOffset, L_AND_G_PATTERNS);\r
+ result.Append((char) ('0' + bestMatch % 10));\r
+ for (int i = 0; i < counters.Length; i++)\r
+ {\r
+ rowOffset += counters[i];\r
+ }\r
+ if (bestMatch >= 10)\r
+ {\r
+ lgPatternFound |= 1 << (5 - x);\r
+ }\r
+ }\r
+ \r
+ determineNumSysAndCheckDigit(result, lgPatternFound);\r
+ \r
+ return rowOffset;\r
+ }\r
+ \r
+ protected internal override int[] decodeEnd(BitArray row, int endStart)\r
+ {\r
+ return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN);\r
+ }\r
+ \r
+ protected internal override bool checkChecksum(System.String s)\r
+ {\r
+ return base.checkChecksum(convertUPCEtoUPCA(s));\r
+ }\r
+ \r
+ private static void determineNumSysAndCheckDigit(System.Text.StringBuilder resultString, int lgPatternFound)\r
+ {\r
+ \r
+ for (int numSys = 0; numSys <= 1; numSys++)\r
+ {\r
+ for (int d = 0; d < 10; d++)\r
+ {\r
+ if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d])\r
+ {\r
+ resultString.Insert(0, (char) ('0' + numSys));\r
+ resultString.Append((char) ('0' + d));\r
+ return ;\r
+ }\r
+ }\r
+ }\r
+ throw ReaderException.Instance;\r
+ }\r
+ \r
+ /// <summary> Expands a UPC-E value back into its full, equivalent UPC-A code value.\r
+ /// \r
+ /// </summary>\r
+ /// <param name="upce">UPC-E code as string of digits\r
+ /// </param>\r
+ /// <returns> equivalent UPC-A code as string of digits\r
+ /// </returns>\r
+ public static System.String convertUPCEtoUPCA(System.String upce)\r
+ {\r
+ char[] upceChars = new char[6];\r
+ SupportClass.GetCharsFromString(upce, 1, 7, upceChars, 0);\r
+ System.Text.StringBuilder result = new System.Text.StringBuilder(12);\r
+ result.Append(upce[0]);\r
+ char lastChar = upceChars[5];\r
+ switch (lastChar)\r
+ {\r
+ \r
+ case '0': \r
+ case '1': \r
+ case '2': \r
+ result.Append(upceChars, 0, 2);\r
+ result.Append(lastChar);\r
+ result.Append("0000");\r
+ result.Append(upceChars, 2, 3);\r
+ break;\r
+ \r
+ case '3': \r
+ result.Append(upceChars, 0, 3);\r
+ result.Append("00000");\r
+ result.Append(upceChars, 3, 2);\r
+ break;\r
+ \r
+ case '4': \r
+ result.Append(upceChars, 0, 4);\r
+ result.Append("00000");\r
+ result.Append(upceChars[4]);\r
+ break;\r
+ \r
+ default: \r
+ result.Append(upceChars, 0, 5);\r
+ result.Append("0000");\r
+ result.Append(lastChar);\r
+ break;\r
+ \r
+ }\r
+ result.Append(upce[7]);\r
+ return result.ToString();\r
+ }\r
+ }\r
+}
\ No newline at end of file