Miscellaneous changes from FindBugs analysis

git-svn-id: https://zxing.googlecode.com/svn/trunk@1038 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-08-22 15:16:36 +00:00
parent 9045bd2409
commit 362236564e
3 changed files with 48 additions and 65 deletions

View file

@ -67,13 +67,13 @@ public final class CharacterSetECI extends ECI {
private static void addCharacterSet(int value, String encodingName) { private static void addCharacterSet(int value, String encodingName) {
CharacterSetECI eci = new CharacterSetECI(value, encodingName); CharacterSetECI eci = new CharacterSetECI(value, encodingName);
VALUE_TO_ECI.put(new Integer(value), eci); VALUE_TO_ECI.put(Integer.valueOf(value), eci);
NAME_TO_ECI.put(encodingName, eci); NAME_TO_ECI.put(encodingName, eci);
} }
private static void addCharacterSet(int value, String[] encodingNames) { private static void addCharacterSet(int value, String[] encodingNames) {
CharacterSetECI eci = new CharacterSetECI(value, encodingNames[0]); CharacterSetECI eci = new CharacterSetECI(value, encodingNames[0]);
VALUE_TO_ECI.put(new Integer(value), eci); VALUE_TO_ECI.put(Integer.valueOf(value), eci);
for (int i = 0; i < encodingNames.length; i++) { for (int i = 0; i < encodingNames.length; i++) {
NAME_TO_ECI.put(encodingNames[i], eci); NAME_TO_ECI.put(encodingNames[i], eci);
} }
@ -92,7 +92,7 @@ public final class CharacterSetECI extends ECI {
if (value < 0 || value >= 900) { if (value < 0 || value >= 900) {
throw new IllegalArgumentException("Bad ECI value: " + value); throw new IllegalArgumentException("Bad ECI value: " + value);
} }
return (CharacterSetECI) VALUE_TO_ECI.get(new Integer(value)); return (CharacterSetECI) VALUE_TO_ECI.get(Integer.valueOf(value));
} }
/** /**

View file

@ -42,7 +42,7 @@ public final class Detector {
// Trick to avoid creating new Integer objects below -- a sort of crude copy of // Trick to avoid creating new Integer objects below -- a sort of crude copy of
// the Integer.valueOf(int) optimization added in Java 5, not in J2ME // the Integer.valueOf(int) optimization added in Java 5, not in J2ME
private static final Integer[] INTEGERS = private static final Integer[] INTEGERS =
{ new Integer(0), new Integer(1), new Integer(2), new Integer(3), new Integer(4) }; { Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4) };
private final BitMatrix image; private final BitMatrix image;
private final MonochromeRectangleDetector rectangleDetector; private final MonochromeRectangleDetector rectangleDetector;

View file

@ -30,14 +30,14 @@ final class BitMatrixParser {
private static final int MAX_ROW_DIFFERENCE = 6; private static final int MAX_ROW_DIFFERENCE = 6;
private static final int MAX_ROWS = 90; private static final int MAX_ROWS = 90;
private static final int MAX_COLUMNS = 30; //private static final int MAX_COLUMNS = 30;
// Maximum Codewords (Data + Error) // Maximum Codewords (Data + Error)
private static final int MAX_CW_CAPACITY = 929; private static final int MAX_CW_CAPACITY = 929;
private static final int MODULES_IN_SYMBOL = 17; private static final int MODULES_IN_SYMBOL = 17;
private final BitMatrix bitMatrix; private final BitMatrix bitMatrix;
private int rows = 0; private int rows = 0;
private int columns = 0; //private int columns = 0;
private int leftColumnECData = 0; private int leftColumnECData = 0;
private int rightColumnECData = 0; private int rightColumnECData = 0;
@ -58,7 +58,7 @@ final class BitMatrixParser {
* *
* @return an array of codewords. * @return an array of codewords.
*/ */
int[] readCodewords() throws ReaderException { int[] readCodewords() {
int width = bitMatrix.getDimension(); int width = bitMatrix.getDimension();
// TODO should be a rectangular matrix // TODO should be a rectangular matrix
int height = width; int height = width;
@ -113,8 +113,7 @@ final class BitMatrixParser {
} else { } else {
if (rowInProgress) { if (rowInProgress) {
// Process Row // Process Row
next = processRow(rowCounters, rowNumber, rowHeight, next = processRow(rowCounters, rowNumber, rowHeight, codewords, next);
moduleWidth, codewords, next);
if (next == -1) { if (next == -1) {
// Something is wrong, since we have exceeded // Something is wrong, since we have exceeded
// the maximum columns in the specification. // the maximum columns in the specification.
@ -142,8 +141,7 @@ final class BitMatrixParser {
// TODO Maybe return error code // TODO Maybe return error code
return null; return null;
} }
next = processRow(rowCounters, rowNumber, rowHeight, moduleWidth, next = processRow(rowCounters, rowNumber, rowHeight, codewords, next);
codewords, next);
rowNumber++; rowNumber++;
rows = rowNumber; rows = rowNumber;
} }
@ -160,7 +158,7 @@ final class BitMatrixParser {
*/ */
private static int[] trimArray(int[] array, int size) { private static int[] trimArray(int[] array, int size) {
if (size > 0) { if (size > 0) {
int a[] = new int[size]; int[] a = new int[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
a[i] = array[i]; a[i] = array[i];
} }
@ -172,36 +170,31 @@ final class BitMatrixParser {
/** /**
* Convert the symbols in the row to codewords. * Convert the symbols in the row to codewords.
* Each PDF417 symbol character consists of four bar elements and four space
* elements, each of which can be one to six modules wide. The four bar and
* four space elements shall measure 17 modules in total.
* *
* @param rowCounters an array containing the counts of black pixels for each column * @param rowCounters an array containing the counts of black pixels for each column
* in the row. * in the row.
* @param rowNumber the current row number of codewords. * @param rowNumber the current row number of codewords.
* @param rowHeight the height of this row in pixels. * @param rowHeight the height of this row in pixels.
* @param moduleWidth the size of a module in pixels.
* @param codewords the codeword array to save codewords into. * @param codewords the codeword array to save codewords into.
* @param next the next available index into the codewords array. * @param next the next available index into the codewords array.
* @return the next available index into the codeword array after processing * @return the next available index into the codeword array after processing
* this row. * this row.
* @throws ReaderException
*/ */
/* int processRow(int[] rowCounters, int rowNumber, int rowHeight, int[] codewords, int next) {
* Each PDF417 symbol character consists of four bar elements and four space
* elements, each of which can be one to six modules wide. The four bar and
* four space elements shall measure 17 modules in total.
*/
int processRow(int[] rowCounters, int rowNumber, int rowHeight,
float moduleWidth, int[] codewords, int next) throws ReaderException {
int width = bitMatrix.getDimension(); int width = bitMatrix.getDimension();
int columnNumber = 0; int columnNumber = 0;
long symbol = 0; long symbol = 0;
for (int i = 0; i < width; i += MODULES_IN_SYMBOL) { for (int i = 0; i < width; i += MODULES_IN_SYMBOL) {
for (int mask = MODULES_IN_SYMBOL - 1; mask >= 0; mask--) { for (int mask = MODULES_IN_SYMBOL - 1; mask >= 0; mask--) {
if (rowCounters[i + (MODULES_IN_SYMBOL - 1 - mask)] >= rowHeight / 2) { if (rowCounters[i + (MODULES_IN_SYMBOL - 1 - mask)] >= rowHeight >>> 1) {
symbol |= 1 << mask; symbol |= 1 << mask;
} }
} }
if (columnNumber > 0) { if (columnNumber > 0) {
int cw = getCodeword(symbol, rowNumber); int cw = getCodeword(symbol);
// if (debug) System.out.println(" " + Long.toBinaryString(symbol) + // if (debug) System.out.println(" " + Long.toBinaryString(symbol) +
// " cw=" +cw + " ColumnNumber=" +columnNumber + "i=" +i); // " cw=" +cw + " ColumnNumber=" +columnNumber + "i=" +i);
if (cw < 0 && i < width - MODULES_IN_SYMBOL) { if (cw < 0 && i < width - MODULES_IN_SYMBOL) {
@ -214,7 +207,7 @@ final class BitMatrixParser {
} }
} else { } else {
// Left row indicator column // Left row indicator column
int cw = getCodeword(symbol, rowNumber); int cw = getCodeword(symbol);
// if (debug) System.out.println(" " + Long.toBinaryString(symbol) + // if (debug) System.out.println(" " + Long.toBinaryString(symbol) +
// " cw=" +cw + " ColumnNumber=" +columnNumber + "i=" +i); // " cw=" +cw + " ColumnNumber=" +columnNumber + "i=" +i);
if (ecLevel < 0) { if (ecLevel < 0) {
@ -230,12 +223,12 @@ final class BitMatrixParser {
} }
} }
symbol = 0; symbol = 0;
columns = columnNumber; //columns = columnNumber;
columnNumber++; columnNumber++;
} }
if (columnNumber > 1) { if (columnNumber > 1) {
// Right row indicator column is in codeword[next] // Right row indicator column is in codeword[next]
columns--; //columns--;
// Overwrite the last codeword i.e. Right Row Indicator // Overwrite the last codeword i.e. Right Row Indicator
--next; --next;
if (ecLevel < 0) { if (ecLevel < 0) {
@ -259,18 +252,16 @@ final class BitMatrixParser {
} }
/** /**
* Build a symbol from the pixels * Build a symbol from the pixels.
*
* @param counters array of pixel counter corresponding to each Bar/Space pattern.
* @param rowNumber
* @return the symbol
*/
/*
* Each symbol character is defined by an 8-digit bar-space sequence which * Each symbol character is defined by an 8-digit bar-space sequence which
* represents the module widths of the eight elements of that symbol * represents the module widths of the eight elements of that symbol
* character. * character.
*
* @param counters array of pixel counter corresponding to each Bar/Space pattern.
* @return the symbol
*/ */
private static long getSymbol(int[] counters, int rowNumber, float moduleWidth) { /*
private static long getSymbol(int[] counters, float moduleWidth) {
int pixelsInSymbol = 0; int pixelsInSymbol = 0;
for (int j = 0; j < counters.length; j++) { for (int j = 0; j < counters.length; j++) {
pixelsInSymbol += counters[j]; pixelsInSymbol += counters[j];
@ -306,29 +297,26 @@ final class BitMatrixParser {
} }
return symbol; return symbol;
} }
*/
/** /**
* Translate the symbol into a codeword. * Translate the symbol into a codeword.
* *
* @param symbol * @param symbol
* @param row
* @return the codeword corresponding to the symbol. * @return the codeword corresponding to the symbol.
* @throws ReaderException
*/ */
private int getCodeword(long symbol, int row) throws ReaderException { private int getCodeword(long symbol) {
long sym = symbol; long sym = symbol;
sym &= 0x3ffff; sym &= 0x3ffff;
int i = findCodewordIndex(sym); int i = findCodewordIndex(sym);
long cw = 0;
if (i == -1) { if (i == -1) {
return -1; return -1;
} else { } else {
cw = CODEWORD_TABLE[i] - 1; long cw = CODEWORD_TABLE[i] - 1;
cw %= 929; cw %= 929;
}
return (int) cw; return (int) cw;
} }
}
/** /**
* Use a binary search to find the index of the codeword corresponding to * Use a binary search to find the index of the codeword corresponding to
@ -337,11 +325,11 @@ final class BitMatrixParser {
* @param symbol the symbol from the barcode. * @param symbol the symbol from the barcode.
* @return the index into the codeword table. * @return the index into the codeword table.
*/ */
private int findCodewordIndex(long symbol) { private static int findCodewordIndex(long symbol) {
int first = 0; int first = 0;
int upto = SYMBOL_TABLE.length; int upto = SYMBOL_TABLE.length;
while (first < upto) { while (first < upto) {
int mid = (first + upto) / 2; // Compute mid point. int mid = (first + upto) >>> 1; // Compute mid point.
if (symbol < SYMBOL_TABLE[mid]) { if (symbol < SYMBOL_TABLE[mid]) {
upto = mid; // repeat search in bottom half. upto = mid; // repeat search in bottom half.
} else if (symbol > SYMBOL_TABLE[mid]) { } else if (symbol > SYMBOL_TABLE[mid]) {
@ -359,16 +347,11 @@ final class BitMatrixParser {
* Ends up being a bit faster than Math.round(). This merely rounds its * Ends up being a bit faster than Math.round(). This merely rounds its
* argument to the nearest int, where x.5 rounds up. * argument to the nearest int, where x.5 rounds up.
*/ */
/*
private static int round(float d) { private static int round(float d) {
return (int) (d + 0.5f); return (int) (d + 0.5f);
} }
/**
* Returns the number of codewords flagged as erasures.
*/ */
public int getEraseCount() {
return eraseCount;
}
/** /**
* Returns an array of locations representing the erasures. * Returns an array of locations representing the erasures.
@ -398,14 +381,15 @@ final class BitMatrixParser {
* this row. * this row.
* @throws ReaderException * @throws ReaderException
*/ */
/*
int processRow1(int[] rowCounters, int rowNumber, int rowHeight, int processRow1(int[] rowCounters, int rowNumber, int rowHeight,
float moduleWidth, int[] codewords, int next) throws ReaderException { float moduleWidth, int[] codewords, int next) {
int width = bitMatrix.getDimension(); int width = bitMatrix.getDimension();
int firstBlack = 0; int firstBlack = 0;
for (firstBlack = 0; firstBlack < width; firstBlack++) { for (firstBlack = 0; firstBlack < width; firstBlack++) {
// Step forward until we find the first black pixels // Step forward until we find the first black pixels
if (rowCounters[firstBlack] >= rowHeight / 2) { if (rowCounters[firstBlack] >= rowHeight >>> 1) {
break; break;
} }
} }
@ -421,7 +405,7 @@ final class BitMatrixParser {
// for // for
// black // black
// If more than half the column is black // If more than half the column is black
if (rowCounters[i] >= rowHeight / 2 || i == width - 1) { if (rowCounters[i] >= rowHeight >>> 1 || i == width - 1) {
if (i == width - 1) { if (i == width - 1) {
counters[state]++; counters[state]++;
} }
@ -435,7 +419,7 @@ final class BitMatrixParser {
counters[state]++; counters[state]++;
} }
} else { } else {
if (rowCounters[i] < rowHeight / 2) { if (rowCounters[i] < rowHeight >>> 1) {
// Found white pixels // Found white pixels
state++; state++;
if (state == 7 && i == width - 1) { if (state == 7 && i == width - 1) {
@ -462,8 +446,8 @@ final class BitMatrixParser {
return -1; return -1;
} }
if (columnNumber > 0) { if (columnNumber > 0) {
symbol = getSymbol(counters, rowNumber, moduleWidth); symbol = getSymbol(counters, moduleWidth);
int cw = getCodeword(symbol, rowNumber); int cw = getCodeword(symbol);
// if (debug) System.out.println(" " + // if (debug) System.out.println(" " +
// Long.toBinaryString(symbol) + " cw=" +cw + " ColumnNumber=" // Long.toBinaryString(symbol) + " cw=" +cw + " ColumnNumber="
// +columnNumber + "i=" +i); // +columnNumber + "i=" +i);
@ -476,8 +460,8 @@ final class BitMatrixParser {
} }
} else { } else {
// Left row indicator column // Left row indicator column
symbol = getSymbol(counters, rowNumber, moduleWidth); symbol = getSymbol(counters, moduleWidth);
int cw = getCodeword(symbol, rowNumber); int cw = getCodeword(symbol);
if (ecLevel < 0) { if (ecLevel < 0) {
switch (rowNumber % 3) { switch (rowNumber % 3) {
case 0: case 0:
@ -496,15 +480,13 @@ final class BitMatrixParser {
counters = new int[8]; counters = new int[8];
columns = columnNumber; columns = columnNumber;
columnNumber++; columnNumber++;
/* // Introduce some errors if (rowNumber == 0 && columnNumber == 4)
* // Introduce some errors if (rowNumber == 0 && columnNumber == 4) // { codewords[next-1] = 0; erasures[eraseCount] = next-1;
* { codewords[next-1] = 0; erasures[eraseCount] = next-1; // eraseCount++; } if (rowNumber == 0 && columnNumber == 6) {
* eraseCount++; } if (rowNumber == 0 && columnNumber == 6) { // codewords[next-1] = 10; erasures[eraseCount] = next-1;
* codewords[next-1] = 10; erasures[eraseCount] = next-1; // eraseCount++; } if (rowNumber == 0 && columnNumber == 8) {
* eraseCount++; } if (rowNumber == 0 && columnNumber == 8) { // codewords[next-1] = 10; erasures[eraseCount] = next-1;
* codewords[next-1] = 10; erasures[eraseCount] = next-1; // eraseCount++; }
* eraseCount++; }
*/
state = 0; state = 0;
symbol = 0; symbol = 0;
} }
@ -533,6 +515,7 @@ final class BitMatrixParser {
} }
return next; return next;
} }
*/
/** /**
* The sorted table of all possible symbols. Extracted from the PDF417 * The sorted table of all possible symbols. Extracted from the PDF417