mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Rationalize the black-run counting to avoid AIOOBE but also get the right answer still
git-svn-id: https://zxing.googlecode.com/svn/trunk@1820 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
f17a43df81
commit
7f06b18976
|
@ -256,33 +256,35 @@ public class Detector {
|
||||||
*/
|
*/
|
||||||
private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) {
|
private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) {
|
||||||
|
|
||||||
float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
|
float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);
|
||||||
|
|
||||||
// Now count other way -- don't run off image though of course
|
// Now count other way -- don't run off image though of course
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
int otherToX = fromX - (toX - fromX);
|
int otherToX = fromX - (toX - fromX);
|
||||||
if (otherToX < 0) {
|
if (otherToX < 0) {
|
||||||
scale = (float) fromX / (float) (fromX - otherToX);
|
scale = (float) fromX / (float) (fromX - otherToX);
|
||||||
otherToX = 0;
|
otherToX = 0;
|
||||||
} else if (otherToX > image.getWidth()) {
|
} else if (otherToX >= image.getWidth()) {
|
||||||
scale = (float) (image.getWidth() - fromX) / (float) (otherToX - fromX);
|
scale = (float) (image.getWidth() - 1 - fromX) / (float) (otherToX - fromX);
|
||||||
otherToX = image.getWidth();
|
otherToX = image.getWidth() - 1;
|
||||||
}
|
}
|
||||||
int otherToY = (int) (fromY - (toY - fromY) * scale);
|
int otherToY = (int) (fromY - (toY - fromY) * scale);
|
||||||
|
|
||||||
scale = 1.0f;
|
scale = 1.0f;
|
||||||
if (otherToY < 0) {
|
if (otherToY < 0) {
|
||||||
scale = (float) fromY / (float) (fromY - otherToY);
|
scale = (float) fromY / (float) (fromY - otherToY);
|
||||||
otherToY = 0;
|
otherToY = 0;
|
||||||
} else if (otherToY > image.getHeight()) {
|
} else if (otherToY >= image.getHeight()) {
|
||||||
scale = (float) (image.getHeight() - fromY) / (float) (otherToY - fromY);
|
scale = (float) (image.getHeight() - 1 - fromY) / (float) (otherToY - fromY);
|
||||||
otherToY = image.getHeight();
|
otherToY = image.getHeight() - 1;
|
||||||
}
|
}
|
||||||
otherToX = (int) (fromX + (otherToX - fromX) * scale);
|
otherToX = (int) (fromX + (otherToX - fromX) * scale);
|
||||||
|
|
||||||
result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
|
result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);
|
||||||
return result;
|
|
||||||
}
|
// Middle pixel is double-counted this way; subtract 1
|
||||||
|
return result - 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This method traces a line from a point in the image, in the direction towards another point.
|
* <p>This method traces a line from a point in the image, in the direction towards another point.
|
||||||
|
@ -313,32 +315,22 @@ public class Detector {
|
||||||
|
|
||||||
// In black pixels, looking for white, first or second time.
|
// In black pixels, looking for white, first or second time.
|
||||||
int state = 0;
|
int state = 0;
|
||||||
for (int x = fromX, y = fromY; x != toX; x += xstep) {
|
// Loop up until x == toX, but not beyond
|
||||||
|
int xLimit = toX + xstep;
|
||||||
|
for (int x = fromX, y = fromY; x != xLimit; x += xstep) {
|
||||||
int realX = steep ? y : x;
|
int realX = steep ? y : x;
|
||||||
int realY = steep ? x : y;
|
int realY = steep ? x : y;
|
||||||
|
|
||||||
// In white pixels, looking for black.
|
// Does current pixel mean we have moved white to black or vice versa?
|
||||||
// FIXME(dswitkin): This method seems to assume square images, which can cause these calls to
|
if (!(state == 1 ^ image.get(realX, realY))) {
|
||||||
// BitMatrix.get() to throw ArrayIndexOutOfBoundsException.
|
if (state == 2) {
|
||||||
if (state == 1) {
|
int diffX = x - fromX;
|
||||||
if (image.get(realX, realY)) {
|
int diffY = y - fromY;
|
||||||
state++;
|
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!image.get(realX, realY)) {
|
|
||||||
state++;
|
|
||||||
}
|
}
|
||||||
|
state++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Found black, white, black, and stumbled back onto white, so we're done.
|
|
||||||
if (state == 3) {
|
|
||||||
int diffX = x - fromX;
|
|
||||||
int diffY = y - fromY;
|
|
||||||
if (xstep < 0) {
|
|
||||||
diffX++;
|
|
||||||
}
|
|
||||||
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
|
||||||
}
|
|
||||||
error += dy;
|
error += dy;
|
||||||
if (error > 0) {
|
if (error > 0) {
|
||||||
if (y == toY) {
|
if (y == toY) {
|
||||||
|
@ -348,9 +340,16 @@ public class Detector {
|
||||||
error -= dx;
|
error -= dx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int diffX = toX - fromX;
|
// Found black-white-black; give the benefit of the doubt that the next pixel outside the image
|
||||||
int diffY = toY - fromY;
|
// is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
|
||||||
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
|
||||||
|
if (state == 2) {
|
||||||
|
int diffX = toX + xstep - fromX;
|
||||||
|
int diffY = toY - fromY;
|
||||||
|
return (float) Math.sqrt((double) (diffX * diffX + diffY * diffY));
|
||||||
|
}
|
||||||
|
// else we didn't find even black-white-black; no estimate is really possible
|
||||||
|
return Float.NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue