Improved notion of pattern variance in 1D barcode elements, improving decode accuracy slightly. Also fixed a small issue in Code 128 decoder.

git-svn-id: https://zxing.googlecode.com/svn/trunk@211 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-02-19 17:29:16 +00:00
parent 69c53f63c7
commit f363480487
2 changed files with 15 additions and 9 deletions

View file

@ -116,9 +116,8 @@ public abstract class AbstractOneDReader implements OneDReader {
/** /**
* Determines how closely a set of observed counts of runs of black/white values matches a given * Determines how closely a set of observed counts of runs of black/white values matches a given
* target pattern. For each counter, the ratio of the difference between it and the pattern value * target pattern. This is reported as the ratio of the total variance from the expected pattern proportions
* is compared to the expected pattern value. This ratio is averaged across counters to produce * across all pattern elements, to the length of the pattern.
* the return value. 0.0 means an exact match; higher values mean poorer matches.
* *
* @param counters observed counters * @param counters observed counters
* @param pattern expected pattern * @param pattern expected pattern
@ -139,9 +138,9 @@ public abstract class AbstractOneDReader implements OneDReader {
float scaledCounter = (float) counters[x] / unitBarWidth; float scaledCounter = (float) counters[x] / unitBarWidth;
float width = pattern[x]; float width = pattern[x];
float abs = scaledCounter > width ? scaledCounter - width : width - scaledCounter; float abs = scaledCounter > width ? scaledCounter - width : width - scaledCounter;
totalVariance += abs / width; totalVariance += abs;
} }
return totalVariance / (float) numCounters; return totalVariance / (float) patternLength;
} }
/** /**

View file

@ -139,7 +139,7 @@ public final class Code128Reader extends AbstractOneDReader {
{2, 3, 3, 1, 1, 1, 2} {2, 3, 3, 1, 1, 1, 2}
}; };
private static final float MAX_VARIANCE = 0.4f; private static final float MAX_VARIANCE = 0.3f;
private static final int CODE_SHIFT = 98; private static final int CODE_SHIFT = 98;
@ -180,11 +180,18 @@ public final class Code128Reader extends AbstractOneDReader {
counters[counterPosition]++; counters[counterPosition]++;
} else { } else {
if (counterPosition == patternLength - 1) { if (counterPosition == patternLength - 1) {
float bestVariance = MAX_VARIANCE;
int bestMatch = -1;
for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) { for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
if (patternMatchVariance(counters, CODE_PATTERNS[startCode]) < MAX_VARIANCE) { float variance = patternMatchVariance(counters, CODE_PATTERNS[startCode]);
return new int[]{patternStart, i, startCode}; if (variance < bestVariance) {
bestVariance = variance;
bestMatch = startCode;
} }
} }
if (bestMatch >= 0) {
return new int[]{patternStart, i, bestMatch};
}
patternStart += counters[0] + counters[1]; patternStart += counters[0] + counters[1];
for (int y = 2; y < patternLength; y++) { for (int y = 2; y < patternLength; y++) {
counters[y - 2] = counters[y]; counters[y - 2] = counters[y];
@ -202,7 +209,7 @@ public final class Code128Reader extends AbstractOneDReader {
private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException { private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
recordPattern(row, rowOffset, counters); recordPattern(row, rowOffset, counters);
float bestVariance = 0.4f; // worst variance we'll accept float bestVariance = MAX_VARIANCE; // worst variance we'll accept
int bestMatch = -1; int bestMatch = -1;
for (int d = 0; d < CODE_PATTERNS.length; d++) { for (int d = 0; d < CODE_PATTERNS.length; d++) {
int[] pattern = CODE_PATTERNS[d]; int[] pattern = CODE_PATTERNS[d];