Another round of optimization, focused on reusing arrays and small objects.

git-svn-id: https://zxing.googlecode.com/svn/trunk@665 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-02 16:07:36 +00:00
parent 14e22bd443
commit 1dc71920e9
7 changed files with 62 additions and 13 deletions

View file

@ -84,6 +84,12 @@ public abstract class AbstractUPCEANReader extends AbstractOneDReader implements
} }
} }
private StringBuffer decodeRowStringBuffer;
public AbstractUPCEANReader() {
decodeRowStringBuffer = new StringBuffer(20);
}
static int[] findStartGuardPattern(BitArray row) throws ReaderException { static int[] findStartGuardPattern(BitArray row) throws ReaderException {
boolean foundStart = false; boolean foundStart = false;
int[] startRange = null; int[] startRange = null;
@ -108,7 +114,8 @@ public abstract class AbstractUPCEANReader extends AbstractOneDReader implements
} }
public final Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange) throws ReaderException { public final Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange) throws ReaderException {
StringBuffer result = new StringBuffer(20); StringBuffer result = decodeRowStringBuffer;
result.setLength(0);
int endStart = decodeMiddle(row, startGuardRange, result); int endStart = decodeMiddle(row, startGuardRange, result);
int[] endRange = decodeEnd(row, endStart); int[] endRange = decodeEnd(row, endStart);

View file

@ -92,9 +92,7 @@ public final class Code39Reader extends AbstractOneDReader {
public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException { public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
int[] start = findAsteriskPattern(row); int[] start = findAsteriskPattern(row);
int nextStart = start[1]; int nextStart = start[1];
int end = row.getSize(); int end = row.getSize();
// Read off white space // Read off white space

View file

@ -62,9 +62,18 @@ public final class EAN13Reader extends AbstractUPCEANReader {
0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A 0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A
}; };
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString) throws ReaderException { private int[] decodeMiddleCounters;
int[] counters = new int[4]; public EAN13Reader() {
decodeMiddleCounters = new int[4];
}
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString) throws ReaderException {
int[] counters = decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int end = row.getSize(); int end = row.getSize();
int rowOffset = startRange[1]; int rowOffset = startRange[1];

View file

@ -27,9 +27,18 @@ import com.google.zxing.common.BitArray;
*/ */
public final class EAN8Reader extends AbstractUPCEANReader { public final class EAN8Reader extends AbstractUPCEANReader {
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException { private int[] decodeMiddleCounters;
int[] counters = new int[4]; public EAN8Reader() {
decodeMiddleCounters = new int[4];
}
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException {
int[] counters = decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int end = row.getSize(); int end = row.getSize();
int rowOffset = startRange[1]; int rowOffset = startRange[1];

View file

@ -46,9 +46,18 @@ public final class UPCEReader extends AbstractUPCEANReader {
{0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A} {0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}
}; };
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException { private int[] decodeMiddleCounters;
int[] counters = new int[4]; public UPCEReader() {
decodeMiddleCounters = new int[4];
}
protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException {
int[] counters = decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
int end = row.getSize(); int end = row.getSize();
int rowOffset = startRange[1]; int rowOffset = startRange[1];

View file

@ -45,6 +45,7 @@ final class AlignmentPatternFinder {
private final int width; private final int width;
private final int height; private final int height;
private final float moduleSize; private final float moduleSize;
private final int[] crossCheckStateCount;
/** /**
* <p>Creates a finder that will look in a portion of the whole image.</p> * <p>Creates a finder that will look in a portion of the whole image.</p>
@ -69,6 +70,7 @@ final class AlignmentPatternFinder {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.moduleSize = moduleSize; this.moduleSize = moduleSize;
this.crossCheckStateCount = new int[3];
} }
/** /**
@ -188,7 +190,10 @@ final class AlignmentPatternFinder {
MonochromeBitmapSource image = this.image; MonochromeBitmapSource image = this.image;
int maxI = image.getHeight(); int maxI = image.getHeight();
int[] stateCount = new int[3]; int[] stateCount = crossCheckStateCount;
stateCount[0] = 0;
stateCount[1] = 0;
stateCount[2] = 0;
// Start counting up from center // Start counting up from center
int i = startI; int i = startI;

View file

@ -45,6 +45,7 @@ final class FinderPatternFinder {
private final MonochromeBitmapSource image; private final MonochromeBitmapSource image;
private final Vector possibleCenters; private final Vector possibleCenters;
private boolean hasSkipped; private boolean hasSkipped;
private final int[] crossCheckStateCount;
/** /**
* <p>Creates a finder that will search the image for three finder patterns.</p> * <p>Creates a finder that will search the image for three finder patterns.</p>
@ -54,6 +55,7 @@ final class FinderPatternFinder {
FinderPatternFinder(MonochromeBitmapSource image) { FinderPatternFinder(MonochromeBitmapSource image) {
this.image = image; this.image = image;
this.possibleCenters = new Vector(); this.possibleCenters = new Vector();
this.crossCheckStateCount = new int[5];
} }
FinderPatternInfo find(Hashtable hints) throws ReaderException { FinderPatternInfo find(Hashtable hints) throws ReaderException {
@ -74,9 +76,10 @@ final class FinderPatternFinder {
boolean done = false; boolean done = false;
int[] stateCount = new int[5]; int[] stateCount = new int[5];
BitArray blackRow = new BitArray(maxJ);
for (int i = iSkip - 1; i < maxI && !done; i += iSkip) { for (int i = iSkip - 1; i < maxI && !done; i += iSkip) {
// Get a row of black/white values // Get a row of black/white values
BitArray blackRow = image.getBlackRow(i, null, 0, maxJ); blackRow = image.getBlackRow(i, blackRow, 0, maxJ);
stateCount[0] = 0; stateCount[0] = 0;
stateCount[1] = 0; stateCount[1] = 0;
stateCount[2] = 0; stateCount[2] = 0;
@ -199,6 +202,15 @@ final class FinderPatternFinder {
Math.abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance; Math.abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance;
} }
private int[] getCrossCheckStateCount() {
crossCheckStateCount[0] = 0;
crossCheckStateCount[1] = 0;
crossCheckStateCount[2] = 0;
crossCheckStateCount[3] = 0;
crossCheckStateCount[4] = 0;
return crossCheckStateCount;
}
/** /**
* <p>After a horizontal scan finds a potential finder pattern, this method * <p>After a horizontal scan finds a potential finder pattern, this method
* "cross-checks" by scanning down vertically through the center of the possible * "cross-checks" by scanning down vertically through the center of the possible
@ -214,7 +226,7 @@ final class FinderPatternFinder {
MonochromeBitmapSource image = this.image; MonochromeBitmapSource image = this.image;
int maxI = image.getHeight(); int maxI = image.getHeight();
int[] stateCount = new int[5]; int[] stateCount = getCrossCheckStateCount();
// Start counting up from center // Start counting up from center
int i = startI; int i = startI;
@ -284,7 +296,7 @@ final class FinderPatternFinder {
MonochromeBitmapSource image = this.image; MonochromeBitmapSource image = this.image;
int maxJ = image.getWidth(); int maxJ = image.getWidth();
int[] stateCount = new int[5]; int[] stateCount = getCrossCheckStateCount();
int j = startJ; int j = startJ;
while (j >= 0 && image.isBlack(j, centerI)) { while (j >= 0 && image.isBlack(j, centerI)) {