mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Port Java qr detector changes
re-port Detector::sizeOfBlackWhiteBlackRunBothWays and Detector::sizeOfBlackWhiteBlackRun to reflect changes from r1820 and earlier. git-svn-id: https://zxing.googlecode.com/svn/trunk@1838 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
10065f8c1c
commit
0d4ae9fef6
|
@ -189,9 +189,9 @@ float Detector::sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX,
|
||||||
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 > (int)image_->getWidth()) {
|
} else if (otherToX >= (int)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);
|
||||||
|
|
||||||
|
@ -199,14 +199,16 @@ float Detector::sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX,
|
||||||
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 > (int)image_->getHeight()) {
|
} else if (otherToY >= (int)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;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
|
float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {
|
||||||
|
@ -225,40 +227,46 @@ float Detector::sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY)
|
||||||
int dx = abs(toX - fromX);
|
int dx = abs(toX - fromX);
|
||||||
int dy = abs(toY - fromY);
|
int dy = abs(toY - fromY);
|
||||||
int error = -dx >> 1;
|
int error = -dx >> 1;
|
||||||
int ystep = fromY < toY ? 1 : -1;
|
|
||||||
int xstep = fromX < toX ? 1 : -1;
|
int xstep = fromX < toX ? 1 : -1;
|
||||||
int state = 0; // In black pixels, looking for white, first or second time
|
int ystep = fromY < toY ? 1 : -1;
|
||||||
for (int x = fromX, y = fromY; x != toX; x += xstep) {
|
|
||||||
|
|
||||||
|
// In black pixels, looking for white, first or second time.
|
||||||
|
int state = 0;
|
||||||
|
// 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;
|
||||||
if (state == 1) { // In white pixels, looking for black
|
|
||||||
if (image_->get(realX, realY)) {
|
|
||||||
state++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!image_->get(realX, realY)) {
|
|
||||||
state++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == 3) { // Found black, white, black, and stumbled back onto white; done
|
// Does current pixel mean we have moved white to black or vice versa?
|
||||||
|
if (!(state == 1 ^ image_->get(realX, realY))) {
|
||||||
|
if (state == 2) {
|
||||||
int diffX = x - fromX;
|
int diffX = x - fromX;
|
||||||
int diffY = y - fromY;
|
int diffY = y - fromY;
|
||||||
if (xstep < 0) {
|
return (float) sqrt((double) (diffX * diffX + diffY * diffY));
|
||||||
diffX++;
|
|
||||||
}
|
}
|
||||||
return (float)sqrt((double)(diffX * diffX + diffY * diffY));
|
state++;
|
||||||
}
|
}
|
||||||
|
|
||||||
error += dy;
|
error += dy;
|
||||||
if (error > 0) {
|
if (error > 0) {
|
||||||
|
if (y == toY) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
y += ystep;
|
y += ystep;
|
||||||
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
|
||||||
|
// is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a
|
||||||
|
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
|
||||||
|
if (state == 2) {
|
||||||
|
int diffX = toX + xstep - fromX;
|
||||||
int diffY = toY - fromY;
|
int diffY = toY - fromY;
|
||||||
return (float)sqrt((double)(diffX * diffX + diffY * diffY));
|
return (float) sqrt((double) (diffX * diffX + diffY * diffY));
|
||||||
|
}
|
||||||
|
// else we didn't find even black-white-black; no estimate is really possible
|
||||||
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref<AlignmentPattern> Detector::findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY,
|
Ref<AlignmentPattern> Detector::findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY,
|
||||||
|
|
Loading…
Reference in a new issue