X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fdatamatrix%2Fdecoder%2FDecodedBitStreamParser.java;h=d5b0394bf901207fc3da1db30237595190873202;hb=67bb87dea0eb848f80814f0353196079023a7aaf;hp=890a668b04ef614fc664917e636f62349c6e85b4;hpb=67e001d097c99bab1fb2f26319736e79ab275b7b;p=zxing.git diff --git a/core/src/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java b/core/src/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java index 890a668b..d5b0394b 100644 --- a/core/src/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java +++ b/core/src/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java @@ -18,6 +18,10 @@ package com.google.zxing.datamatrix.decoder; import com.google.zxing.ReaderException; import com.google.zxing.common.BitSource; +import com.google.zxing.common.DecoderResult; + +import java.util.Vector; +import java.io.UnsupportedEncodingException; /** *

Data Matrix Codes can encode text as bits in one of several modes, and can use multiple modes @@ -26,7 +30,7 @@ import com.google.zxing.common.BitSource; *

See ISO 16022:2006, 5.2.1 - 5.2.9.2

* * @author bbrown@google.com (Brian Brown) - * @author srowen@google.com (Sean Owen) + * @author Sean Owen */ final class DecodedBitStreamParser { @@ -71,10 +75,11 @@ final class DecodedBitStreamParser { private DecodedBitStreamParser() { } - static String decode(byte[] bytes) throws ReaderException { + static DecoderResult decode(byte[] bytes) throws ReaderException { BitSource bits = new BitSource(bytes); StringBuffer result = new StringBuffer(); StringBuffer resultTrailer = new StringBuffer(0); + Vector byteSegments = new Vector(1); int mode = ASCII_ENCODE; do { if (mode == ASCII_ENCODE) { @@ -94,7 +99,7 @@ final class DecodedBitStreamParser { decodeEdifactSegment(bits, result); break; case BASE256_ENCODE: - decodeBase256Segment(bits, result); + decodeBase256Segment(bits, result, byteSegments); break; default: throw new ReaderException("Unsupported mode indicator"); @@ -105,7 +110,7 @@ final class DecodedBitStreamParser { if (resultTrailer.length() > 0) { result.append(resultTrailer); } - return result.toString(); + return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments); } /** @@ -411,7 +416,7 @@ final class DecodedBitStreamParser { /** * See ISO 16022:2006, 5.2.9 and Annex B, B.2 */ - private static void decodeBase256Segment(BitSource bits, StringBuffer result) { + private static void decodeBase256Segment(BitSource bits, StringBuffer result, Vector byteSegments) { // Figure out how long the Base 256 Segment is. int d1 = bits.readBits(8); int count; @@ -422,23 +427,26 @@ final class DecodedBitStreamParser { } else { count = 250 * (d1 - 249) + bits.readBits(8); } + byte[] bytes = new byte[count]; for (int i = 0; i < count; i++) { - result.append(unrandomize255State(bits.readBits(8), count)); + bytes[i] = unrandomize255State(bits.readBits(8), i); + } + byteSegments.addElement(bytes); + try { + result.append(new String(bytes, "ISO8859_1")); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("Platform does not support required encoding: " + uee); } } /** * See ISO 16022:2006, Annex B, B.2 */ - private static char unrandomize255State(int randomizedBase256Codeword, + private static byte unrandomize255State(int randomizedBase256Codeword, int base256CodewordPosition) { int pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1; int tempVariable = randomizedBase256Codeword - pseudoRandomNumber; - if (tempVariable >= 0) { - return (char) tempVariable; - } else { - return (char) (tempVariable + 256); - } + return (byte) (tempVariable >= 0 ? tempVariable : (tempVariable + 256)); } }