Issue 1758 try again with better rule implementation that counts all exterior regions as white

git-svn-id: https://zxing.googlecode.com/svn/trunk@2873 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-08-19 12:15:24 +00:00
parent b5c2264d34
commit 558dad051e
5 changed files with 74 additions and 90 deletions

View file

@ -76,7 +76,6 @@ final class MaskUtil {
for (int x = 0; x < width; x++) {
byte[] arrayY = array[y]; // We can at least optimize this access
if (x + 6 < width &&
(x == 0 || arrayY[x-1] == 0) &&
arrayY[x] == 1 &&
arrayY[x + 1] == 0 &&
arrayY[x + 2] == 1 &&
@ -84,21 +83,10 @@ final class MaskUtil {
arrayY[x + 4] == 1 &&
arrayY[x + 5] == 0 &&
arrayY[x + 6] == 1 &&
(x + 7 >= width || arrayY[x+7] == 0) &&
((x + 10 < width &&
arrayY[x + 7] == 0 &&
arrayY[x + 8] == 0 &&
arrayY[x + 9] == 0 &&
arrayY[x + 10] == 0) ||
(x - 4 >= 0 &&
arrayY[x - 1] == 0 &&
arrayY[x - 2] == 0 &&
arrayY[x - 3] == 0 &&
arrayY[x - 4] == 0))) {
(isWhiteHorizontal(arrayY, x - 4, x) || isWhiteHorizontal(arrayY, x + 7, x + 11))) {
numPenalties++;
}
if (y + 6 < height &&
(y == 0 || array[y-1][x] == 0) &&
array[y][x] == 1 &&
array[y + 1][x] == 0 &&
array[y + 2][x] == 1 &&
@ -106,17 +94,7 @@ final class MaskUtil {
array[y + 4][x] == 1 &&
array[y + 5][x] == 0 &&
array[y + 6][x] == 1 &&
(y + 7 >= height || array[y+7][x] == 0) &&
((y + 10 < height &&
array[y + 7][x] == 0 &&
array[y + 8][x] == 0 &&
array[y + 9][x] == 0 &&
array[y + 10][x] == 0) ||
(y - 4 >= 0 &&
array[y - 1][x] == 0 &&
array[y - 2][x] == 0 &&
array[y - 3][x] == 0 &&
array[y - 4][x] == 0))) {
(isWhiteVertical(array, x, y - 4, y) || isWhiteVertical(array, x, y + 7, y + 11))) {
numPenalties++;
}
}
@ -124,6 +102,24 @@ final class MaskUtil {
return numPenalties * N3;
}
private static boolean isWhiteHorizontal(byte[] rowArray, int from, int to) {
for (int i = from; i < to; i++) {
if (i >= 0 && i < rowArray.length && rowArray[i] == 1) {
return false;
}
}
return true;
}
private static boolean isWhiteVertical(byte[][] array, int col, int from, int to) {
for (int i = from; i < to; i++) {
if (i >= 0 && i < array.length && array[i][col] == 1) {
return false;
}
}
return true;
}
/**
* Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
* penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 189 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 B

View file

@ -128,18 +128,6 @@ public final class QRCodeWriterTestCase extends Assert {
public void testRegressionTest() throws Exception {
compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
"renderer-test-01.png");
compareToGoldenFile("12345", ErrorCorrectionLevel.L, 58, "renderer-test-02.png");
// Test in Katakana in Shift_JIS.
// TODO: this test is bogus now that byte mode has been basically fixed to assuming ISO-8859-1 encoding
// The real solution is to implement Kanji mode, in which case the golden file will be wrong again
/*
compareToGoldenFile(
new String(new byte[] {(byte)0x83, 0x65, (byte)0x83, 0x58, (byte)0x83, 0x67}, "Shift_JIS"),
ErrorCorrectionLevel.H, 145,
"renderer-test-03.png");
*/
}
}

View file

@ -97,33 +97,33 @@ public final class EncoderTestCase extends Assert {
QRCode qrCode = Encoder.encode("ABCDEF", ErrorCorrectionLevel.H);
String expected =
"<<\n" +
" mode: ALPHANUMERIC\n" +
" ecLevel: H\n" +
" version: 1\n" +
" maskPattern: 0\n" +
" matrix:\n" +
" 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
" 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 1\n" +
" 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n" +
" 1 0 1 1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 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
" 1 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 1 1 0 1\n" +
" 1 0 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 1 1 0 1\n" +
" 1 0 0 0 0 0 1 0 1 0 0 1 1 0 1 0 0 0 0 0 1\n" +
" 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
" 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0\n" +
" 0 0 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 0 0 1 0\n" +
" 0 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 0 1 1 0 1\n" +
" 1 0 0 0 0 1 1 0 0 1 0 1 0 0 0 1 1 1 0 1 1\n" +
" 1 0 0 1 1 1 0 0 1 1 1 1 0 0 0 0 1 0 0 0 0\n" +
" 0 1 1 1 1 1 1 0 1 0 1 0 1 1 1 0 0 1 1 0 0\n" +
" 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 1\n" +
" 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 1 0 0\n" +
" 1 0 0 0 0 0 1 0 1 1 0 1 0 0 0 1 0 1 1 1 1\n" +
" 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 1 0 0 1 1\n" +
" 1 0 1 1 1 0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1\n" +
" 1 0 1 1 1 0 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0\n" +
" 1 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 0 1\n" +
" 1 1 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 0 1 1 1\n" +
">>\n";
assertEquals(expected, qrCode.toString());
}
@ -134,33 +134,33 @@ public final class EncoderTestCase extends Assert {
QRCode qrCode = Encoder.encode("hello", ErrorCorrectionLevel.H, hints);
String expected =
"<<\n" +
" m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