+++ /dev/null
-/*\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
-\r
-using System;\r
-using ReaderException = com.google.zxing.ReaderException;\r
-using com.google.zxing.common;\r
-namespace com.google.zxing.qrcode.decoder\r
-{\r
-\r
- /// <summary> <p>QR Codes can encode text as bits in one of several modes, and can use multiple modes\r
- /// in one QR Code. This class decodes the bits back into text.</p>\r
- /// \r
- /// <p>See ISO 18004:2006, 6.4.3 - 6.4.7</p>\r
- /// \r
- /// </summary>\r
- /// <author> srowen@google.com (Sean Owen)\r
- /// </author>\r
- public sealed class DecodedBitStreamParser\r
- {\r
-\r
- /// <summary> See ISO 18004:2006, 6.4.4 Table 5</summary>\r
- //UPGRADE_NOTE: Final was removed from the declaration of 'ALPHANUMERIC_CHARS '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
- private static readonly char[] ALPHANUMERIC_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':' };\r
- private const System.String SHIFT_JIS = "Shift_JIS";\r
- private static bool ASSUME_SHIFT_JIS;\r
-\r
- private DecodedBitStreamParser()\r
- {\r
- }\r
-\r
- internal static System.String decode(sbyte[] bytes, Version version)\r
- {\r
- BitSource bits = new BitSource(bytes);\r
- System.Text.StringBuilder result = new System.Text.StringBuilder();\r
- Mode mode;\r
- do\r
- {\r
- // While still another segment to read...\r
- mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits\r
- if (!mode.Equals(Mode.TERMINATOR))\r
- {\r
- // How many characters will follow, encoded in this mode?\r
- int count = bits.readBits(mode.getCharacterCountBits(version));\r
- if (mode.Equals(Mode.NUMERIC))\r
- {\r
- decodeNumericSegment(bits, result, count);\r
- }\r
- else if (mode.Equals(Mode.ALPHANUMERIC))\r
- {\r
- decodeAlphanumericSegment(bits, result, count);\r
- }\r
- else if (mode.Equals(Mode.BYTE))\r
- {\r
- decodeByteSegment(bits, result, count);\r
- }\r
- else if (mode.Equals(Mode.KANJI))\r
- {\r
- decodeKanjiSegment(bits, result, count);\r
- }\r
- else\r
- {\r
- throw new ReaderException("Unsupported mode indicator");\r
- }\r
- }\r
- }\r
- while (!mode.Equals(Mode.TERMINATOR));\r
-\r
- // I thought it wasn't allowed to leave extra bytes after the terminator but it happens\r
- /*\r
- int bitsLeft = bits.available();\r
- if (bitsLeft > 0) {\r
- if (bitsLeft > 6 || bits.readBits(bitsLeft) != 0) {\r
- throw new ReaderException("Excess bits or non-zero bits after terminator mode indicator");\r
- }\r
- }\r
- */\r
- return result.ToString();\r
- }\r
-\r
- private static void decodeKanjiSegment(BitSource bits, System.Text.StringBuilder result, int count)\r
- {\r
- // Each character will require 2 bytes. Read the characters as 2-byte pairs\r
- // and decode as Shift_JIS afterwards\r
- sbyte[] buffer = new sbyte[2 * count];\r
- int offset = 0;\r
- while (count > 0)\r
- {\r
- // Each 13 bits encodes a 2-byte character\r
- int twoBytes = bits.readBits(13);\r
- int assembledTwoBytes = ((twoBytes / 0x0C0) << 8) | (twoBytes % 0x0C0);\r
- if (assembledTwoBytes < 0x01F00)\r
- {\r
- // In the 0x8140 to 0x9FFC range\r
- assembledTwoBytes += 0x08140;\r
- }\r
- else\r
- {\r
- // In the 0xE040 to 0xEBBF range\r
- assembledTwoBytes += 0x0C140;\r
- }\r
- buffer[offset] = (sbyte)(assembledTwoBytes >> 8);\r
- buffer[offset + 1] = (sbyte)assembledTwoBytes;\r
- offset += 2;\r
- count--;\r
- }\r
- // Shift_JIS may not be supported in some environments:\r
- try\r
- {\r
- byte[] bytes = SupportClass.ToByteArray(buffer);\r
- //UPGRADE_TODO: The differences in the Format of parameters for constructor 'java.lang.String.String' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"\r
- result.Append(System.Text.Encoding.GetEncoding("Shift_JIS").GetString(bytes, 0, bytes.Length));\r
- }\r
- catch (System.IO.IOException uee)\r
- {\r
- throw new ReaderException("SHIFT_JIS encoding is not supported on this device");\r
- }\r
- }\r
-\r
- private static void decodeByteSegment(BitSource bits, System.Text.StringBuilder result, int count)\r
- {\r
- sbyte[] readBytes = new sbyte[count];\r
- if (count << 3 > bits.available())\r
- {\r
- throw new ReaderException("Count too large: " + count);\r
- }\r
- for (int i = 0; i < count; i++)\r
- {\r
- readBytes[i] = (sbyte)bits.readBits(8);\r
- }\r
- // The spec isn't clear on this mode; see\r
- // section 6.4.5: t does not say which encoding to assuming\r
- // upon decoding. I have seen ISO-8859-1 used as well as\r
- // Shift_JIS -- without anything like an ECI designator to\r
- // give a hint.\r
- System.String encoding = guessEncoding(readBytes);\r
- try\r
- {\r
- byte[] bytes = SupportClass.ToByteArray(readBytes);\r
- //System.Windows.Forms.MessageBox.Show("encodings: "+ System.Text.Encoding.());\r
- //UPGRADE_TODO: The differences in the Format of parameters for constructor 'java.lang.String.String' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"\r
- result.Append(System.Text.Encoding.GetEncoding(encoding).GetString(bytes, 0, bytes.Length));\r
- }\r
- catch (System.IO.IOException uce)\r
- {\r
- //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"\r
- throw new ReaderException(uce.ToString());\r
- }\r
- }\r
-\r
- private static void decodeAlphanumericSegment(BitSource bits, System.Text.StringBuilder result, int count)\r
- {\r
- // Read two characters at a time\r
- while (count > 1)\r
- {\r
- int nextTwoCharsBits = bits.readBits(11);\r
- result.Append(ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]);\r
- result.Append(ALPHANUMERIC_CHARS[nextTwoCharsBits % 45]);\r
- count -= 2;\r
- }\r
- if (count == 1)\r
- {\r
- // special case: one character left\r
- result.Append(ALPHANUMERIC_CHARS[bits.readBits(6)]);\r
- }\r
- }\r
-\r
- private static void decodeNumericSegment(BitSource bits, System.Text.StringBuilder result, int count)\r
- {\r
- // Read three digits at a time\r
- while (count >= 3)\r
- {\r
- // Each 10 bits encodes three digits\r
- int threeDigitsBits = bits.readBits(10);\r
- if (threeDigitsBits >= 1000)\r
- {\r
- throw new ReaderException("Illegal value for 3-digit unit: " + threeDigitsBits);\r
- }\r
- result.Append(ALPHANUMERIC_CHARS[threeDigitsBits / 100]);\r
- result.Append(ALPHANUMERIC_CHARS[(threeDigitsBits / 10) % 10]);\r
- result.Append(ALPHANUMERIC_CHARS[threeDigitsBits % 10]);\r
- count -= 3;\r
- }\r
- if (count == 2)\r
- {\r
- // Two digits left over to read, encoded in 7 bits\r
- int twoDigitsBits = bits.readBits(7);\r
- if (twoDigitsBits >= 100)\r
- {\r
- throw new ReaderException("Illegal value for 2-digit unit: " + twoDigitsBits);\r
- }\r
- result.Append(ALPHANUMERIC_CHARS[twoDigitsBits / 10]);\r
- result.Append(ALPHANUMERIC_CHARS[twoDigitsBits % 10]);\r
- }\r
- else if (count == 1)\r
- {\r
- // One digit left over to read\r
- int digitBits = bits.readBits(4);\r
- if (digitBits >= 10)\r
- {\r
- throw new ReaderException("Illegal value for digit unit: " + digitBits);\r
- }\r
- result.Append(ALPHANUMERIC_CHARS[digitBits]);\r
- }\r
- }\r
-\r
- private static System.String guessEncoding(sbyte[] bytes)\r
- {\r
- if (ASSUME_SHIFT_JIS)\r
- {\r
- return SHIFT_JIS;\r
- }\r
- // For now, merely tries to distinguish ISO-8859-1 and Shift_JIS,\r
- // which should be by far the most common encodings. ISO-8859-1\r
- // should not have bytes in the 0x80 - 0x9F range, while Shift_JIS\r
- // uses this as a first byte of a two-byte character. If we see this\r
- // followed by a valid second byte in Shift_JIS, assume it is Shift_JIS.\r
- int length = bytes.Length;\r
- for (int i = 0; i < length; i++)\r
- {\r
- int value_Renamed = bytes[i] & 0xFF;\r
- if (value_Renamed >= 0x80 && value_Renamed <= 0x9F && i < length - 1)\r
- {\r
- // ISO-8859-1 shouldn't use this, but before we decide it is Shift_JIS,\r
- // just double check that it is followed by a byte that's valid in\r
- // the Shift_JIS encoding\r
- int nextValue = bytes[i + 1] & 0xFF;\r
- if ((value_Renamed & 0x1) == 0)\r
- {\r
- // if even,\r
- if (nextValue >= 0x40 && nextValue <= 0x9E)\r
- {\r
- return SHIFT_JIS;\r
- }\r
- }\r
- else\r
- {\r
- if (nextValue >= 0x9F && nextValue <= 0x7C)\r
- {\r
- return SHIFT_JIS;\r
- }\r
- }\r
- }\r
- }\r
- return "ASCII";\r
- }\r
- //static DecodedBitStreamParser()\r
- //{\r
- // {\r
- // //UPGRADE_ISSUE: Method 'java.lang.System.getProperty' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangSystem'"\r
- // System.String platformDefault = System_Renamed.getProperty("file.encoding");\r
- // ASSUME_SHIFT_JIS = SHIFT_JIS.ToUpper().Equals(platformDefault.ToUpper()) || "EUC-JP".ToUpper().Equals(platformDefault.ToUpper());\r
- // }\r
- //}\r
- }\r
-}
\ No newline at end of file