Merge pull request #591 from tanelihuuskonen/master

Aztec decoder returns raw bytes
This commit is contained in:
Sean Owen 2016-05-05 21:28:33 +01:00
commit 7a4e0935b5
2 changed files with 50 additions and 2 deletions

View file

@ -75,8 +75,9 @@ public final class Decoder {
BitMatrix matrix = detectorResult.getBits();
boolean[] rawbits = extractBits(matrix);
boolean[] correctedBits = correctBits(rawbits);
byte[] rawBytes = convertBoolArrayToByteArray(correctedBits);
String result = getEncodedData(correctedBits);
return new DecoderResult(null, result, null, null);
return new DecoderResult(rawBytes, result, null, null);
}
// This method is used for testing the high-level encoder
@ -332,6 +333,28 @@ public final class Decoder {
return res;
}
/**
* Reads a code of length 8 in an array of bits, padding with zeros
*/
private static byte readByte(boolean[] rawbits, int startIndex) {
int n = rawbits.length - startIndex;
if (n >= 8) {
return (byte) readCode(rawbits, startIndex, 8);
}
return (byte) (readCode(rawbits, startIndex, n) << (8 - n));
}
/**
* Packs a bit array into bytes, most significant bit first
*/
static byte[] convertBoolArrayToByteArray(boolean[] boolArr) {
byte[] byteArr = new byte[(boolArr.length + 7) / 8];
for (int i = 0; i < byteArr.length; i++) {
byteArr[i] = readByte(boolArr, 8 * i);
}
return byteArr;
}
private static int totalBitsInLayer(int layers, boolean compact) {
return ((compact ? 88 : 112) + 16 * layers) * layers;
}

View file

@ -20,8 +20,9 @@ import com.google.zxing.ResultPoint;
import com.google.zxing.aztec.AztecDetectorResult;
import com.google.zxing.common.BitMatrix;
import org.junit.Test;
import org.junit.Assert;
public final class DecoderTest {
public final class DecoderTest extends Assert {
private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
@ -105,4 +106,28 @@ public final class DecoderTest {
new Decoder().decode(r);
}
@Test
public void testRawBytes() {
boolean[] bool0 = {};
boolean[] bool1 = { true };
boolean[] bool7 = { true, false, true, false, true, false, true };
boolean[] bool8 = { true, false, true, false, true, false, true, false };
boolean[] bool9 = { true, false, true, false, true, false, true, false,
true };
boolean[] bool16 = { false, true, true, false, false, false, true, true,
true, true, false, false, false, false, false, true };
byte[] byte0 = {};
byte[] byte1 = { -128 };
byte[] byte7 = { -86 };
byte[] byte8 = { -86 };
byte[] byte9 = { -86, -128 };
byte[] byte16 = { 99, -63 };
assertArrayEquals(byte0, Decoder.convertBoolArrayToByteArray(bool0));
assertArrayEquals(byte1, Decoder.convertBoolArrayToByteArray(bool1));
assertArrayEquals(byte7, Decoder.convertBoolArrayToByteArray(bool7));
assertArrayEquals(byte8, Decoder.convertBoolArrayToByteArray(bool8));
assertArrayEquals(byte9, Decoder.convertBoolArrayToByteArray(bool9));
assertArrayEquals(byte16, Decoder.convertBoolArrayToByteArray(bool16));
}
}