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
6 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 using ReaderException = com.google.zxing.ReaderException;
\r
17 using com.google.zxing.common;
\r
18 namespace com.google.zxing.qrcode.decoder
\r
21 /// <summary> <p>QR Codes can encode text as bits in one of several modes, and can use multiple modes
\r
22 /// in one QR Code. This class decodes the bits back into text.</p>
\r
24 /// <p>See ISO 18004:2006, 6.4.3 - 6.4.7</p>
\r
27 /// <author> srowen@google.com (Sean Owen)
\r
29 public sealed class DecodedBitStreamParser
\r
32 /// <summary> See ISO 18004:2006, 6.4.4 Table 5</summary>
\r
33 //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
34 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
35 private const System.String SHIFT_JIS = "Shift_JIS";
\r
36 private static bool ASSUME_SHIFT_JIS;
\r
38 private DecodedBitStreamParser()
\r
42 internal static System.String decode(sbyte[] bytes, Version version)
\r
44 BitSource bits = new BitSource(bytes);
\r
45 System.Text.StringBuilder result = new System.Text.StringBuilder();
\r
49 // While still another segment to read...
\r
50 mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
\r
51 if (!mode.Equals(Mode.TERMINATOR))
\r
53 // How many characters will follow, encoded in this mode?
\r
54 int count = bits.readBits(mode.getCharacterCountBits(version));
\r
55 if (mode.Equals(Mode.NUMERIC))
\r
57 decodeNumericSegment(bits, result, count);
\r
59 else if (mode.Equals(Mode.ALPHANUMERIC))
\r
61 decodeAlphanumericSegment(bits, result, count);
\r
63 else if (mode.Equals(Mode.BYTE))
\r
65 decodeByteSegment(bits, result, count);
\r
67 else if (mode.Equals(Mode.KANJI))
\r
69 decodeKanjiSegment(bits, result, count);
\r
73 throw new ReaderException("Unsupported mode indicator");
\r
77 while (!mode.Equals(Mode.TERMINATOR));
\r
79 // I thought it wasn't allowed to leave extra bytes after the terminator but it happens
\r
81 int bitsLeft = bits.available();
\r
83 if (bitsLeft > 6 || bits.readBits(bitsLeft) != 0) {
\r
84 throw new ReaderException("Excess bits or non-zero bits after terminator mode indicator");
\r
88 return result.ToString();
\r
91 private static void decodeKanjiSegment(BitSource bits, System.Text.StringBuilder result, int count)
\r
93 // Each character will require 2 bytes. Read the characters as 2-byte pairs
\r
94 // and decode as Shift_JIS afterwards
\r
95 sbyte[] buffer = new sbyte[2 * count];
\r
99 // Each 13 bits encodes a 2-byte character
\r
100 int twoBytes = bits.readBits(13);
\r
101 int assembledTwoBytes = ((twoBytes / 0x0C0) << 8) | (twoBytes % 0x0C0);
\r
102 if (assembledTwoBytes < 0x01F00)
\r
104 // In the 0x8140 to 0x9FFC range
\r
105 assembledTwoBytes += 0x08140;
\r
109 // In the 0xE040 to 0xEBBF range
\r
110 assembledTwoBytes += 0x0C140;
\r
112 buffer[offset] = (sbyte)(assembledTwoBytes >> 8);
\r
113 buffer[offset + 1] = (sbyte)assembledTwoBytes;
\r
117 // Shift_JIS may not be supported in some environments:
\r
120 byte[] bytes = SupportClass.ToByteArray(buffer);
\r
121 //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
122 result.Append(System.Text.Encoding.GetEncoding("Shift_JIS").GetString(bytes, 0, bytes.Length));
\r
124 catch (System.IO.IOException uee)
\r
126 throw new ReaderException("SHIFT_JIS encoding is not supported on this device");
\r
130 private static void decodeByteSegment(BitSource bits, System.Text.StringBuilder result, int count)
\r
132 sbyte[] readBytes = new sbyte[count];
\r
133 if (count << 3 > bits.available())
\r
135 throw new ReaderException("Count too large: " + count);
\r
137 for (int i = 0; i < count; i++)
\r
139 readBytes[i] = (sbyte)bits.readBits(8);
\r
141 // The spec isn't clear on this mode; see
\r
142 // section 6.4.5: t does not say which encoding to assuming
\r
143 // upon decoding. I have seen ISO-8859-1 used as well as
\r
144 // Shift_JIS -- without anything like an ECI designator to
\r
146 System.String encoding = guessEncoding(readBytes);
\r
149 byte[] bytes = SupportClass.ToByteArray(readBytes);
\r
150 //System.Windows.Forms.MessageBox.Show("encodings: "+ System.Text.Encoding.());
\r
151 //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
152 result.Append(System.Text.Encoding.GetEncoding(encoding).GetString(bytes, 0, bytes.Length));
\r
154 catch (System.IO.IOException uce)
\r
156 //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
157 throw new ReaderException(uce.ToString());
\r
161 private static void decodeAlphanumericSegment(BitSource bits, System.Text.StringBuilder result, int count)
\r
163 // Read two characters at a time
\r
166 int nextTwoCharsBits = bits.readBits(11);
\r
167 result.Append(ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]);
\r
168 result.Append(ALPHANUMERIC_CHARS[nextTwoCharsBits % 45]);
\r
173 // special case: one character left
\r
174 result.Append(ALPHANUMERIC_CHARS[bits.readBits(6)]);
\r
178 private static void decodeNumericSegment(BitSource bits, System.Text.StringBuilder result, int count)
\r
180 // Read three digits at a time
\r
183 // Each 10 bits encodes three digits
\r
184 int threeDigitsBits = bits.readBits(10);
\r
185 if (threeDigitsBits >= 1000)
\r
187 throw new ReaderException("Illegal value for 3-digit unit: " + threeDigitsBits);
\r
189 result.Append(ALPHANUMERIC_CHARS[threeDigitsBits / 100]);
\r
190 result.Append(ALPHANUMERIC_CHARS[(threeDigitsBits / 10) % 10]);
\r
191 result.Append(ALPHANUMERIC_CHARS[threeDigitsBits % 10]);
\r
196 // Two digits left over to read, encoded in 7 bits
\r
197 int twoDigitsBits = bits.readBits(7);
\r
198 if (twoDigitsBits >= 100)
\r
200 throw new ReaderException("Illegal value for 2-digit unit: " + twoDigitsBits);
\r
202 result.Append(ALPHANUMERIC_CHARS[twoDigitsBits / 10]);
\r
203 result.Append(ALPHANUMERIC_CHARS[twoDigitsBits % 10]);
\r
205 else if (count == 1)
\r
207 // One digit left over to read
\r
208 int digitBits = bits.readBits(4);
\r
209 if (digitBits >= 10)
\r
211 throw new ReaderException("Illegal value for digit unit: " + digitBits);
\r
213 result.Append(ALPHANUMERIC_CHARS[digitBits]);
\r
217 private static System.String guessEncoding(sbyte[] bytes)
\r
219 if (ASSUME_SHIFT_JIS)
\r
223 // For now, merely tries to distinguish ISO-8859-1 and Shift_JIS,
\r
224 // which should be by far the most common encodings. ISO-8859-1
\r
225 // should not have bytes in the 0x80 - 0x9F range, while Shift_JIS
\r
226 // uses this as a first byte of a two-byte character. If we see this
\r
227 // followed by a valid second byte in Shift_JIS, assume it is Shift_JIS.
\r
228 int length = bytes.Length;
\r
229 for (int i = 0; i < length; i++)
\r
231 int value_Renamed = bytes[i] & 0xFF;
\r
232 if (value_Renamed >= 0x80 && value_Renamed <= 0x9F && i < length - 1)
\r
234 // ISO-8859-1 shouldn't use this, but before we decide it is Shift_JIS,
\r
235 // just double check that it is followed by a byte that's valid in
\r
236 // the Shift_JIS encoding
\r
237 int nextValue = bytes[i + 1] & 0xFF;
\r
238 if ((value_Renamed & 0x1) == 0)
\r
241 if (nextValue >= 0x40 && nextValue <= 0x9E)
\r
248 if (nextValue >= 0x9F && nextValue <= 0x7C)
\r
257 //static DecodedBitStreamParser()
\r
260 // //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
261 // System.String platformDefault = System_Renamed.getProperty("file.encoding");
\r
262 // ASSUME_SHIFT_JIS = SHIFT_JIS.ToUpper().Equals(platformDefault.ToUpper()) || "EUC-JP".ToUpper().Equals(platformDefault.ToUpper());
\r