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;
/**
* <p>Data Matrix Codes can encode text as bits in one of several modes, and can use multiple modes
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) {
decodeEdifactSegment(bits, result);
break;
case BASE256_ENCODE:
- decodeBase256Segment(bits, result);
+ decodeBase256Segment(bits, result, byteSegments);
break;
default:
throw new ReaderException("Unsupported mode indicator");
if (resultTrailer.length() > 0) {
result.append(resultTrailer);
}
- return result.toString();
+ return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments);
}
/**
/**
* 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;
} 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));
}
}