mirror of
https://github.com/zxing/zxing.git
synced 2025-01-13 20:27:34 -08:00
Some formatting changes, and a few tiny optimizations
git-svn-id: https://zxing.googlecode.com/svn/trunk@767 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
7f5c93a71d
commit
36e9361184
|
@ -26,335 +26,307 @@ import com.google.zxing.common.GenericResultPoint;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*<p>
|
* <p>Implements decoding of the ITF format.</p>
|
||||||
* Implements decoding of the ITF format.
|
*
|
||||||
* </p>
|
* <p>"ITF" stands for Interleaved Two of Five. This Reader will scan ITF barcode with 6, 10 or 14 digits.
|
||||||
*<p>
|
* The checksum is optional and is not applied by this Reader. The consumer of the decoded value
|
||||||
* "ITF" stands for Interleaved Two of Five. This Reader will scan ITF barcode with 6, 10 or 14 digits.
|
* will have to apply a checksum if required.</p>
|
||||||
* The checksum is optional and is not applied by this Reader. The consumer of the decoded value will have to apply a checksum if
|
*
|
||||||
* required.
|
* <p><a href="http://en.wikipedia.org/wiki/Interleaved_2_of_5">http://en.wikipedia.org/wiki/Interleaved_2_of_5</a>
|
||||||
* </p>
|
* is a great reference for Interleaved 2 of 5 information.</p>
|
||||||
*
|
*
|
||||||
*<p>
|
|
||||||
* <a
|
|
||||||
* href="http://en.wikipedia.org/wiki/Interleaved_2_of_5">http://en.wikipedia.
|
|
||||||
* org/wiki/Interleaved_2_of_5</a> is a great reference for Interleaved 2 of 5
|
|
||||||
* information.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @author kevin.osullivan@sita.aero, SITA Lab.
|
* @author kevin.osullivan@sita.aero, SITA Lab.
|
||||||
*/
|
*/
|
||||||
public class ITFReader extends AbstractOneDReader {
|
public final class ITFReader extends AbstractOneDReader {
|
||||||
|
|
||||||
private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
|
private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
|
||||||
private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f);
|
private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f);
|
||||||
|
|
||||||
private static final int W = 3; // Pixel width of a wide line
|
private static final int W = 3; // Pixel width of a wide line
|
||||||
private static final int N = 1; // Pixed width of a narrow line
|
private static final int N = 1; // Pixed width of a narrow line
|
||||||
|
|
||||||
// Stores the actual narrow line width of the image being decoded.
|
// Stores the actual narrow line width of the image being decoded.
|
||||||
private int narrowLineWidth = -1;
|
private int narrowLineWidth = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start/end guard pattern.
|
* Start/end guard pattern.
|
||||||
*
|
*
|
||||||
* Note: The end pattern is reversed because the row is reversed before
|
* Note: The end pattern is reversed because the row is reversed before
|
||||||
* searching for the END_PATTERN
|
* searching for the END_PATTERN
|
||||||
*/
|
*/
|
||||||
private static final int[] START_PATTERN = { N, N, N, N };
|
private static final int[] START_PATTERN = {N, N, N, N};
|
||||||
private static final int[] END_PATTERN_REVERSED = { N, N, W };
|
private static final int[] END_PATTERN_REVERSED = {N, N, W};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Patterns of Wide / Narrow lines to indicate each digit
|
* Patterns of Wide / Narrow lines to indicate each digit
|
||||||
*/
|
*/
|
||||||
static final int[][] PATTERNS = { { N, N, W, W, N }, // 0
|
static final int[][] PATTERNS = {
|
||||||
{ W, N, N, N, W }, // 1
|
{N, N, W, W, N}, // 0
|
||||||
{ N, W, N, N, W }, // 2
|
{W, N, N, N, W}, // 1
|
||||||
{ W, W, N, N, N }, // 3
|
{N, W, N, N, W}, // 2
|
||||||
{ N, N, W, N, W }, // 4
|
{W, W, N, N, N}, // 3
|
||||||
{ W, N, W, N, N }, // 5
|
{N, N, W, N, W}, // 4
|
||||||
{ N, W, W, N, N }, // 6
|
{W, N, W, N, N}, // 5
|
||||||
{ N, N, N, W, W }, // 7
|
{N, W, W, N, N}, // 6
|
||||||
{ W, N, N, W, N }, // 8
|
{N, N, N, W, W}, // 7
|
||||||
{ N, W, N, W, N } // 9
|
{W, N, N, W, N}, // 8
|
||||||
};
|
{N, W, N, W, N} // 9
|
||||||
|
};
|
||||||
|
|
||||||
public final Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
|
public final Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
|
||||||
|
|
||||||
StringBuffer result = new StringBuffer(20);
|
StringBuffer result = new StringBuffer(20);
|
||||||
|
|
||||||
/**
|
// Find out where the Middle section (payload) starts & ends
|
||||||
* Find out where the Middle section (payload) starts & ends
|
int[] startRange = decodeStart(row);
|
||||||
*/
|
int[] endRange = decodeEnd(row);
|
||||||
int[] startRange = decodeStart(row);
|
|
||||||
int[] endRange = decodeEnd(row);
|
|
||||||
|
|
||||||
decodeMiddle(row, startRange[1], endRange[0], result);
|
decodeMiddle(row, startRange[1], endRange[0], result);
|
||||||
|
|
||||||
String resultString = result.toString();
|
String resultString = result.toString();
|
||||||
/**
|
|
||||||
* To avoid false positives with 2D barcodes (and other patterns), make
|
|
||||||
* an assumption that the decoded string must be 6, 10 or 14 digits.
|
|
||||||
*/
|
|
||||||
if ((resultString.length() != 6 && resultString.length() != 10 && resultString.length() != 14) ||
|
|
||||||
resultString.length() % 2 == 1)
|
|
||||||
throw ReaderException.getInstance();
|
|
||||||
|
|
||||||
return new Result(resultString,
|
// To avoid false positives with 2D barcodes (and other patterns), make
|
||||||
null, // no natural byte representation for these barcodes
|
// an assumption that the decoded string must be 6, 10 or 14 digits.
|
||||||
new ResultPoint[] { new GenericResultPoint(startRange[1], (float) rowNumber), new GenericResultPoint(startRange[0], (float) rowNumber) },
|
int length = resultString.length();
|
||||||
BarcodeFormat.ITF);
|
if (length != 6 && length != 10 && length != 14) {
|
||||||
}
|
throw ReaderException.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return new Result(
|
||||||
* @param row
|
resultString,
|
||||||
* row of black/white values to search
|
null, // no natural byte representation for these barcodes
|
||||||
* @param payloadStart
|
new ResultPoint[] { new GenericResultPoint(startRange[1], (float) rowNumber),
|
||||||
* offset of start pattern
|
new GenericResultPoint(startRange[0], (float) rowNumber)},
|
||||||
* @param resultString
|
BarcodeFormat.ITF);
|
||||||
* {@link StringBuffer} to append decoded chars to
|
}
|
||||||
* @throws ReaderException
|
|
||||||
* if decoding could not complete successfully
|
|
||||||
*/
|
|
||||||
protected void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException {
|
|
||||||
|
|
||||||
// Digits are interleaved in pairs - 5 black lines for one digit, and the
|
/**
|
||||||
// 5
|
* @param row row of black/white values to search
|
||||||
// interleaved white lines for the second digit.
|
* @param payloadStart offset of start pattern
|
||||||
// Therefore, need to scan 10 lines and then
|
* @param resultString {@link StringBuffer} to append decoded chars to
|
||||||
// split these into two arrays
|
* @throws ReaderException if decoding could not complete successfully
|
||||||
int[] counterDigitPair = new int[10];
|
*/
|
||||||
int[] counterBlack = new int[5];
|
protected void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException {
|
||||||
int[] counterWhite = new int[5];
|
|
||||||
|
|
||||||
for (int x = 0; payloadStart < payloadEnd; x++) {
|
// Digits are interleaved in pairs - 5 black lines for one digit, and the
|
||||||
|
// 5
|
||||||
|
// interleaved white lines for the second digit.
|
||||||
|
// Therefore, need to scan 10 lines and then
|
||||||
|
// split these into two arrays
|
||||||
|
int[] counterDigitPair = new int[10];
|
||||||
|
int[] counterBlack = new int[5];
|
||||||
|
int[] counterWhite = new int[5];
|
||||||
|
|
||||||
// Get 10 runs of black/white.
|
while (payloadStart < payloadEnd) {
|
||||||
recordPattern(row, payloadStart, counterDigitPair);
|
|
||||||
// Split them into each array
|
|
||||||
for (int k = 0; k < 5; k++) {
|
|
||||||
counterBlack[k] = counterDigitPair[k * 2];
|
|
||||||
counterWhite[k] = counterDigitPair[(k * 2) + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
int bestMatch = decodeDigit(counterBlack);
|
// Get 10 runs of black/white.
|
||||||
resultString.append((char) ('0' + bestMatch % 10));
|
recordPattern(row, payloadStart, counterDigitPair);
|
||||||
bestMatch = decodeDigit(counterWhite);
|
// Split them into each array
|
||||||
resultString.append((char) ('0' + bestMatch % 10));
|
for (int k = 0; k < 5; k++) {
|
||||||
|
int twoK = k << 1;
|
||||||
|
counterBlack[k] = counterDigitPair[twoK];
|
||||||
|
counterWhite[k] = counterDigitPair[twoK + 1];
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < counterDigitPair.length; i++) {
|
int bestMatch = decodeDigit(counterBlack);
|
||||||
payloadStart += counterDigitPair[i];
|
resultString.append((char) ('0' + bestMatch % 10));
|
||||||
}
|
bestMatch = decodeDigit(counterWhite);
|
||||||
}
|
resultString.append((char) ('0' + bestMatch % 10));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
for (int i = 0; i < counterDigitPair.length; i++) {
|
||||||
* Identify where the start of the middle / payload section starts.
|
payloadStart += counterDigitPair[i];
|
||||||
*
|
}
|
||||||
* @param row
|
}
|
||||||
* row of black/white values to search
|
}
|
||||||
* @return Array, containing index of start of 'start block' and end of
|
|
||||||
* 'start block'
|
|
||||||
* @throws ReaderException
|
|
||||||
*/
|
|
||||||
int[] decodeStart(BitArray row) throws ReaderException {
|
|
||||||
int endStart = skipWhiteSpace(row);
|
|
||||||
int startPattern[] = findGuardPattern(row, endStart, START_PATTERN);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the width of a narrow line in pixels. We can do this by
|
* Identify where the start of the middle / payload section starts.
|
||||||
* getting the width of the start pattern and dividing by 4 because its
|
*
|
||||||
* made up of 4 narrow lines.
|
* @param row row of black/white values to search
|
||||||
*/
|
* @return Array, containing index of start of 'start block' and end of
|
||||||
this.narrowLineWidth = (startPattern[1] - startPattern[0]) / 4;
|
* 'start block'
|
||||||
|
* @throws ReaderException
|
||||||
|
*/
|
||||||
|
int[] decodeStart(BitArray row) throws ReaderException {
|
||||||
|
int endStart = skipWhiteSpace(row);
|
||||||
|
int startPattern[] = findGuardPattern(row, endStart, START_PATTERN);
|
||||||
|
|
||||||
validateQuietZone(row, startPattern[0]);
|
// Determine the width of a narrow line in pixels. We can do this by
|
||||||
|
// getting the width of the start pattern and dividing by 4 because its
|
||||||
|
// made up of 4 narrow lines.
|
||||||
|
this.narrowLineWidth = (startPattern[1] - startPattern[0]) >> 2;
|
||||||
|
|
||||||
return startPattern;
|
validateQuietZone(row, startPattern[0]);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return startPattern;
|
||||||
*
|
}
|
||||||
* The start & end patterns must be pre/post fixed by a quiet zone. This
|
|
||||||
* zone must be at least 10 times the width of a narrow line. Scan back until
|
|
||||||
* we either get to the start of the barcode or match the necessary number of
|
|
||||||
* quiet zone pixels.
|
|
||||||
*
|
|
||||||
* Note: Its assumed the row is reversed when using this method to find
|
|
||||||
* quiet zone after the end pattern.
|
|
||||||
*
|
|
||||||
* ref: http://www.barcode-1.net/i25code.html
|
|
||||||
*
|
|
||||||
* @param row - The bit array representing the scanned barcode.
|
|
||||||
* @param startPattern - The index into row of the start or end pattern.
|
|
||||||
* @throws ReaderException - If the quiet zone cannot be found, a ReaderException is thrown.
|
|
||||||
*/
|
|
||||||
private void validateQuietZone(BitArray row, int startPattern) throws ReaderException {
|
|
||||||
|
|
||||||
int quietCount=this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone
|
/**
|
||||||
|
* The start & end patterns must be pre/post fixed by a quiet zone. This
|
||||||
int i=0;
|
* zone must be at least 10 times the width of a narrow line. Scan back until
|
||||||
for (i=startPattern-1; quietCount>0 && i>=0; i--)
|
* we either get to the start of the barcode or match the necessary number of
|
||||||
{
|
* quiet zone pixels.
|
||||||
if (row.get(i)==true)
|
*
|
||||||
break;
|
* Note: Its assumed the row is reversed when using this method to find
|
||||||
quietCount--;
|
* quiet zone after the end pattern.
|
||||||
}
|
*
|
||||||
if (quietCount!=0)
|
* ref: http://www.barcode-1.net/i25code.html
|
||||||
{
|
*
|
||||||
// Unable to find the necessary number of quiet zone pixels.
|
* @param row bit array representing the scanned barcode.
|
||||||
throw ReaderException.getInstance();
|
* @param startPattern index into row of the start or end pattern.
|
||||||
}
|
* @throws ReaderException if the quiet zone cannot be found, a ReaderException is thrown.
|
||||||
}
|
*/
|
||||||
|
private void validateQuietZone(BitArray row, int startPattern) throws ReaderException {
|
||||||
|
|
||||||
/**
|
int quietCount = this.narrowLineWidth * 10; // expect to find this many pixels of quiet zone
|
||||||
* Skip all whitespace until we get to the first black line.
|
|
||||||
*
|
|
||||||
* @param row
|
|
||||||
* row of black/white values to search
|
|
||||||
* @return index of the first black line.
|
|
||||||
* @throws ReaderException
|
|
||||||
* Throws exception if no black lines are found in the row
|
|
||||||
*/
|
|
||||||
private int skipWhiteSpace(BitArray row) throws ReaderException {
|
|
||||||
int width = row.getSize();
|
|
||||||
int endStart = 0;
|
|
||||||
while (endStart < width) {
|
|
||||||
if (row.get(endStart)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
endStart++;
|
|
||||||
}
|
|
||||||
if (endStart == width)
|
|
||||||
throw ReaderException.getInstance();
|
|
||||||
|
|
||||||
return endStart;
|
for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) {
|
||||||
}
|
if (row.get(i)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
quietCount--;
|
||||||
|
}
|
||||||
|
if (quietCount != 0) {
|
||||||
|
// Unable to find the necessary number of quiet zone pixels.
|
||||||
|
throw ReaderException.getInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identify where the end of the middle / payload section ends.
|
* Skip all whitespace until we get to the first black line.
|
||||||
*
|
*
|
||||||
* @param row
|
* @param row row of black/white values to search
|
||||||
* row of black/white values to search
|
* @return index of the first black line.
|
||||||
* @return Array, containing index of start of 'end block' and end of 'end
|
* @throws ReaderException Throws exception if no black lines are found in the row
|
||||||
* block'
|
*/
|
||||||
* @throws ReaderException
|
private int skipWhiteSpace(BitArray row) throws ReaderException {
|
||||||
*/
|
int width = row.getSize();
|
||||||
|
int endStart = 0;
|
||||||
|
while (endStart < width) {
|
||||||
|
if (row.get(endStart)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
endStart++;
|
||||||
|
}
|
||||||
|
if (endStart == width) {
|
||||||
|
throw ReaderException.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
int[] decodeEnd(BitArray row) throws ReaderException {
|
return endStart;
|
||||||
|
}
|
||||||
|
|
||||||
// For convenience, reverse the row and then
|
/**
|
||||||
// search from 'the start' for the end block
|
* Identify where the end of the middle / payload section ends.
|
||||||
row.reverse();
|
*
|
||||||
|
* @param row row of black/white values to search
|
||||||
|
* @return Array, containing index of start of 'end block' and end of 'end
|
||||||
|
* block'
|
||||||
|
* @throws ReaderException
|
||||||
|
*/
|
||||||
|
|
||||||
int endStart = skipWhiteSpace(row);
|
int[] decodeEnd(BitArray row) throws ReaderException {
|
||||||
int endPattern[] = null;
|
|
||||||
try {
|
|
||||||
endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED);
|
|
||||||
} catch (ReaderException e) {
|
|
||||||
// Put our row of data back the right way before throwing
|
|
||||||
row.reverse();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// For convenience, reverse the row and then
|
||||||
* The start & end patterns must be pre/post fixed by a quiet zone. This
|
// search from 'the start' for the end block
|
||||||
* zone must be at least 10 times the width of a narrow line.
|
row.reverse();
|
||||||
*
|
|
||||||
* ref: http://www.barcode-1.net/i25code.html
|
|
||||||
*/
|
|
||||||
|
|
||||||
validateQuietZone(row, endPattern[0]);
|
|
||||||
|
|
||||||
// Now recalc the indicies of where the 'endblock' starts & stops to
|
int endStart = skipWhiteSpace(row);
|
||||||
// accomodate
|
int endPattern[];
|
||||||
// the reversed nature of the search
|
try {
|
||||||
int temp = endPattern[0];
|
endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED);
|
||||||
endPattern[0] = row.getSize() - endPattern[1];
|
} catch (ReaderException e) {
|
||||||
endPattern[1] = row.getSize() - temp;
|
// Put our row of data back the right way before throwing
|
||||||
|
row.reverse();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
// Put the row back the righ way.
|
// The start & end patterns must be pre/post fixed by a quiet zone. This
|
||||||
row.reverse();
|
// zone must be at least 10 times the width of a narrow line.
|
||||||
return endPattern;
|
// ref: http://www.barcode-1.net/i25code.html
|
||||||
}
|
validateQuietZone(row, endPattern[0]);
|
||||||
|
|
||||||
/**
|
// Now recalc the indicies of where the 'endblock' starts & stops to
|
||||||
* @param row
|
// accomodate
|
||||||
* row of black/white values to search
|
// the reversed nature of the search
|
||||||
* @param rowOffset
|
int temp = endPattern[0];
|
||||||
* position to start search
|
endPattern[0] = row.getSize() - endPattern[1];
|
||||||
* @param pattern
|
endPattern[1] = row.getSize() - temp;
|
||||||
* pattern of counts of number of black and white pixels that are
|
|
||||||
* being searched for as a pattern
|
|
||||||
* @return start/end horizontal offset of guard pattern, as an array of two
|
|
||||||
* ints
|
|
||||||
* @throws ReaderException
|
|
||||||
* if pattern is not found
|
|
||||||
*
|
|
||||||
* TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be merged to
|
|
||||||
* a single method.
|
|
||||||
*/
|
|
||||||
int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException {
|
|
||||||
int patternLength = pattern.length;
|
|
||||||
int[] counters = new int[patternLength];
|
|
||||||
int width = row.getSize();
|
|
||||||
boolean isWhite = false;
|
|
||||||
|
|
||||||
int counterPosition = 0;
|
// Put the row back the righ way.
|
||||||
int patternStart = rowOffset;
|
row.reverse();
|
||||||
for (int x = rowOffset; x < width; x++) {
|
return endPattern;
|
||||||
boolean pixel = row.get(x);
|
}
|
||||||
if ((!pixel && isWhite) || (pixel && !isWhite)) {
|
|
||||||
counters[counterPosition]++;
|
|
||||||
} else {
|
|
||||||
if (counterPosition == patternLength - 1) {
|
|
||||||
if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
|
|
||||||
return new int[] { patternStart, x };
|
|
||||||
}
|
|
||||||
patternStart += counters[0] + counters[1];
|
|
||||||
for (int y = 2; y < patternLength; y++) {
|
|
||||||
counters[y - 2] = counters[y];
|
|
||||||
}
|
|
||||||
counters[patternLength - 2] = 0;
|
|
||||||
counters[patternLength - 1] = 0;
|
|
||||||
counterPosition--;
|
|
||||||
} else {
|
|
||||||
counterPosition++;
|
|
||||||
}
|
|
||||||
counters[counterPosition] = 1;
|
|
||||||
isWhite = !isWhite;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw ReaderException.getInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to decode a sequence of ITF black/white lines into single
|
* @param row row of black/white values to search
|
||||||
* digit.
|
* @param rowOffset position to start search
|
||||||
*
|
* @param pattern pattern of counts of number of black and white pixels that are
|
||||||
* @param counters
|
* being searched for as a pattern
|
||||||
* the counts of runs of observed black/white/black/... values
|
* @return start/end horizontal offset of guard pattern, as an array of two
|
||||||
*
|
* ints
|
||||||
* @return The decoded digit
|
* @throws ReaderException if pattern is not found
|
||||||
*
|
*/
|
||||||
* @throws ReaderException
|
int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException {
|
||||||
* if digit cannot be decoded
|
|
||||||
*/
|
|
||||||
static int decodeDigit(int[] counters) throws ReaderException {
|
|
||||||
|
|
||||||
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
|
// TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be merged to
|
||||||
int bestMatch = -1;
|
// a single method.
|
||||||
int max = PATTERNS.length;
|
|
||||||
for (int i = 0; i < max; i++) {
|
int patternLength = pattern.length;
|
||||||
int[] pattern = PATTERNS[i];
|
int[] counters = new int[patternLength];
|
||||||
int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
|
int width = row.getSize();
|
||||||
if (variance < bestVariance) {
|
boolean isWhite = false;
|
||||||
bestVariance = variance;
|
|
||||||
bestMatch = i;
|
int counterPosition = 0;
|
||||||
}
|
int patternStart = rowOffset;
|
||||||
}
|
for (int x = rowOffset; x < width; x++) {
|
||||||
if (bestMatch >= 0) {
|
boolean pixel = row.get(x);
|
||||||
return bestMatch;
|
if ((!pixel && isWhite) || (pixel && !isWhite)) {
|
||||||
|
counters[counterPosition]++;
|
||||||
|
} else {
|
||||||
|
if (counterPosition == patternLength - 1) {
|
||||||
|
if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
|
||||||
|
return new int[]{patternStart, x};
|
||||||
|
}
|
||||||
|
patternStart += counters[0] + counters[1];
|
||||||
|
for (int y = 2; y < patternLength; y++) {
|
||||||
|
counters[y - 2] = counters[y];
|
||||||
|
}
|
||||||
|
counters[patternLength - 2] = 0;
|
||||||
|
counters[patternLength - 1] = 0;
|
||||||
|
counterPosition--;
|
||||||
|
} else {
|
||||||
|
counterPosition++;
|
||||||
|
}
|
||||||
|
counters[counterPosition] = 1;
|
||||||
|
isWhite = !isWhite;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw ReaderException.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to decode a sequence of ITF black/white lines into single
|
||||||
|
* digit.
|
||||||
|
*
|
||||||
|
* @param counters the counts of runs of observed black/white/black/... values
|
||||||
|
* @return The decoded digit
|
||||||
|
* @throws ReaderException if digit cannot be decoded
|
||||||
|
*/
|
||||||
|
static int decodeDigit(int[] counters) throws ReaderException {
|
||||||
|
|
||||||
|
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
|
||||||
|
int bestMatch = -1;
|
||||||
|
int max = PATTERNS.length;
|
||||||
|
for (int i = 0; i < max; i++) {
|
||||||
|
int[] pattern = PATTERNS[i];
|
||||||
|
int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
|
||||||
|
if (variance < bestVariance) {
|
||||||
|
bestVariance = variance;
|
||||||
|
bestMatch = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bestMatch >= 0) {
|
||||||
|
return bestMatch;
|
||||||
} else {
|
} else {
|
||||||
throw ReaderException.getInstance();
|
throw ReaderException.getInstance();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue